0.5.4
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-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 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 nlohmann::json j;
53 j["id"] = std::to_string(f.id().value());
54 j["name"] = f.name();
55 auto display_name = f.full_name(false);
56#if defined(_MSC_VER)
57 util::replace_all(display_name, "\\", "/");
58#endif
59 j["display_name"] = std::move(display_name);
60
62 LOG_DBG("Generating directory {}", f.name());
63
64 j["type"] = "folder";
65
66 util::for_each(f, [this, &j](const auto &file) {
67 generate(dynamic_cast<const source_file &>(*file), j);
68 });
69
70 parent["elements"].push_back(std::move(j));
71 }
72 else {
73 LOG_DBG("Generating file {}", f.name());
74
75 j["type"] = "file";
76 j["file_kind"] = to_string(f.type());
78 j["is_system"] = f.is_system_header();
79 }
80
81 parent["elements"].push_back(std::move(j));
82 }
83}
84
85void generator::generate_diagram(nlohmann::json &parent) const
86{
87 parent["elements"] = std::vector<nlohmann::json>{};
88 parent["relationships"] = std::vector<nlohmann::json>{};
89
90 // Generate files and folders
91 util::for_each(model(), [this, &parent](const auto &f) {
92 generate(dynamic_cast<source_file &>(*f), parent);
93 });
94
95 // Process file include relationships
96 util::for_each(model(), [this, &parent](const auto &f) {
97 generate_relationships(dynamic_cast<source_file &>(*f), parent);
98 });
99}
100} // namespace clanguml::include_diagram::generators::json