0.6.0
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 63 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 objc_interface &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.
 
template<typename T >
void generate_relationships (const T &c, nlohmann::json &parent) const
 Generate all relationships originating at a diagram element.
 
template<>
void generate_relationships (const package &p, nlohmann::json &parent) const
 
template<>
void generate_relationships (const package &p, nlohmann::json &parent) const
 
- 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.
 
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
 
std::optional< std::string > render_link (const common::model::diagram_element &e) const
 
std::optional< std::string > render_link (const common::model::relationship &e) const
 
std::optional< std::string > render_tooltip (const common::model::diagram_element &e) const
 
std::optional< std::string > render_tooltip (const common::model::relationship &e) 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 179 of file class_diagram_generator.cc.

180 : common_generator<diagram_config, diagram_model>{config, model}
181{
182}

Member Function Documentation

◆ generate() [1/5]

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 260 of file class_diagram_generator.cc.

261{
262 nlohmann::json object = c;
263
264 // Perform config dependent postprocessing on generated class
265 if (!config().generate_fully_qualified_name())
266 object["display_name"] =
267 display_name_adapter(c).with_packages().full_name_no_ns();
268
269 object["display_name"] =
270 config().simplify_template_type(object["display_name"]);
271
272 for (auto &tp : object["template_parameters"]) {
273 if (tp.contains("type") && tp.at("type").is_string()) {
274 tp["type"] = config().using_namespace().relative(tp.at("type"));
275 }
276 }
277 for (auto &tp : object["members"]) {
278 if (tp.contains("type") && tp.at("type").is_string()) {
279 tp["type"] = config().using_namespace().relative(tp.at("type"));
280 }
281 }
282
283 parent["elements"].push_back(std::move(object));
284}

◆ generate() [2/5]

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 296 of file class_diagram_generator.cc.

297{
298 nlohmann::json object = c;
299
300 if (!config().generate_fully_qualified_name())
301 object["display_name"] = display_name_adapter(c).full_name_no_ns();
302
303 parent["elements"].push_back(std::move(object));
304}

◆ generate() [3/5]

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 286 of file class_diagram_generator.cc.

287{
288 nlohmann::json object = e;
289
290 if (!config().generate_fully_qualified_name())
291 object["display_name"] = display_name_adapter(e).full_name_no_ns();
292
293 parent["elements"].push_back(std::move(object));
294}

◆ generate() [4/5]

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

Render ObjC interface or protocol element into a JSON node.

Parameters
cenum diagram element
parentJSON node

Definition at line 306 of file class_diagram_generator.cc.

307{
308 nlohmann::json object = c;
309
310 // Perform config dependent postprocessing on generated class
311 if (!config().generate_fully_qualified_name())
312 object["display_name"] = display_name_adapter(c).full_name_no_ns();
313
314 object["display_name"] =
315 config().simplify_template_type(object["display_name"]);
316
317 parent["elements"].push_back(std::move(object));
318}

◆ generate() [5/5]

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 216 of file class_diagram_generator.cc.

217{
218 const auto &uns = config().using_namespace();
219
220 nlohmann::json package_object;
221
222 if (config().generate_packages()) {
223 // Don't generate packages from namespaces filtered out by
224 // using_namespace
225 if (!uns.starts_with({p.full_name(false)})) {
226 LOG_DBG("Generating package {}", p.name());
227
228 package_object["type"] = to_string(config().package_type());
229 package_object["name"] = p.name();
230 package_object["display_name"] =
231 display_name_adapter(p).with_packages().name();
232 }
233 }
234
235 for (const auto &subpackage : p) {
236 if (dynamic_cast<package *>(subpackage.get()) != nullptr) {
237 const auto &sp = dynamic_cast<package &>(*subpackage);
238 if (!sp.is_empty()) {
239 if (config().generate_packages())
240 generate(sp, package_object);
241 else
242 generate(sp, parent);
243 }
244 }
245 else {
246 model().dynamic_apply(subpackage.get(), [&](auto *el) {
247 if (config().generate_packages())
248 generate(*el, package_object);
249 else
250 generate(*el, parent);
251 });
252 }
253 }
254
255 if (config().generate_packages() && !package_object.empty()) {
256 parent["elements"].push_back(std::move(package_object));
257 }
258}

◆ 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 184 of file class_diagram_generator.cc.

185{
186 if (config().using_namespace)
187 parent["using_namespace"] = config().using_namespace().to_string();
188
189 if (config().using_module)
190 parent["using_module"] = config().using_module();
191
192 if (config().generate_packages.has_value)
193 parent["package_type"] = to_string(config().package_type());
194 parent["elements"] = std::vector<nlohmann::json>{};
195 parent["relationships"] = std::vector<nlohmann::json>{};
196
198
200}

◆ generate_relationships() [1/4]

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

Definition at line 334 of file class_diagram_generator.cc.

336{
337 for (const auto &subpackage : p) {
338 if (dynamic_cast<package *>(subpackage.get()) != nullptr) {
339 const auto &sp = dynamic_cast<package &>(*subpackage);
340 if (!sp.is_empty())
341 generate_relationships(sp, parent);
342 }
343 else {
344 model().dynamic_apply(subpackage.get(), [&](auto *el) {
345 if (model().should_include(*el)) {
346 generate_relationships(*el, parent);
347 }
348 });
349 }
350 }
351}

◆ generate_relationships() [2/4]

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

◆ generate_relationships() [3/4]

template<typename T >
void clanguml::class_diagram::generators::json::generator::generate_relationships ( const T &  c,
nlohmann::json &  parent 
) const

Generate all relationships originating at a diagram element.

Template Parameters
TType of diagram element
Parameters
cDiagram diagram element
parentJSON node

Definition at line 149 of file class_diagram_generator.h.

150{
151 const auto &model =
153
154 for (const auto &r : c.relationships()) {
155 auto target_element = model.get(r.destination());
156 if (!target_element.has_value()) {
157 LOG_DBG("Skipping {} relation from '{}' to '{}' due "
158 "to unresolved destination id",
159 to_string(r.type()), c.full_name(true),
160 r.destination().value());
161 continue;
162 }
163
164 nlohmann::json rel = r;
165 rel["source"] = std::to_string(c.id().value());
166 parent["relationships"].push_back(rel);
167 }
168}

◆ generate_relationships() [4/4]

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 320 of file class_diagram_generator.cc.

321{
322 for (const auto &p : model()) {
323 if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
324 generate_relationships(*pkg, parent);
325 }
326 else {
327 model().dynamic_apply(p.get(),
328 [&](auto *el) { generate_relationships(*el, parent); });
329 }
330 }
331}

◆ 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 202 of file class_diagram_generator.cc.

203{
204 for (const auto &p : model()) {
205 if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
206 if (!pkg->is_empty())
207 generate(*pkg, parent);
208 }
209 else {
210 model().dynamic_apply(
211 p.get(), [&](auto *el) { generate(*el, parent); });
212 }
213 }
214}

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