0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
diagram_element.h
Go to the documentation of this file.
1/**
2 * src/common/model/diagram_element.h
3 *
4 * Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
20#include "decorated_element.h"
21#include "relationship.h"
22#include "source_location.h"
23#include "util/util.h"
24
25#include <inja/inja.hpp>
26
27#include <atomic>
28#include <exception>
29#include <set>
30#include <string>
31#include <vector>
32
34
35class diagram_filter;
36
37/**
38 * @brief Base class for standalone diagram elements.
39 *
40 * This is a base cass of any standalone elements such as classes, structs,
41 * concepts, packages and so on participants and so on.
42 */
44public:
46
47 ~diagram_element() override = default;
48
49 /**
50 * @brief Returns diagram element id.
51 *
52 * Each element in the diagram is uniquely identified by id. The id
53 * is currently calculated from the full string representation of the
54 * element, in order to be uniquely identifiable among multiple translation
55 * units.
56 *
57 * @return Elements id.
58 */
59 const eid_t &id() const;
60
61 /**
62 * Set elements id.
63 *
64 * @param id Elements id.
65 */
66 void set_id(eid_t id);
67
68 /**
69 * Get elements parent package id.
70 *
71 * @return Parent package id if element is nested.
72 */
73 std::optional<eid_t> parent_element_id() const;
74
75 /**
76 * Set elements parent package id.
77 *
78 * @param id Id of parent package.
79 */
81
82 /**
83 * @brief Return elements' diagram alias.
84 *
85 * @todo This is a PlantUML specific method - it shouldn't be here.
86 *
87 * @return PlantUML's diagram element alias.
88 */
89 virtual std::string alias() const;
90
91 /**
92 * Set diagram elements name.
93 *
94 * @param name Elements name.
95 */
96 void set_name(const std::string &name) { name_ = name; }
97
98 /**
99 * Return diagram element name.
100 *
101 * @return Diagram element name.
102 */
103 std::string name() const { return name_; }
104
105 /**
106 * Return the type name of the diagram element.
107 *
108 * @return Diagrams element type name.
109 */
110 virtual std::string type_name() const { return "__undefined__"; };
111
112 /**
113 * @brief Return the elements fully qualified name.
114 *
115 * This method should be implemented in each subclass, and ensure that
116 * for instance it includes fully qualified namespace, template params, etc.
117 *
118 * @return Full elements name.
119 */
120 virtual std::string full_name(bool /*relative*/) const { return name(); }
121
122 /**
123 * Return all relationships outgoing from this element.
124 *
125 * @return List of relationships.
126 */
127 std::vector<relationship> &relationships();
128
129 /**
130 * Return all relationships outgoing from this element.
131 *
132 * @return List of relationships.
133 */
134 const std::vector<relationship> &relationships() const;
135
136 /**
137 * Add relationships, whose source is this element.
138 *
139 * @param cr Relationship to another diagram element.
140 */
142
143 /**
144 * Add element to the diagram.
145 *
146 * @param e Diagram element.
147 */
148 void append(const decorated_element &e);
149
150 friend bool operator==(const diagram_element &l, const diagram_element &r);
151
152 friend std::ostream &operator<<(
153 std::ostream &out, const diagram_element &rhs);
154
155 /**
156 * Return elements inja JSON context.
157 *
158 * @return Element context.
159 */
160 virtual inja::json context() const;
161
162 /**
163 * Whether this element is nested in another element.
164 *
165 * @return
166 */
167 bool is_nested() const;
168
169 /**
170 * Set element's nested status.
171 *
172 * @param nested
173 */
174 void nested(bool nested);
175
176 /**
177 * Returns the diagrams completion status.
178 *
179 * @return Whether the diagram is complete.
180 */
181 bool complete() const;
182
183 /**
184 * Set the diagrams completion status.
185 *
186 * @param completed
187 */
188 void complete(bool completed);
189
190 virtual void apply_filter(
191 const diagram_filter &filter, const std::set<eid_t> &removed);
192
193private:
195 std::optional<eid_t> parent_element_id_{};
196 std::string name_;
197 std::vector<relationship> relationships_;
198 bool nested_{false};
199 bool complete_{false};
200};
201} // namespace clanguml::common::model