0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
include_diagram_generator.cc
Go to the documentation of this file.
1/**
2 * @file src/include_diagram/generators/json/include_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
21#include "util/error.h"
22
24
27{
28}
29
31 const source_file &f, nlohmann::json &parent) const
32{
33 LOG_DBG("Generating relationships for file {}", f.full_name(true));
34
36 util::for_each(f, [this, &parent](const auto &file) {
38 dynamic_cast<const source_file &>(*file), parent);
39 });
40 }
41 else {
42 for (const auto &r : f.relationships()) {
43 nlohmann::json rel = r;
44 rel["source"] = std::to_string(f.id().value());
45 parent["relationships"].push_back(std::move(rel));
46 }
47 }
48}
49
50void generator::generate(const source_file &f, nlohmann::json &parent) const
51{
52 if (config().generate_packages && !config().generate_packages()) {
54 }
55 else {
56 generate_with_packages(f, parent);
57 }
58}
59
61 const source_file &f, nlohmann::json &parent) const
62{
63 nlohmann::json j;
64 j["id"] = std::to_string(f.id().value());
65 j["name"] = f.name();
66 auto display_name = f.full_name(false);
67#if defined(_MSC_VER)
68 util::replace_all(display_name, "\\", "/");
69#endif
70 j["display_name"] = std::move(display_name);
71
73 LOG_DBG("Generating directory {}", f.name());
74
75 j["type"] = "folder";
76
77 util::for_each(f, [this, &j](const auto &file) {
78 generate(dynamic_cast<const source_file &>(*file), j);
79 });
80
81 parent["elements"].push_back(std::move(j));
82 }
83 else {
84 LOG_DBG("Generating file {}", f.name());
85
86 j["type"] = "file";
87 j["file_kind"] = to_string(f.type());
89 j["is_system"] = f.is_system_header();
90 }
91
92 parent["elements"].push_back(std::move(j));
93 }
94}
95
97 const source_file &f, nlohmann::json &parent) const
98{
99 nlohmann::json j;
100 j["id"] = std::to_string(f.id().value());
101 j["name"] = f.file_relative();
102 auto display_name = f.full_name(false);
103#if defined(_MSC_VER)
104 util::replace_all(display_name, "\\", "/");
105#endif
106 j["display_name"] = std::move(display_name);
107
109 util::for_each(f, [this, &parent](const auto &file) {
110 generate(dynamic_cast<const source_file &>(*file), parent);
111 });
112 }
113 else {
114 LOG_DBG("Generating file {}", f.file_relative());
115
116 j["type"] = "file";
117 j["file_kind"] = to_string(f.type());
119 j["is_system"] = f.is_system_header();
120 }
121
122 parent["elements"].push_back(std::move(j));
123 }
124}
125
126void generator::generate_diagram(nlohmann::json &parent) const
127{
128 parent["elements"] = std::vector<nlohmann::json>{};
129 parent["relationships"] = std::vector<nlohmann::json>{};
130
131 // Generate files and folders
132 util::for_each(model(), [this, &parent](const auto &f) {
133 generate(dynamic_cast<source_file &>(*f), parent);
134 });
135
136 // Process file include relationships
137 util::for_each(model(), [this, &parent](const auto &f) {
138 generate_relationships(dynamic_cast<source_file &>(*f), parent);
139 });
140}
141} // namespace clanguml::include_diagram::generators::json