0.6.0
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/class_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
20#include "class.h"
25#include "common/types.h"
26#include "concept.h"
27#include "config/config.h"
28#include "enum.h"
29#include "objc_interface.h"
30
31#include <regex>
32#include <string>
33#include <unordered_set>
34#include <vector>
35
37
38using common::opt_ref;
39using common::model::diagram_element;
41using common::model::eid_t;
42using common::model::element_view;
43using common::model::element_views;
44using common::model::path;
46
50
51/**
52 * @brief Class representing a class diagram.
53 */
55 public element_views<class_, enum_, concept_, objc_interface>,
56 public nested_trait_ns {
57public:
58 diagram() = default;
59
60 diagram(const diagram &) = delete;
61 diagram(diagram &&) = default;
62 diagram &operator=(const diagram &) = delete;
63 diagram &operator=(diagram &&) = default;
64
65 /**
66 * @brief Get the diagram model type - in this case class.
67 *
68 * @return Type of class diagram.
69 */
70 diagram_t type() const override;
71
72 /**
73 * Inherit the should_include methods from the common diagram model.
74 */
76
77 /**
78 * @brief Whether a class_member should be included in the diagram.
79 *
80 * @param m Class member
81 * @return True, if class member should be included in the diagram.
82 */
83 bool should_include(const class_member &m) const;
84 bool should_include(const objc_member &m) const;
85
86 /**
87 * @brief Whether a class_method should be included in the diagram.
88 *
89 * @param m Class method
90 * @return True, if class method should be included in the diagram.
91 */
92 bool should_include(const class_method &m) const;
93 bool should_include(const objc_method &m) const;
94
95 /**
96 * @brief Search for element in the diagram by fully qualified name.
97 *
98 * @param full_name Fully qualified element name.
99 * @return Optional reference to a diagram element.
100 */
101 opt_ref<diagram_element> get(const std::string &full_name) const override;
102
103 /**
104 * @brief Search for element in the diagram by id.
105 *
106 * @param id Element id.
107 * @return Optional reference to a diagram element.
108 */
109 opt_ref<diagram_element> get(eid_t id) const override;
110
111 /**
112 * @brief Get list of references to classes in the diagram model.
113 *
114 * @return List of references to classes in the diagram model.
115 */
117
118 /**
119 * @brief Get list of references to enums in the diagram model.
120 *
121 * @return List of references to enums in the diagram model.
122 */
124
125 /**
126 * @brief Get list of references to concepts in the diagram model.
127 *
128 * @return List of references to concepts in the diagram model.
129 */
131
133
134 /**
135 * @brief Check, if diagram contains a specific element.
136 *
137 * @tparam ElementT Type of diagram element (e.g. class_)
138 * @param e Element to check
139 * @return True, if element already exists in the diagram
140 */
141 template <typename ElementT> bool contains(const ElementT &e);
142
143 /**
144 * @brief Find an element in the diagram by name.
145 *
146 * This method allows for typed search, where the type of searched for
147 * element is determined from template specialization.
148 *
149 * @tparam ElementT Type of element (e.g. class_)
150 * @param name Fully qualified name of the element
151 * @return Optional reference to a diagram element
152 */
153 template <typename ElementT>
154 opt_ref<ElementT> find(const std::string &name) const;
155
156 /**
157 * @brief Find elements in the diagram by regex pattern.
158 *
159 * This method allows for typed search, where the type of searched for
160 * element is determined from template specialization.
161 *
162 * @tparam ElementT Type of element (e.g. class_)
163 * @param name String or regex pattern
164 * @return List of optional references to matched elements.
165 */
166 template <typename ElementT>
167 std::vector<opt_ref<ElementT>> find(
168 const clanguml::common::string_or_regex &pattern) const;
169
170 /**
171 * @brief Find an element in the diagram by id.
172 *
173 * This method allows for typed search, where the type of searched for
174 * element is determined from template specialization.
175 *
176 * @tparam ElementT Type of element (e.g. class_)
177 * @param id Id of the element
178 * @return Optional reference to a diagram element
179 */
180 template <typename ElementT> opt_ref<ElementT> find(eid_t id) const;
181
182 /**
183 * @brief Get reference to vector of elements of specific type
184 *
185 * @tparam ElementT Type of elements view
186 * @return Reference to elements vector
187 */
188 template <typename ElementT>
190
191 /**
192 * @brief Add element to the diagram at a specified nested path.
193 *
194 * Adds an element to a diagram, at a specific package (if any exist).
195 * The package is specified by the `parent_path`, which can be either
196 * a namespace or a directory path.
197 *
198 * @tparam ElementT Type of diagram element.
199 * @param parent_path Path to the parent package of the new diagram element.
200 * @param e Diagram element to be added.
201 * @return True, if the element was added to the diagram.
202 */
203 template <typename ElementT>
204 bool add(const path &parent_path, std::unique_ptr<ElementT> &&e)
205 {
206 if (parent_path.type() == common::model::path_type::kNamespace) {
207 return add_with_namespace_path(std::move(e));
208 }
209
210 if (parent_path.type() == common::model::path_type::kModule) {
211 return add_with_module_path(parent_path, std::move(e));
212 }
213
214 return add_with_filesystem_path(parent_path, std::move(e));
215 }
216
217 /**
218 * @brief Convert element id to PlantUML alias.
219 *
220 * @todo This method does not belong here - refactor to PlantUML specific
221 * code.
222 *
223 * @param id Id of the diagram element.
224 * @return PlantUML alias.
225 */
226 std::string to_alias(eid_t id) const;
227
228 /**
229 * @brief Given an initial set of classes, add all their parents to the
230 * argument.
231 * @param parents In and out parameter with the parent classes.
232 */
234
235 /**
236 * @brief Check if diagram contains element by id.
237 *
238 * @todo Remove in favour of 'contains'
239 *
240 * @param id Id of the element.
241 * @return True, if diagram contains an element with a specific id.
242 */
243 bool has_element(eid_t id) const override;
244
245 /**
246 * @brief Remove redundant dependency relationships
247 */
249
250 /**
251 * @brief Check whether the diagram is empty
252 *
253 * @return True, if diagram is empty
254 */
255 bool is_empty() const override;
256
257 void apply_filter() override;
258
259private:
260 template <typename ElementT>
261 bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);
262
263 template <typename ElementT>
265 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
266
267 template <typename ElementT>
269 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
270};
271
272template <typename ElementT> bool diagram::contains(const ElementT &element)
273{
274 return std::any_of(element_view<ElementT>::view().cbegin(),
276 [&element](
277 const auto &element_opt) { return element_opt.get() == element; });
278}
279
280template <typename ElementT>
281bool diagram::add_with_namespace_path(std::unique_ptr<ElementT> &&e)
282{
283 const auto base_name = e->name();
284 const auto full_name = e->full_name(false);
285 const auto element_type = e->type_name();
286
287 LOG_DBG("Adding {}: {}::{}, {}", element_type,
288 e->get_namespace().to_string(), base_name, full_name);
289
290 if (util::contains(base_name, "::"))
291 throw std::runtime_error("Name cannot contain namespace: " + base_name);
292
293 if (util::contains(base_name, "*"))
294 throw std::runtime_error("Name cannot contain *: " + base_name);
295
296 const auto ns = e->get_relative_namespace();
297 auto name = base_name;
298 auto name_and_ns = ns | name;
299 auto &e_ref = *e;
300 auto id = e_ref.id();
301
302 try {
303 if (!contains(e_ref)) {
304 if (add_element(ns, std::move(e)))
305 element_view<ElementT>::add(std::ref(e_ref));
306
307 const auto &el = get_element<ElementT>(name_and_ns).value();
308
309 if ((el.name() != name) || !(el.get_relative_namespace() == ns))
310 throw std::runtime_error(
311 "Invalid element stored in the diagram tree");
312
313 LOG_DBG("Added {} {} ({} - [{}])", element_type, base_name,
314 full_name, id);
315
316 return true;
317 }
318 }
319 catch (const std::runtime_error &e) {
320 LOG_WARN("Cannot add {} {} with id {} due to: {}", element_type, name,
321 id, e.what());
322 return false;
323 }
324
325 LOG_DBG("{} {} ({} - [{}]) already in the model", element_type, base_name,
326 full_name, id);
327
328 return false;
329}
330
331template <typename ElementT>
333 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e)
334{
335 const auto element_type = e->type_name();
336
337 // Make sure all parent modules are already packages in the
338 // model
339 for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
340 auto pkg = std::make_unique<common::model::package>(
341 e->using_namespace(), parent_path.type());
342 pkg->set_name(*it);
343 auto ns =
344 common::model::path(parent_path.begin(), it, parent_path.type());
345 // ns.pop_back();
346 pkg->set_namespace(ns);
347 pkg->set_id(common::to_id(pkg->full_name(false)));
348
349 add(ns, std::move(pkg));
350 }
351
352 const auto base_name = e->name();
353 const auto full_name = e->full_name(false);
354 auto &e_ref = *e;
355
356 if (add_element(parent_path, std::move(e))) {
357 element_view<ElementT>::add(std::ref(e_ref));
358 return true;
359 }
360
361 return false;
362}
363
364template <typename ElementT>
366 const common::model::path &parent_path, std::unique_ptr<ElementT> &&e)
367{
368 const auto element_type = e->type_name();
369
370 // Make sure all parent modules are already packages in the
371 // model
372 for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
373 auto pkg = std::make_unique<common::model::package>(
374 e->using_namespace(), parent_path.type());
375 pkg->set_name(*it);
376 auto ns =
377 common::model::path(parent_path.begin(), it, parent_path.type());
378 // ns.pop_back();
379 pkg->set_namespace(ns);
380 pkg->set_id(common::to_id(pkg->full_name(false)));
381
382 add(ns, std::move(pkg));
383 }
384
385 const auto base_name = e->name();
386 const auto full_name = e->full_name(false);
387 auto &e_ref = *e;
388
389 if (add_element(parent_path, std::move(e))) {
390 element_view<ElementT>::add(std::ref(e_ref));
391 return true;
392 }
393
394 return false;
395}
396
397template <typename ElementT>
398opt_ref<ElementT> diagram::find(const std::string &name) const
399{
400 for (const auto &element : element_view<ElementT>::view()) {
401 const auto full_name = element.get().full_name(false);
402
403 auto full_name_escaped = full_name;
404 util::replace_all(full_name_escaped, "##", "::");
405
406 if (name == full_name || name == full_name_escaped) {
407 return {element};
408 }
409 }
410
411 return {};
412}
413
414template <typename ElementT>
415std::vector<opt_ref<ElementT>> diagram::find(
416 const common::string_or_regex &pattern) const
417{
418 std::vector<opt_ref<ElementT>> result;
419
420 for (const auto &element : element_view<ElementT>::view()) {
421 const auto full_name = element.get().full_name(false);
422 auto full_name_escaped = full_name;
423 util::replace_all(full_name_escaped, "##", "::");
424
425 if (pattern == full_name || pattern == full_name_escaped) {
426 result.emplace_back(element);
427 }
428 }
429
430 return result;
431}
432
433template <typename ElementT> opt_ref<ElementT> diagram::find(eid_t id) const
434{
435 for (const auto &element : element_view<ElementT>::view()) {
436 if (element.get().id() == id) {
437 return {element};
438 }
439 }
440
441 return {};
442}
443
444template <typename ElementT>
446{
448}
449
450//
451// Template method specialization pre-declarations...
452//
453template <>
454bool diagram::add_with_namespace_path<common::model::package>(
455 std::unique_ptr<common::model::package> &&p);
456
457template <>
458bool diagram::add_with_module_path<common::model::package>(
459 const common::model::path &parent_path,
460 std::unique_ptr<common::model::package> &&p);
461
462template <>
463bool diagram::add_with_filesystem_path<common::model::package>(
464 const common::model::path &parent_path,
465 std::unique_ptr<common::model::package> &&p);
466
467} // namespace clanguml::class_diagram::model
468
469namespace clanguml::common::model {
470template <>
471bool check_diagram_type<clanguml::class_diagram::model::diagram>(diagram_t t);
472}