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/common/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
20#include "diagram_element.h"
21#include "enums.h"
22#include "namespace.h"
23#include "source_file.h"
24
25#include <memory>
26#include <string>
27
29
30class diagram_filter;
31class element;
32class relationship;
33
34/**
35 * @brief Base class for all diagram models
36 *
37 * @embed{diagram_hierarchy_class.svg}
38 */
39class diagram {
40public:
42
43 diagram(const diagram &) = delete;
44 diagram(diagram && /*unused*/) noexcept;
45 diagram &operator=(const diagram &) = delete;
46 diagram &operator=(diagram && /*unused*/) noexcept;
47
48 virtual ~diagram();
49
50 /**
51 * @brief Return type of the diagram.
52 *
53 * @return Type of diagram
54 */
55 virtual diagram_t type() const = 0;
56
57 /**
58 * Return optional reference to a diagram_element by name.
59 *
60 * @param full_name Fully qualified name of a diagram element.
61 * @return Optional reference to a diagram element.
62 */
63 virtual opt_ref<clanguml::common::model::diagram_element> get(
64 const std::string &full_name) const = 0;
65
66 /**
67 * Return optional reference to a diagram_element by id.
68 *
69 * @param id Id of a diagram element.
70 * @return Optional reference to a diagram element.
71 */
72 virtual common::optional_ref<clanguml::common::model::diagram_element> get(
73 eid_t id) const = 0;
74
75 /**
76 * Return optional reference to a diagram_element by name and namespace.
77 *
78 * @param name Name of the diagram element (e.g. a class name)
79 * @param ns Namespace of the element.
80 * @return Optional reference to a diagram element.
81 */
82 virtual common::optional_ref<clanguml::common::model::diagram_element>
83 get_with_namespace(const std::string &name, const namespace_ &ns) const;
84
85 /**
86 * Set diagram name.
87 *
88 * @param name Name of the diagram.
89 */
90 void set_name(const std::string &name);
91
92 /**
93 * Return the name of the diagram.
94 *
95 * @return Name of the diagram.
96 */
97 std::string name() const;
98
99 /**
100 * Set diagram filter for this diagram.
101 *
102 * @param filter diagram_filter instance
103 *
104 * @see clanguml::common::model::diagram_filter
105 */
106 void set_filter(std::unique_ptr<diagram_filter> filter);
107
108 /**
109 * Get diagram filter
110 *
111 * @return Reference to the diagrams element filter
112 */
113 const diagram_filter &filter() const { return *filter_; }
114
115 /**
116 * @brief Set diagram in a complete state.
117 *
118 * This must be called after the diagram's 'translation_unit_visitor' has
119 * completed for all translation units, in order to apply filters which can
120 * only work after the diagram is complete.
121 *
122 * @param complete Status of diagram visitor completion.
123 */
124 void set_complete(bool complete);
125
126 /**
127 * @brief Whether the diagram is complete.
128 *
129 * This flag is set to true, when all translation units for this diagram
130 * have been visited.
131 *
132 * @return Diagram completion status.
133 */
134 bool complete() const;
135
136 /**
137 * @brief Once the diagram is complete, run any final processing.
138 *
139 * This method should be overriden by specific diagram models to do some
140 * final tasks like cleaning up the model (e.g. some filters only work
141 * on completed diagrams).
142 */
143 virtual void finalize();
144
145 // TODO: refactor to a template method
146 bool should_include(const element &e) const;
147 bool should_include(const namespace_ &ns) const;
148 bool should_include(const source_file &path) const;
149 bool should_include(relationship r) const;
150 bool should_include(relationship_t r) const;
151 bool should_include(access_t s) const;
152 // Disallow std::string overload
153 bool should_include(const std::string &s) const = delete;
154
155 virtual bool has_element(const eid_t /*id*/) const { return false; }
156
157 virtual bool should_include(
158 const namespace_ &ns, const std::string &name) const;
159
160 /**
161 * Return diagrams JSON context for inja templates.
162 *
163 * @return JSON context.
164 */
165 virtual inja::json context() const = 0;
166
167 /**
168 * @brief Check whether the diagram is empty
169 *
170 * @return True, if diagram is empty
171 */
172 virtual bool is_empty() const = 0;
173
174 virtual void apply_filter() { }
175
176private:
177 std::string name_;
178 std::unique_ptr<diagram_filter> filter_;
179 bool complete_{false};
180 bool filtered_{false};
181};
182
183template <typename DiagramT> bool check_diagram_type(diagram_t t);
184} // namespace clanguml::common::model