0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
class_diagram_generator.h
Go to the documentation of this file.
1/**
2 * @file src/class_diagram/generators/json/class_diagram_generator.h
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#pragma once
19
28#include "config/config.h"
29#include "util/util.h"
30
31#include <glob/glob.hpp>
32#include <nlohmann/json.hpp>
33
34#include <filesystem>
35#include <fstream>
36#include <iostream>
37#include <sstream>
38
39namespace clanguml {
40namespace class_diagram {
41namespace generators {
42namespace json {
43
46template <typename C, typename D>
48
57
58using namespace clanguml::util;
59
60/**
61 * @brief Class diagram JSON generator
62 */
63class generator : public common_generator<diagram_config, diagram_model> {
64public:
66
68
69 /**
70 * @brief Main generator method.
71 *
72 * This method is called first and coordinates the entire diagram
73 * generation.
74 *
75 * @param ostr Output stream.
76 */
77 void generate_diagram(nlohmann::json &parent) const override;
78
79 /**
80 * Render class element into a JSON node.
81 *
82 * @param c class diagram element
83 * @param parent JSON node
84 */
85 void generate(const class_ &c, nlohmann::json &parent) const;
86
87 /**
88 * Render ObjC interface or protocol element into a JSON node.
89 *
90 * @param c enum diagram element
91 * @param parent JSON node
92 */
93 void generate(const objc_interface &c, nlohmann::json &parent) const;
94
95 /**
96 * Render enum element into a JSON node.
97 *
98 * @param c enum diagram element
99 * @param parent JSON node
100 */
101 void generate(const enum_ &c, nlohmann::json &parent) const;
102
103 /**
104 * Render concept element into a JSON node.
105 *
106 * @param c concept diagram element
107 * @param parent JSON node
108 */
109 void generate(const concept_ &c, nlohmann::json &parent) const;
110
111 /**
112 * Render package element into a JSON node.
113 *
114 * @param p package diagram element
115 * @param parent JSON node
116 */
117 void generate(const package &p, nlohmann::json &parent) const;
118
119 /**
120 * @brief In a nested diagram, generate the top level elements.
121 *
122 * This method iterates over the top level elements. In case the diagram
123 * is nested (i.e. includes packages), for each package it recursively
124 * call generation of elements contained in each package.
125 *
126 * @param parent JSON node
127 */
128 void generate_top_level_elements(nlohmann::json &parent) const;
129
130 /**
131 * @brief Generate all relationships in the diagram.
132 *
133 * @param parent JSON node
134 */
135 void generate_relationships(nlohmann::json &parent) const;
136
137 /**
138 * @brief Generate all relationships originating at a diagram element.
139 *
140 * @tparam T Type of diagram element
141 * @param c Diagram diagram element
142 * @param parent JSON node
143 */
144 template <typename T>
145 void generate_relationships(const T &c, nlohmann::json &parent) const;
146};
147
148template <typename T>
149void generator::generate_relationships(const T &c, nlohmann::json &parent) const
150{
151 const auto &model =
153
154 for (const auto &r : c.relationships()) {
155 auto target_element = model.get(r.destination());
156 if (!target_element.has_value()) {
157 LOG_DBG("Skipping {} relation from '{}' to '{}' due "
158 "to unresolved destination id",
159 to_string(r.type()), c.full_name(true),
160 r.destination().value());
161 continue;
162 }
163
164 nlohmann::json rel = r;
165 rel["source"] = std::to_string(c.id().value());
166 parent["relationships"].push_back(rel);
167 }
168}
169
170template <>
171void generator::generate_relationships<package>(
172 const package &p, nlohmann::json &parent) const;
173
174} // namespace json
175} // namespace generators
176} // namespace class_diagram
177} // namespace clanguml