forked from Skazzi00/GraphVizLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gvl.h
133 lines (109 loc) · 3.26 KB
/
gvl.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
#ifndef GVL_H_
#define GVL_H_
#include <utility>
#include <vector>
#include <unordered_set>
#include <string_view>
#include <ostream>
namespace gvl {
struct Property {
std::string_view name;
std::string_view value;
Property(const std::string_view& name, const std::string_view& value) : name(name), value(value) {}
};
struct NodeId {
std::string_view name;
std::string_view port;
NodeId(const char* name, const char* port = "") : name(name), port(port) {}
};
struct Edge {
NodeId from;
NodeId to;
std::vector<Property> properties;
Edge(const NodeId& from, const NodeId& to, std::vector<Property> properties = {})
: from(from), to(to), properties(std::move(properties)) {}
};
struct Node {
NodeId id;
std::vector<Property> properties;
explicit Node(const NodeId& id, std::vector<Property> properties = {}) : id(id), properties(std::move(properties)) {}
};
/*!
* A class describing the structure of the desired graph for output through GraphViz.
* All Strings should be alive when RenderDot will be called. Temporary strings are not allowed.
*/
class Graph {
public:
/*!
* Create empty graph
* @param graph_name name of created graph
*/
explicit Graph(const std::string_view& graph_name)
: nodes_(),
edges_(),
graph_name_(graph_name),
graph_props_(),
common_edge_props_(),
common_node_props_() {}
// Ban copy and move
Graph(const Graph& other) = delete;
Graph& operator=(const Graph& other) = delete;
Graph(Graph&& other) = delete;
Graph& operator=(Graph&& other) = delete;
/*!
* Add a new node to the list of nodes with default properties
* @param node name and port.
*/
void AddNode(const NodeId& node) {
const std::vector<Property> default_node_properties = {{"label", node.name}};
nodes_.emplace_back(node, default_node_properties);
}
/*!
* Add a new edge to the list of edges with no properties.
* @param from start of edge
* @param to end of edge
*/
void AddEdge(const NodeId& from, const NodeId& to) {
edges_.emplace_back(from, to);
}
/*!
* Add a property to graph
* @param name property name
* @param value property value
*/
void AddGraphProperty(const std::string_view& name, const std::string_view& value) {
graph_props_.emplace_back(name, value);
}
/*!
* Add a common property to edges
* @param name property name
* @param value property value
*/
void AddCommonEdgeProperty(const std::string_view& name, const std::string_view& value) {
common_edge_props_.emplace_back(name, value);
}
/*!
* Add a common property to nodes
* @param name property name
* @param value property value
*/
void AddCommonNodeProperty(const std::string_view& name, const std::string_view& value) {
common_node_props_.emplace_back(name, value);
}
/*!
* Write graph to file in DOT language format.
* @param out output stream to write
*/
void RenderDot(std::ostream & out) const;
private:
void PrintCommonProperties(std::ostream & out) const;
private:
std::vector<Node> nodes_;
std::vector<Edge> edges_;
std::string_view graph_name_;
std::vector<Property> graph_props_;
std::vector<Property> common_edge_props_;
std::vector<Property> common_node_props_;
};
} // namespace gvl
#endif // GVL_H_