0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | List of all members
clanguml::class_diagram::generators::json::generator Class Reference

Class diagram JSON generator. More...

Detailed Description

Class diagram JSON generator.

Definition at line 62 of file class_diagram_generator.h.

#include <class_diagram_generator.h>

Public Member Functions

 generator (diagram_config &config, diagram_model &model)
 
void generate_diagram (nlohmann::json &parent) const override
 Main generator method.
 
void generate (const class_ &c, nlohmann::json &parent) const
 
void generate (const enum_ &c, nlohmann::json &parent) const
 
void generate (const concept_ &c, nlohmann::json &parent) const
 
void generate (const package &p, nlohmann::json &parent) const
 
void generate_top_level_elements (nlohmann::json &parent) const
 In a nested diagram, generate the top level elements.
 
void generate_relationships (nlohmann::json &parent) const
 Generate all relationships in the diagram.
 
void generate_relationships (const class_ &c, nlohmann::json &parent) const
 Generate all relationships originating at a class element.
 
void generate_relationships (const enum_ &c, nlohmann::json &parent) const
 Generate all relationships originating at an enum element.
 
void generate_relationships (const concept_ &c, nlohmann::json &parent) const
 Generate all relationships originating at a concept element.
 
void generate_relationships (const package &p, nlohmann::json &parent) const
 Generate all relationships in a package.
 
- Public Member Functions inherited from clanguml::common::generators::json::generator< ConfigType, DiagramType >
 ~generator () override=default
 
void generate (std::ostream &ostr) const override
 Generate diagram.
 
virtual void generate_diagram (nlohmann::json &parent) const =0
 Generate diagram model.
 
void generate_metadata (nlohmann::json &parent) const
 Generate metadata element with diagram metadata.
 
- Public Member Functions inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
 generator (ConfigType &config, DiagramType &model)
 Constructor.
 
virtual ~generator ()=default
 
virtual void generate (std::ostream &ostr) const =0
 Generate diagram.
 
const ConfigType & config () const
 Get reference to diagram config.
 
const DiagramType & model () const
 Get reference to diagram model.
 
template<typename E >
inja::json element_context (const E &e) const
 
std::optional< std::pair< std::string, std::string > > get_link_pattern (const common::model::source_location &sl) const
 
std::optional< std::pair< std::string, std::string > > get_tooltip_pattern (const common::model::source_location &sl) const
 
void init_context ()
 Initialize diagram Jinja context.
 
void update_context () const
 Update diagram Jinja context.
 
void init_env ()
 
const inja::json & context () const
 
inja::Environment & env () const
 

Additional Inherited Members

- Protected Attributes inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
inja::json m_context
 
inja::Environment m_env
 

Constructor & Destructor Documentation

◆ generator()

clanguml::class_diagram::generators::json::generator::generator ( diagram_config config,
diagram_model model 
)

Definition at line 134 of file class_diagram_generator.cc.

135 : common_generator<diagram_config, diagram_model>{config, model}
136{
137}

Member Function Documentation

◆ generate() [1/4]

void clanguml::class_diagram::generators::json::generator::generate ( const class_ c,
nlohmann::json &  parent 
) const

Render class element into a JSON node.

Parameters
cclass diagram element
parentJSON node

Definition at line 229 of file class_diagram_generator.cc.

230{
231 nlohmann::json object = c;
232
233 // Perform config dependent postprocessing on generated class
234 if (!config().generate_fully_qualified_name())
235 object["display_name"] =
236 common::generators::json::render_name(c.full_name_no_ns());
237
238 object["display_name"] =
239 config().simplify_template_type(object["display_name"]);
240
241 for (auto &tp : object["template_parameters"]) {
242 if (tp.contains("type") && tp.at("type").is_string()) {
243 tp["type"] = config().using_namespace().relative(tp.at("type"));
244 }
245 }
246 for (auto &tp : object["members"]) {
247 if (tp.contains("type") && tp.at("type").is_string()) {
248 tp["type"] = config().using_namespace().relative(tp.at("type"));
249 }
250 }
251
252 std::string object_str = object.dump(2);
253
254 parent["elements"].push_back(std::move(object));
255}

◆ generate() [2/4]

void clanguml::class_diagram::generators::json::generator::generate ( const concept_ c,
nlohmann::json &  parent 
) const

Render concept element into a JSON node.

Parameters
cconcept diagram element
parentJSON node

Definition at line 268 of file class_diagram_generator.cc.

269{
270 nlohmann::json object = c;
271
272 if (!config().generate_fully_qualified_name())
273 object["display_name"] =
274 common::generators::json::render_name(c.full_name_no_ns());
275
276 parent["elements"].push_back(std::move(object));
277}

◆ generate() [3/4]

void clanguml::class_diagram::generators::json::generator::generate ( const enum_ c,
nlohmann::json &  parent 
) const

Render enum element into a JSON node.

Parameters
cenum diagram element
parentJSON node

Definition at line 257 of file class_diagram_generator.cc.

258{
259 nlohmann::json object = e;
260
261 if (!config().generate_fully_qualified_name())
262 object["display_name"] =
263 common::generators::json::render_name(e.full_name_no_ns());
264
265 parent["elements"].push_back(std::move(object));
266}

◆ generate() [4/4]

void clanguml::class_diagram::generators::json::generator::generate ( const package p,
nlohmann::json &  parent 
) const

Render package element into a JSON node.

Parameters
ppackage diagram element
parentJSON node

Definition at line 176 of file class_diagram_generator.cc.

177{
178 const auto &uns = config().using_namespace();
179
180 nlohmann::json package_object;
181
182 if (config().generate_packages()) {
183 // Don't generate packages from namespaces filtered out by
184 // using_namespace
185 if (!uns.starts_with({p.full_name(false)})) {
186 LOG_DBG("Generating package {}", p.name());
187
188 package_object["type"] = to_string(config().package_type());
189 package_object["name"] = p.name();
190 package_object["display_name"] = p.name();
191 }
192 }
193
194 for (const auto &subpackage : p) {
195 if (dynamic_cast<package *>(subpackage.get()) != nullptr) {
196 const auto &sp = dynamic_cast<package &>(*subpackage);
197 if (!sp.is_empty()) {
198 if (config().generate_packages())
199 generate(sp, package_object);
200 else
201 generate(sp, parent);
202 }
203 }
204 else if (auto *cls = dynamic_cast<class_ *>(subpackage.get()); cls) {
205 if (config().generate_packages())
206 generate(*cls, package_object);
207 else
208 generate(*cls, parent);
209 }
210 else if (auto *enm = dynamic_cast<enum_ *>(subpackage.get()); enm) {
211 if (config().generate_packages())
212 generate(*enm, package_object);
213 else
214 generate(*enm, parent);
215 }
216 else if (auto *cpt = dynamic_cast<concept_ *>(subpackage.get()); cpt) {
217 if (config().generate_packages())
218 generate(*cpt, package_object);
219 else
220 generate(*cpt, parent);
221 }
222 }
223
224 if (config().generate_packages() && !package_object.empty()) {
225 parent["elements"].push_back(std::move(package_object));
226 }
227}

◆ generate_diagram()

void clanguml::class_diagram::generators::json::generator::generate_diagram ( nlohmann::json &  parent) const
overridevirtual

Main generator method.

This method is called first and coordinates the entire diagram generation.

Parameters
ostrOutput stream.

Implements clanguml::common::generators::json::generator< ConfigType, DiagramType >.

Definition at line 139 of file class_diagram_generator.cc.

140{
141 if (config().using_namespace)
142 parent["using_namespace"] = config().using_namespace().to_string();
143
144 if (config().using_module)
145 parent["using_module"] = config().using_module();
146
147 if (config().generate_packages.has_value)
148 parent["package_type"] = to_string(config().package_type());
149 parent["elements"] = std::vector<nlohmann::json>{};
150 parent["relationships"] = std::vector<nlohmann::json>{};
151
153
155}

◆ generate_relationships() [1/5]

void clanguml::class_diagram::generators::json::generator::generate_relationships ( const class_ c,
nlohmann::json &  parent 
) const

Generate all relationships originating at a class element.

Parameters
cClass diagram element
parentJSON node

Definition at line 297 of file class_diagram_generator.cc.

299{
300 for (const auto &r : c.relationships()) {
301 auto target_element = model().get(r.destination());
302 if (!target_element.has_value()) {
303 LOG_DBG("Skipping {} relation from {} to {} due "
304 "to unresolved destination id",
305 to_string(r.type()), c.full_name(), r.destination());
306 continue;
307 }
308
309 nlohmann::json rel = r;
310 rel["source"] = std::to_string(c.id().value());
311 parent["relationships"].push_back(rel);
312 }
313
314 if (model().should_include(relationship_t::kExtension)) {
315 for (const auto &b : c.parents()) {
316 common::model::relationship r(
317 relationship_t::kExtension, b.id(), b.access());
318 nlohmann::json rel = r;
319 rel["source"] = std::to_string(c.id().value());
320 parent["relationships"].push_back(rel);
321 }
322 }
323}

◆ generate_relationships() [2/5]

void clanguml::class_diagram::generators::json::generator::generate_relationships ( const concept_ c,
nlohmann::json &  parent 
) const

Generate all relationships originating at a concept element.

Parameters
cConcept diagram element
parentJSON node

Definition at line 343 of file class_diagram_generator.cc.

345{
346 for (const auto &r : c.relationships()) {
347 auto target_element = model().get(r.destination());
348 if (!target_element.has_value()) {
349 LOG_DBG("Skipping {} relation from {} to {} due "
350 "to unresolved destination id",
351 to_string(r.type()), c.full_name(), r.destination());
352 continue;
353 }
354
355 nlohmann::json rel = r;
356 rel["source"] = std::to_string(c.id().value());
357 parent["relationships"].push_back(rel);
358 }
359}

◆ generate_relationships() [3/5]

void clanguml::class_diagram::generators::json::generator::generate_relationships ( const enum_ c,
nlohmann::json &  parent 
) const

Generate all relationships originating at an enum element.

Parameters
cEnum diagram element
parentJSON node

Definition at line 325 of file class_diagram_generator.cc.

327{
328 for (const auto &r : c.relationships()) {
329 auto target_element = model().get(r.destination());
330 if (!target_element.has_value()) {
331 LOG_DBG("Skipping {} relation from {} to {} due "
332 "to unresolved destination id",
333 to_string(r.type()), c.full_name(), r.destination());
334 continue;
335 }
336
337 nlohmann::json rel = r;
338 rel["source"] = std::to_string(c.id().value());
339 parent["relationships"].push_back(rel);
340 }
341}

◆ generate_relationships() [4/5]

void clanguml::class_diagram::generators::json::generator::generate_relationships ( const package p,
nlohmann::json &  parent 
) const

Generate all relationships in a package.

If the diagram is nested, it recursively calls relationship generation for all subelements.

Parameters
pPackage diagram element
parentJSON node

Definition at line 361 of file class_diagram_generator.cc.

363{
364 for (const auto &subpackage : p) {
365 if (dynamic_cast<package *>(subpackage.get()) != nullptr) {
366 const auto &sp = dynamic_cast<package &>(*subpackage);
367 if (!sp.is_empty())
368 generate_relationships(sp, parent);
369 }
370 else if (dynamic_cast<class_ *>(subpackage.get()) != nullptr) {
371 if (model().should_include(*subpackage)) {
373 dynamic_cast<class_ &>(*subpackage), parent);
374 }
375 }
376 else if (dynamic_cast<enum_ *>(subpackage.get()) != nullptr) {
377 if (model().should_include(*subpackage)) {
379 dynamic_cast<enum_ &>(*subpackage), parent);
380 }
381 }
382 else if (dynamic_cast<concept_ *>(subpackage.get()) != nullptr) {
383 if (model().should_include(*subpackage)) {
385 dynamic_cast<concept_ &>(*subpackage), parent);
386 }
387 }
388 }
389}

◆ generate_relationships() [5/5]

void clanguml::class_diagram::generators::json::generator::generate_relationships ( nlohmann::json &  parent) const

Generate all relationships in the diagram.

Parameters
parentJSON node

Definition at line 279 of file class_diagram_generator.cc.

280{
281 for (const auto &p : model()) {
282 if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
283 generate_relationships(*pkg, parent);
284 }
285 else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
286 generate_relationships(*cls, parent);
287 }
288 else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
289 generate_relationships(*enm, parent);
290 }
291 else if (auto *cpt = dynamic_cast<concept_ *>(p.get()); cpt) {
292 generate_relationships(*cpt, parent);
293 }
294 }
295}

◆ generate_top_level_elements()

void clanguml::class_diagram::generators::json::generator::generate_top_level_elements ( nlohmann::json &  parent) const

In a nested diagram, generate the top level elements.

This method iterates over the top level elements. In case the diagram is nested (i.e. includes packages), for each package it recursively call generation of elements contained in each package.

Parameters
parentJSON node

Definition at line 157 of file class_diagram_generator.cc.

158{
159 for (const auto &p : model()) {
160 if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
161 if (!pkg->is_empty())
162 generate(*pkg, parent);
163 }
164 else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
165 generate(*cls, parent);
166 }
167 else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
168 generate(*enm, parent);
169 }
170 else if (auto *cpt = dynamic_cast<concept_ *>(p.get()); cpt) {
171 generate(*cpt, parent);
172 }
173 }
174}

The documentation for this class was generated from the following files: