0.5.4
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/include_diagram/model/diagram.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
24#include "common/types.h"
25
26#include <string>
27#include <vector>
28
30
35
38
39/**
40 * @brief Class representing an include diagram model.
41 */
43 public clanguml::common::model::element_view<source_file>,
44 public nested_trait_fspath {
45public:
46 diagram() = default;
47
48 diagram(const diagram &) = delete;
49 diagram(diagram &&) = default;
50 diagram &operator=(const diagram &) = delete;
51 diagram &operator=(diagram &&) = default;
52
53 /**
54 * @brief Get the diagram model type - in this case include.
55 *
56 * @return Type of include diagram.
57 */
58 common::model::diagram_t type() const override;
59
60 /**
61 * @brief Search for element in the diagram by fully qualified name.
62 *
63 * @param full_name Fully qualified element name.
64 * @return Optional reference to a diagram element.
65 */
66 opt_ref<diagram_element> get(const std::string &full_name) const override;
67
68 /**
69 * @brief Search for element in the diagram by id.
70 *
71 * @param id Element id.
72 * @return Optional reference to a diagram element.
73 */
74 opt_ref<diagram_element> get(eid_t id) const override;
75
76 /**
77 * @brief Add include diagram element, an include file.
78 *
79 * @param f Include diagram element
80 */
81 void add_file(std::unique_ptr<common::model::source_file> &&f);
82
83 /**
84 * @brief Find an element in the diagram by name.
85 *
86 * This method allows for typed search, where the type of searched for
87 * element is determined from `ElementT`.
88 *
89 * @tparam ElementT Type of element (e.g. source_file)
90 * @param name Fully qualified name of the element
91 * @return Optional reference to a diagram element
92 */
93 template <typename ElementT>
94 opt_ref<ElementT> find(const std::string &name) const;
95
96 /**
97 * @brief Find an element in the diagram by id.
98 *
99 * This method allows for typed search, where the type of searched for
100 * element is determined from `ElementT`.
101 *
102 * @tparam ElementT Type of element (e.g. source_file)
103 * @param id Id of the element
104 * @return Optional reference to a diagram element
105 */
106 template <typename ElementT> opt_ref<ElementT> find(eid_t id) const;
107
108 /**
109 * @brief Get list of references to files in the diagram model.
110 *
111 * @return List of references to concepts in the diagram model.
112 */
114
115 /**
116 * @brief Find diagram element with a specified name and path.
117 *
118 * @param name Name of the element
119 * @param ns Path relative to the diagram
120 * @return Optional reference to diagram element, if found.
121 */
122 opt_ref<diagram_element> get_with_namespace(const std::string &name,
123 const common::model::namespace_ &ns) const override;
124
125 inja::json context() const override;
126
127 /**
128 * @brief Check whether the diagram is empty
129 *
130 * @return True, if diagram is empty
131 */
132 bool is_empty() const override;
133
134 void apply_filter() override;
135};
136
137template <typename ElementT>
138opt_ref<ElementT> diagram::find(const std::string &name) const
139{
140 // Convert the name to the OS preferred path
141 std::filesystem::path namePath{name};
142 namePath.make_preferred();
143
144 for (const auto &element : element_view<ElementT>::view()) {
145 const auto full_name = element.get().full_name(false);
146
147 if (full_name == namePath.string()) {
148 return {element};
149 }
150 }
151
152 return {};
153}
154
155template <typename ElementT> opt_ref<ElementT> diagram::find(eid_t id) const
156{
157 for (const auto &element : element_view<ElementT>::view()) {
158 if (element.get().id() == id) {
159 return {element};
160 }
161 }
162
163 return {};
164}
165
166} // namespace clanguml::include_diagram::model
167
168namespace clanguml::common::model {
169template <>
170bool check_diagram_type<clanguml::include_diagram::model::diagram>(diagram_t t);
171}