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

Package diagram JSON generator. More...

Detailed Description

Package diagram JSON generator.

Definition at line 50 of file package_diagram_generator.h.

#include <package_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_relationships (const package &p, nlohmann::json &parent) const
 Generate relationships originating from package p
 
void generate (const package &p, nlohmann::json &parent) const
 Generate diagram 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.
 
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::package_diagram::generators::json::generator::generator ( diagram_config config,
diagram_model model 
)

Definition at line 26 of file package_diagram_generator.cc.

27 : common_generator<diagram_config, diagram_model>{config, model}
28{
29}

Member Function Documentation

◆ generate()

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

Generate diagram package.

Parameters
pDiagram package element
parentParent JSON node

Definition at line 58 of file package_diagram_generator.cc.

59{
61
62 LOG_DBG("Generating package {}", p.full_name(false));
63
64 const auto &uns = config().using_namespace();
65 if (!uns.starts_with({p.full_name(false)})) {
66 nlohmann::json j;
67 j["id"] = std::to_string(p.id().value());
68 j["name"] = p.name();
69 j["type"] = to_string(config().package_type());
70 j["display_name"] = display_name_adapter(p).with_packages().name();
71 switch (config().package_type()) {
73 j["namespace"] = p.get_namespace().to_string();
74 break;
76 j["namespace"] = p.get_namespace().to_string();
77 break;
79 j["path"] = p.get_namespace().to_string();
80 break;
81 }
82
83 j["is_deprecated"] = p.is_deprecated();
84 if (!p.file().empty())
85 j["source_location"] =
86 dynamic_cast<const common::model::source_location &>(p);
87 if (const auto &comment = p.comment(); comment)
88 j["comment"] = comment.value();
89
90 for (const auto &subpackage : p) {
91 auto &pkg = dynamic_cast<package &>(*subpackage);
92 generate(pkg, j);
93 }
94
95 parent["elements"].push_back(std::move(j));
96 }
97 else {
98 for (const auto &subpackage : p) {
99 auto &pkg = dynamic_cast<package &>(*subpackage);
100 generate(pkg, parent);
101 }
102 }
103}

◆ generate_diagram()

void clanguml::package_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 105 of file package_diagram_generator.cc.

106{
107 if (config().using_namespace)
108 parent["using_namespace"] = config().using_namespace().to_string();
109 if (config().using_module)
110 parent["using_module"] = config().using_module();
111
112 parent["name"] = model().name();
113 parent["diagram_type"] = "package";
114 parent["package_type"] = to_string(config().package_type());
115 parent["elements"] = std::vector<nlohmann::json>{};
116 parent["relationships"] = std::vector<nlohmann::json>{};
117
118 for (const auto &p : model()) {
119 auto &pkg = dynamic_cast<package &>(*p);
120 generate(pkg, parent);
121 }
122
123 // Process package relationships
124 for (const auto &p : model()) {
125 generate_relationships(dynamic_cast<package &>(*p), parent);
126 }
127}

◆ generate_relationships()

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

Generate relationships originating from package p

Parameters
pDiagram element
parentJSON node

Definition at line 31 of file package_diagram_generator.cc.

33{
34 LOG_DBG("Generating relationships for package {}", p.full_name(true));
35
36 // Generate this packages relationship
37 if (model().should_include(relationship_t::kDependency)) {
38 for (const auto &r : p.relationships()) {
39 nlohmann::json rel = r;
40
41 auto destination_package = model().get(r.destination());
42
43 if (!destination_package)
44 continue;
45
46 rel["source"] = std::to_string(p.id().value());
47 parent["relationships"].push_back(std::move(rel));
48 }
49 }
50
51 // Process it's subpackages relationships
52 for (const auto &subpackage : p) {
54 dynamic_cast<const package &>(*subpackage), parent);
55 }
56}

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