0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
package_diagram_generator.cc
Go to the documentation of this file.
1/**
2 * @file src/package_diagram/generators/json/package_diagram_generator.cc
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
20
22#include "util/error.h"
23
25
28{
29}
30
32 const package &p, nlohmann::json &parent) const
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}
57
58void generator::generate(const package &p, nlohmann::json &parent) const
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}
104
105void generator::generate_diagram(nlohmann::json &parent) const
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}
128
129} // namespace clanguml::package_diagram::generators::json