0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
sequence_diagram_generator.h
Go to the documentation of this file.
1/**
2 * @file src/sequence_diagram/generators/json/sequence_diagram_generator.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
21#include "config/config.h"
23#include "util/util.h"
24
25#include <glob/glob.hpp>
26
27#include <filesystem>
28#include <fstream>
29#include <iostream>
30#include <optional>
31#include <sstream>
32
34
36
39
40template <typename C, typename D>
42
43/**
44 * @brief Sequence diagram JSON generator
45 */
46class generator : public common_generator<diagram_config, diagram_model> {
47public:
49
51
52 /**
53 * @brief Main generator method.
54 *
55 * This method is called first and coordinates the entire diagram
56 * generation.
57 *
58 * @param ostr Output stream.
59 */
60 void generate_diagram(nlohmann::json &parent) const override;
61
62 /**
63 * @brief Generate sequence diagram message.
64 *
65 * @param m Message model
66 * @param parent JSON node
67 */
69 nlohmann::json &parent) const;
70
71 /**
72 * @brief Generate sequence diagram participant
73 *
74 * @param parent JSON node
75 * @param id Participant id
76 * @param force If true, generate the participant even if its not in
77 * the set of active participants
78 * @return Id of the generated participant
79 */
80 std::optional<eid_t> generate_participant(
81 nlohmann::json &parent, eid_t id, bool force = false) const;
82
83 /**
84 * @brief Generate sequence diagram participant by name
85 *
86 * This is convienience wrapper over `generate_participant()` by id.
87 *
88 * @param parent JSON node
89 * @param name Full participant name
90 */
92 nlohmann::json &parent, const std::string &name) const;
93
94 /**
95 * @brief Generate sequence diagram activity.
96 *
97 * @param a Activity model
98 * @param visited List of already visited participants, this is necessary
99 * for breaking infinite recursion on recursive calls
100 */
102 std::vector<eid_t> &visited) const;
103
104 /**
105 * @brief Get reference to the current block statement.
106 *
107 * This method returns a reference to the last block statement (e.g if
108 * statement or for loop) in the call stack.
109 *
110 * @return Reference to the current block statement.
111 */
112 nlohmann::json &current_block_statement() const;
113
114 std::string make_display_name(const std::string &full_name) const;
115
116private:
117 /**
118 * @brief Check if specified participant has already been generated.
119 *
120 * @param id Participant id.
121 * @return True, if participant has already been generated.
122 */
123 bool is_participant_generated(eid_t id) const;
124
125 /**
126 * @brief Process call message
127 *
128 * @param m Message model
129 * @param visited List of already visited participants
130 */
132 const model::message &m, std::vector<eid_t> &visited) const;
133
134 /**
135 * @brief Process `if` statement message
136 *
137 * @param m Message model
138 */
139 void process_if_message(const model::message &m) const;
140
141 /**
142 * @brief Process `else if` statement message
143 */
144 void process_else_if_message() const;
145
146 /**
147 * @brief Process `end if` statement message
148 */
149 void process_end_if_message() const;
150
151 /**
152 * @brief Process `:?` statement message
153 *
154 * @param m Message model
155 */
156 void process_conditional_message(const model::message &m) const;
157
158 /**
159 * @brief Process end of conditional statement message
160 */
162
163 /**
164 * @brief Process conditional else statement message
165 *
166 * @param m Message model
167 */
169
170 /**
171 * @brief Process `switch` statement message
172 *
173 * @param m Message model
174 */
175 void process_switch_message(const model::message &m) const;
176
177 /**
178 * @brief Process switch end statement message
179 */
180 void process_end_switch_message() const;
181
182 /**
183 * @brief Process `switch` `case` statement message
184 *
185 * @param m Message model
186 */
187 void process_case_message(const model::message &m) const;
188
189 /**
190 * @brief Process `try` statement message
191 *
192 * @param m Message model
193 */
194 void process_try_message(const model::message &m) const;
195
196 /**
197 * @brief Process `try` end statement message
198 */
199 void process_end_try_message() const;
200
201 /**
202 * @brief Process `catch` statement message
203 */
204 void process_catch_message() const;
205
206 /**
207 * @brief Process `do` loop statement message
208 *
209 * @param m Message model
210 */
211 void process_do_message(const model::message &m) const;
212
213 /**
214 * @brief Process `do` end statement message
215 */
216 void process_end_do_message() const;
217
218 /**
219 * @brief Process `for` loop statement message
220 *
221 * @param m Message model
222 */
223 void process_for_message(const model::message &m) const;
224
225 /**
226 * @brief Process `for` end statement message
227 */
228 void process_end_for_message() const;
229
230 /**
231 * @brief Process `while` loop message
232 *
233 * @param m Message model
234 */
235 void process_while_message(const model::message &m) const;
236
237 /**
238 * @brief Process `while` end loop message
239 */
240 void process_end_while_message() const;
241
242 void generate_from_to_sequences(nlohmann::json &parent) const;
243
244 void generate_to_sequences(nlohmann::json &parent) const;
245
246 void generate_from_sequences(nlohmann::json &parent) const;
247
248 std::vector<eid_t> find_from_activities() const;
249
250 mutable std::set<eid_t> generated_participants_;
251
252 // Needed to add "participants" array in a temporary object accessible from
253 // all methods of the generator
254 mutable nlohmann::json json_;
255
256 mutable std::vector<std::reference_wrapper<nlohmann::json>>
258
259 mutable std::vector<model::message> already_generated_in_static_context_;
260};
261
262} // namespace clanguml::sequence_diagram::generators::json