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