0.5.4
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-2024 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
21#include "util/error.h"
22
24
27{
28}
29
31 const package &p, nlohmann::json &parent) const
32{
33 LOG_DBG("Generating relationships for package {}", p.full_name(true));
34
35 // Generate this packages relationship
36 if (model().should_include(relationship_t::kDependency)) {
37 for (const auto &r : p.relationships()) {
38 nlohmann::json rel = r;
39
40 auto destination_package = model().get(r.destination());
41
42 if (!destination_package)
43 continue;
44
45 rel["source"] = std::to_string(p.id().value());
46 parent["relationships"].push_back(std::move(rel));
47 }
48 }
49
50 // Process it's subpackages relationships
51 for (const auto &subpackage : p) {
53 dynamic_cast<const package &>(*subpackage), parent);
54 }
55}
56
57void generator::generate(const package &p, nlohmann::json &parent) const
58{
59 LOG_DBG("Generating package {}", p.full_name(false));
60
61 const auto &uns = config().using_namespace();
62 if (!uns.starts_with({p.full_name(false)})) {
63 nlohmann::json j;
64 j["id"] = std::to_string(p.id().value());
65 j["name"] = p.name();
66 j["type"] = to_string(config().package_type());
67 j["display_name"] = p.name();
68 switch (config().package_type()) {
70 j["namespace"] = p.get_namespace().to_string();
71 break;
73 j["namespace"] = p.get_namespace().to_string();
74 break;
76 j["path"] = p.get_namespace().to_string();
77 break;
78 }
79
80 j["is_deprecated"] = p.is_deprecated();
81 if (!p.file().empty())
82 j["source_location"] =
83 dynamic_cast<const common::model::source_location &>(p);
84 if (const auto &comment = p.comment(); comment)
85 j["comment"] = comment.value();
86
87 for (const auto &subpackage : p) {
88 auto &pkg = dynamic_cast<package &>(*subpackage);
89 generate(pkg, j);
90 }
91
92 parent["elements"].push_back(std::move(j));
93 }
94 else {
95 for (const auto &subpackage : p) {
96 auto &pkg = dynamic_cast<package &>(*subpackage);
97 generate(pkg, parent);
98 }
99 }
100}
101
102void generator::generate_diagram(nlohmann::json &parent) const
103{
104 if (config().using_namespace)
105 parent["using_namespace"] = config().using_namespace().to_string();
106 if (config().using_module)
107 parent["using_module"] = config().using_module();
108
109 parent["name"] = model().name();
110 parent["diagram_type"] = "package";
111 parent["package_type"] = to_string(config().package_type());
112 parent["elements"] = std::vector<nlohmann::json>{};
113 parent["relationships"] = std::vector<nlohmann::json>{};
114
115 for (const auto &p : model()) {
116 auto &pkg = dynamic_cast<package &>(*p);
117 generate(pkg, parent);
118 }
119
120 // Process package relationships
121 for (const auto &p : model()) {
122 generate_relationships(dynamic_cast<package &>(*p), parent);
123 }
124}
125
126} // namespace clanguml::package_diagram::generators::json