-
Notifications
You must be signed in to change notification settings - Fork 1
/
cobjectgraph.h
252 lines (209 loc) · 7.65 KB
/
cobjectgraph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef __COBJECTGRAPH_H__
#define __COBJECTGRAPH_H__
#include <iostream>
#include <sstream>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <memory>
namespace CObjectGraph
{
class Graph;
class Position
{
public:
Position() : x{0}, y{0}, set{false} { }
Position(int x_, int y_) : x{x_}, y{y_}, set{true} { }
void Set(int x, int y) { this->x = x; this->y = y; this->set = true; }
bool IsSet() const { return set; }
int X() const
{
if (!set) throw std::logic_error("You should set the position first!");
return x;
}
int Y() const
{
if (!set) throw std::logic_error("You should set the position first!");
return y;
}
std::string ToDot() const
{
if (!set) throw std::logic_error("You should set the position first!");
std::ostringstream oss;
oss << x << "," << y;
return oss.str();
}
private:
int x, y;
bool set; // Has x, y been set?
};
class BaseNode
{
public:
BaseNode();
std::string GetName() const;
virtual std::string ToDot() = 0;
virtual void SetAttribute(std::string key, std::string value) = 0;
virtual void SetPosition(int x, int y) = 0;
virtual bool RepresentsObject(const void * object) = 0;
virtual ~BaseNode() { }
private:
std::string name;
static int counter;
};
enum class AttributeScope
{
GRAPH,
ALL_NODES,
ALL_EDGES,
SPECIFIC_NODE
};
struct Attribute
{
std::string key;
std::string value;
AttributeScope scope;
};
template <typename C> // C must be a container of Attribute e.g. vector<Attribute>
void set_attribute(C& c, std::string key, std::string value, AttributeScope scope)
{
auto f = std::find_if(std::begin(c), std::end(c),
[&key, &scope] (const Attribute& x) {
return (x.key == key) && (x.scope == scope);
});
if (f != std::end(c))
{
(*f).value = value;
}
else
{
c.push_back(Attribute {key, value, scope});
}
}
template <typename T>
class Node: public BaseNode
{
public:
explicit Node(const T* object, std::string var_name = "")
{
this->object = object;
this->var_name = var_name;
SetNodeAttributes();
}
void SetPosition(int x, int y) override
{
pos.Set(x, y);
}
std::string ToDot() override
{
std::ostringstream oss;
oss << this->GetName() << " [label=\"";
(object == nullptr) ? WriteNullNodeLabel(oss) : WriteNodeLabel(oss);
oss << "\"";
if (pos.IsSet()) oss << ", pos=\"" << pos.ToDot() << "\"";
for (const auto& a : attributes)
oss << ", " << a.key << "=\"" << a.value << "\"";
oss << "]";
return oss.str();
}
void SetAttribute(std::string key, std::string value) override
{
if (key == "label" || key == "pos")
throw std::logic_error("label and pos attributes cannot be set this way!");
set_attribute(attributes, key, value, AttributeScope::SPECIFIC_NODE);
}
bool RepresentsObject(const void * object) override
{
return ((const void *)this->object == object);
}
void AddRelatedObjects(Graph * graph)
{
// Default implementation does nothing
}
private:
const T* object;
Position pos;
std::string var_name;
std::vector<Attribute> attributes;
static const char* type_name;
void SetNodeAttributes()
{
// Default implementation does nothing
}
void WriteNodeLabel(std::ostringstream& oss)
{
oss << type_name;
}
void WriteNullNodeLabel(std::ostringstream& oss)
{
oss << "null";
}
};
class Edge
{
public:
Edge(const BaseNode * from, const BaseNode * to, std::string label);
std::string ToDot();
private:
const BaseNode * from;
const BaseNode * to;
std::string label;
};
class Graph
{
public:
explicit Graph(std::string title_ = "G", bool separate_node_for_each_null_object_ = false)
: title{title_}, separate_node_for_each_null_object{separate_node_for_each_null_object_} { }
template <typename T>
BaseNode * AddNode(const T* object, std::string var_name = "")
{
return AddNodeIfNotFound(object, false, 0, 0, var_name);
}
template <typename T>
BaseNode * AddNode(const T* object, int x, int y, std::string var_name = "")
{
return AddNodeIfNotFound(object, true, x, y, var_name);
}
void AddEdge(const void * from, const void * to, std::string label);
void AddEdge(const BaseNode * from, const BaseNode * to, std::string label);
void SetSameRank(const BaseNode * obj1Node, const BaseNode * obj2Node);
void SetSameRank(const void* obj1, const void* obj2);
void SetAttribute(AttributeScope scope, std::string key, std::string value);
void PrintDot(std::ostream& os = std::cout);
private:
std::string title;
bool separate_node_for_each_null_object;
std::vector< std::unique_ptr< BaseNode > > nodes;
std::vector< std::unique_ptr< Edge > > edges;
std::vector< Attribute > attributes;
std::vector< std::vector< const BaseNode * > > rankings;
template <typename T>
BaseNode * AddNodeIfNotFound(const T* object, bool set_pos, int x, int y, std::string var_name)
{
BaseNode * node = FindNodeForObject(object);
if (node == nullptr || (object == nullptr && separate_node_for_each_null_object))
{
Node<T> * new_node = new Node<T>(object, var_name);
nodes.push_back(std::unique_ptr<BaseNode>(new_node));
node = new_node;
if (set_pos)
new_node->SetPosition(x, y);
if (object != nullptr)
new_node->AddRelatedObjects(this);
}
return node;
}
BaseNode * FindNodeForObject(const void * object);
};
}
#define COG_DEFINE_NODE(T) \
template <> const char* CObjectGraph::Node<T>::type_name = #T;
#define COG_SET_NODE_ATTRIBUTES(T) \
template <> void CObjectGraph::Node<T>::SetNodeAttributes()
#define COG_WRITE_NODE_LABEL(T) \
template <> void CObjectGraph::Node<T>::WriteNodeLabel(std::ostringstream& oss)
#define COG_WRITE_NULL_NODE_LABEL(T) \
template <> void CObjectGraph::Node<T>::WriteNullNodeLabel(std::ostringstream& oss)
#define COG_ADD_RELATED_OBJECTS(T) \
template <> void CObjectGraph::Node<T>::AddRelatedObjects(CObjectGraph::Graph * graph)
#endif