0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
class_diagram_generator.h
Go to the documentation of this file.
1/**
2 * @file src/class_diagram/generators/plantuml/class_diagram_generator.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
28#include "config/config.h"
29#include "util/util.h"
30
31#include <glob/glob.hpp>
32
33#include <filesystem>
34#include <fstream>
35#include <iostream>
36#include <sstream>
37
38namespace clanguml {
39namespace class_diagram {
40namespace generators {
41namespace plantuml {
42
45template <typename C, typename D>
48
60
61using namespace clanguml::util;
62
63/**
64 * @brief Class diagram PlantUML generator
65 */
66class generator : public common_generator<diagram_config, diagram_model> {
67 using method_groups_t = std::map<std::string, std::vector<class_method>>;
68
69public:
71
73
74 /**
75 * @brief Main generator method.
76 *
77 * This method is called first and coordinates the entire diagram
78 * generation.
79 *
80 * @param ostr Output stream.
81 */
82 void generate_diagram(std::ostream &ostr) const override;
83
84 /**
85 * @brief In a nested diagram, generate the top level elements.
86 *
87 * This method iterates over the top level elements. In case the diagram
88 * is nested (i.e. includes packages), for each package it recursively
89 * call generation of elements contained in each package.
90 *
91 * @param parent JSON node
92 */
93 void generate_top_level_elements(std::ostream &ostr) const;
94
95 /**
96 * @brief Generate a hyperlink for a class element.
97 *
98 * @param ostr Output stream
99 * @param e Class element (e.g. a method)
100 */
101 void generate_link(std::ostream &ostr, const class_element &e) const;
102
103 /**
104 * @brief Generate PlantUML alias for a class element.
105 *
106 * @param c Class element
107 * @param ostr Output stream
108 */
109 void generate_alias(const class_ &c, std::ostream &ostr) const;
110
111 /**
112 * @brief Generate PlantUML alias for a enum element.
113 *
114 * @param e Enum element
115 * @param ostr Output stream
116 */
117 void generate_alias(const enum_ &e, std::ostream &ostr) const;
118
119 /**
120 * @brief Generate PlantUML alias for a concept element.
121 *
122 * @param c Concept element
123 * @param ostr Output stream
124 */
125 void generate_alias(const concept_ &c, std::ostream &ostr) const;
126
127 /**
128 * @brief Render class element to PlantUML
129 *
130 * @param c Class element
131 * @param ostr Output stream
132 */
133 void generate(const class_ &c, std::ostream &ostr) const;
134
135 /**
136 * @brief Render class methods to PlantUML
137 *
138 * @param methods List of class methods
139 * @param ostr Output stream
140 */
141 void generate_methods(
142 const std::vector<class_method> &methods, std::ostream &ostr) const;
143
144 /**
145 * @brief Render class methods to PlantUML in groups
146 *
147 * @param methods Methods grouped by method type
148 * @param ostr Output stream
149 */
150 void generate_methods(
151 const method_groups_t &methods, std::ostream &ostr) const;
152
153 /**
154 * @brief Render class method to PlantUML
155 *
156 * @param m Class method
157 * @param ostr Output stream
158 */
159 void generate_method(const class_method &m, std::ostream &ostr) const;
160
161 /**
162 * @brief Render class member to PlantUML
163 *
164 * @param m Class member
165 * @param ostr Output stream
166 */
167 void generate_member(const class_member &m, std::ostream &ostr) const;
168
169 /**
170 * @brief Render all relationships in the diagram to PlantUML
171 *
172 * @param ostr Output stream
173 */
174 void generate_relationships(std::ostream &ostr) const;
175
176 /**
177 * @brief Render all relationships originating from class element.
178 *
179 * @param c Class element
180 * @param ostr Output stream
181 */
182 void generate_relationships(const class_ &c, std::ostream &ostr) const;
183
184 /**
185 * @brief Render a specific relationship to PlantUML.
186 *
187 * @param r Relationship model
188 * @param rendered_relations Set of already rendered relationships, to
189 * ensure that there are no duplicate
190 * relationships
191 */
193 const relationship &r, std::set<std::string> &rendered_relations) const;
194
195 /**
196 * @brief Render enum element to PlantUML
197 *
198 * @param e Enum element
199 * @param ostr Output stream
200 */
201 void generate(const enum_ &e, std::ostream &ostr) const;
202
203 /**
204 * @brief Render all relationships originating from enum element.
205 *
206 * @param c Enum element
207 * @param ostr Output stream
208 */
209 void generate_relationships(const enum_ &c, std::ostream &ostr) const;
210
211 /**
212 * @brief Render concept element to PlantUML
213 *
214 * @param c Concept element
215 * @param ostr Output stream
216 */
217 void generate(const concept_ &c, std::ostream &ostr) const;
218
219 /**
220 * @brief Render all relationships originating from concept element.
221 *
222 * @param c Concept element
223 * @param ostr Output stream
224 */
225 void generate_relationships(const concept_ &c, std::ostream &ostr) const;
226
227 /**
228 * @brief Render package element to PlantUML
229 *
230 * @param p Package element
231 * @param ostr Output stream
232 */
233 void generate(const package &p, std::ostream &ostr) const;
234
235 /**
236 * @brief Render all relationships originating from package element.
237 *
238 * @param p Package element
239 * @param ostr Output stream
240 */
241 void generate_relationships(const package &p, std::ostream &ostr) const;
242
243 /**
244 * @brief Generate any notes attached specifically to some class element.
245 *
246 * @param ostream Output stream
247 * @param member Class element (member or method)
248 * @param alias PlantUML class alias
249 */
250 void generate_member_notes(std::ostream &ostream,
251 const class_element &member, const std::string &alias) const;
252
253 /**
254 * @brief Generate elements grouped together in `together` groups.
255 *
256 * @param ostr Output stream
257 */
258 void generate_groups(std::ostream &ostr) const;
259
260 /**
261 * @brief Group class methods based on method type.
262 *
263 * @param methods List of class methods.
264 *
265 * @return Map of method groups.
266 */
268 const std::vector<class_method> &methods) const;
269
270private:
271 const std::vector<std::string> method_groups_{
272 "constructors", "assignment", "operators", "other"};
273
274 std::string render_name(std::string name) const;
275
276 template <typename T>
277 void sort_class_elements(std::vector<T> &elements) const
278 {
279 if (config().member_order() == config::member_order_t::lexical) {
280 std::sort(elements.begin(), elements.end(),
281 [](const auto &m1, const auto &m2) {
282 return m1.name() < m2.name();
283 });
284 }
285 }
286
289};
290
291} // namespace plantuml
292} // namespace generators
293} // namespace class_diagram
294} // namespace clanguml