0.6.1
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
diagram.h
Go to the documentation of this file.
1/**
2 * @file src/package_diagram/model/diagram.h
3 *
4 * Copyright (c) 2021-2025 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
23
24#include <string>
25#include <vector>
26
28
34
38
39/**
40 * @brief Package diagram model.
41 */
44 public nested_trait_ns {
45public:
47
48 diagram() = default;
49
50 diagram(const diagram &) = delete;
51 diagram(diagram &&) = default;
52 diagram &operator=(const diagram &) = delete;
53 diagram &operator=(diagram &&) = default;
54
55 /**
56 * @brief Get the diagram model type - in this case package.
57 *
58 * @return Type of package diagram.
59 */
60 common::model::diagram_t type() const override;
61
62 /**
63 * @brief Get list of references to packages in the diagram model.
64 *
65 * @return List of references to packages in the diagram model.
66 */
68
69 /**
70 * @brief Search for element in the diagram by fully qualified name.
71 *
72 * @param full_name Fully qualified element name.
73 * @return Optional reference to a diagram element.
74 */
75 opt_ref<diagram_element> get(const std::string &full_name) const override;
76
77 /**
78 * @brief Search for element in the diagram by id.
79 *
80 * @param id Element id.
81 * @return Optional reference to a diagram element.
82 */
83 opt_ref<diagram_element> get(eid_t id) const override;
84
85 /**
86 * @brief Find an element in the diagram by name.
87 *
88 * This method allows for typed search, where the type of searched for
89 * element is determined from template specialization.
90 *
91 * @tparam ElementT Type of element (e.g. package)
92 * @param name Fully qualified name of the element
93 * @return Optional reference to a diagram element
94 */
95 template <typename ElementT>
96 opt_ref<ElementT> find(const std::string &name) const;
97
98 /**
99 * @brief Find an element in the diagram by id.
100 *
101 * This method allows for typed search, where the type of searched for
102 * element is determined from template specialization.
103 *
104 * @tparam ElementT Type of element (e.g. package)
105 * @param id Id of the element
106 * @return Optional reference to a diagram element
107 */
108 template <typename ElementT> opt_ref<ElementT> find(eid_t id) const;
109
110 /**
111 * @brief Find elements in the diagram by regex pattern.
112 *
113 * This method allows for typed search, where the type of searched for
114 * element is determined from template specialization.
115 *
116 * @tparam ElementT Type of element (e.g. class_)
117 * @param name String or regex pattern
118 * @return List of optional references to matched elements.
119 */
120 template <typename ElementT>
121 std::vector<opt_ref<ElementT>> find(
122 const clanguml::common::string_or_regex &pattern) const;
123
124 /**
125 * @brief Add diagram element at nested path
126 *
127 * This method handled both diagrams where packages are created from
128 * namespaces, as well as those were packages are created from project
129 * subdirectories.
130 *
131 * @tparam ElementT Type of diagram element to add
132 * @param parent_path Package nested path where the element should be added
133 * @param e Diagram element to add
134 * @return True, if the element was added.
135 */
136 template <typename ElementT>
137 bool add(const path &parent_path, std::unique_ptr<ElementT> &&e)
138 {
139 if (parent_path.type() == common::model::path_type::kNamespace) {
140 return add_with_namespace_path(std::move(e));
141 }
142
143 if (parent_path.type() == common::model::path_type::kModule) {
144 return add_with_module_path(parent_path, std::move(e));
145 }
146
147 return add_with_filesystem_path(parent_path, std::move(e));
148 }
149
150 /**
151 * @brief Get alias of existing diagram element
152 *
153 * @param id Id of a package in the diagram
154 * @return PlantUML alias of the element
155 */
156 std::string to_alias(eid_t id) const;
157
158 /**
159 * @brief Check whether the diagram is empty
160 *
161 * @return True, if diagram is empty
162 */
163 bool is_empty() const override;
164
165 void apply_filter() override;
166
167 /**
168 * @brief Get reference to vector of elements of specific type
169 *
170 * @tparam ElementT Type of elements view
171 * @return Reference to elements vector
172 */
173 template <typename ElementT>
175
176private:
177 /**
178 * @brief Add element using module as diagram path
179 *
180 * @tparam ElementT Element type
181 * @param e Element to add
182 * @return True, if the element was added
183 */
184 template <typename ElementT>
186 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
187
188 /**
189 * @brief Add element using namespace as diagram path
190 *
191 * @tparam ElementT Element type
192 * @param e Element to add
193 * @return True, if the element was added
194 */
195 template <typename ElementT>
196 bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);
197
198 /**
199 * @brief Add element using relative filesystem path as diagram path
200 *
201 * @tparam ElementT Element type
202 * @param parent_path Path to diagram elements parent package
203 * @param e Element to add
204 * @return True, if the element was added
205 */
206 template <typename ElementT>
208 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
209};
210
211template <typename ElementT>
212opt_ref<ElementT> diagram::find(const std::string &name) const
213{
214 for (const auto &element : element_view<ElementT>::view()) {
215 const auto full_name = element.get().full_name(false);
216
217 if (full_name == name) {
218 return {element};
219 }
220 }
221
222 return {};
223}
224
225template <typename ElementT> opt_ref<ElementT> diagram::find(eid_t id) const
226{
227 for (const auto &element : element_view<ElementT>::view()) {
228 if (element.get().id() == id) {
229 return {element};
230 }
231 }
232
233 return {};
234}
235
236template <typename ElementT>
237std::vector<opt_ref<ElementT>> diagram::find(
238 const common::string_or_regex &pattern) const
239{
240 std::vector<opt_ref<ElementT>> result;
241
242 for (const auto &element : element_view<ElementT>::view()) {
243 const auto full_name = element.get().full_name(false);
244
245 if (pattern == full_name) {
246 result.emplace_back(element);
247 }
248 }
249
250 return result;
251}
252
253template <typename ElementT>
254bool diagram::add_with_namespace_path(std::unique_ptr<ElementT> &&p)
255{
256 LOG_DBG(
257 "Adding package: {}, {}, [{}]", p->name(), p->full_name(true), p->id());
258
259 auto ns = p->get_relative_namespace();
260 auto p_ref = std::ref(*p);
261
262 auto res = add_element(ns, std::move(p));
263 if (res)
264 element_view<ElementT>::add(p_ref);
265
266 return res;
267}
268
269template <typename ElementT>
271 const common::model::path &parent_path, std::unique_ptr<ElementT> &&p)
272{
273 LOG_DBG("Adding package: {}, {}, {}, [{}]", p->name(), p->full_name(false),
274 parent_path.to_string(), p->id());
275
276 // Make sure all parent modules are already packages in the
277 // model
278 auto module_relative_to = path{p->using_namespace()};
279
280 for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
281 auto pkg = std::make_unique<common::model::package>(
282 p->using_namespace(), common::model::path_type::kModule);
283 pkg->set_name(*it);
284
285 auto module_relative_part = common::model::path(
286 parent_path.begin(), it, common::model::path_type::kModule);
287
288 auto module_absolute_path = module_relative_to | module_relative_part;
289 pkg->set_module(module_absolute_path.to_string());
290 pkg->set_namespace(module_absolute_path);
291
292 auto package_absolute_path = module_absolute_path | pkg->name();
293
294 pkg->set_id(common::to_id(package_absolute_path.to_string()));
295
296 auto p_ref = std::ref(*pkg);
297
298 auto res = add_element(module_relative_part, std::move(pkg));
299 if (res)
300 element_view<ElementT>::add(p_ref);
301 }
302
303 auto p_ref = std::ref(*p);
304
305 auto res = add_element(parent_path, std::move(p));
306 if (res)
307 element_view<ElementT>::add(p_ref);
308
309 return res;
310}
311
312template <typename ElementT>
314 const common::model::path &parent_path, std::unique_ptr<ElementT> &&p)
315{
316 LOG_TRACE("Adding package: {}, {}", p->name(), p->full_name(true));
317
318 // Make sure all parent directories are already packages in the
319 // model
320 for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
321 auto pkg = std::make_unique<common::model::package>(
322 p->using_namespace(), common::model::path_type::kFilesystem);
323 pkg->set_name(*it);
324 auto ns = common::model::path(
326 pkg->set_namespace(ns);
327 pkg->set_id(common::to_id(pkg->full_name(false)));
328
329 add_with_filesystem_path(ns, std::move(pkg));
330 }
331
332 auto pp = std::ref(*p);
333 auto res = add_element(parent_path, std::move(p));
334 if (res)
335 element_view<ElementT>::add(pp);
336
337 return res;
338}
339
340template <typename ElementT>
342{
343 return element_view<ElementT>::view();
344}
345
346} // namespace clanguml::package_diagram::model
347
348namespace clanguml::common::model {
349template <>
350bool check_diagram_type<clanguml::package_diagram::model::diagram>(diagram_t t);
351}