0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
generator.h
Go to the documentation of this file.
1/**
2 * @file src/common/generators/json/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
22#include "config/config.h"
23#include "util/error.h"
24#include "util/util.h"
25#include "version/version.h"
26
27#include <clang/Basic/Version.h>
28#include <clang/Frontend/CompilerInstance.h>
29#include <clang/Tooling/CompilationDatabase.h>
30#include <clang/Tooling/Tooling.h>
31#include <glob/glob.hpp>
32
33#include <ostream>
34
36using nlohmann::json;
37
38void to_json(json &j, const source_location &sl);
39
40void to_json(json &j, const element &c);
41
42void to_json(json &j, const template_parameter &c);
43
44void to_json(json &j, const relationship &c);
45} // namespace clanguml::common::model
46
48
53
54/**
55 * @brief Base class for diagram generators
56 *
57 * @tparam ConfigType Configuration type
58 * @tparam DiagramType Diagram model type
59 */
60template <typename ConfigType, typename DiagramType>
62 : public clanguml::common::generators::generator<ConfigType, DiagramType> {
63public:
65 DiagramType>::generator;
66
67 ~generator() override = default;
68
69 /**
70 * @brief Generate diagram
71 *
72 * This is the main diagram generation entrypoint. It is responsible for
73 * calling other methods in appropriate order to generate the diagram into
74 * the output stream. It generates diagram elements, that are common
75 * to all types of diagrams in a given generator.
76 *
77 * @param ostr Output stream
78 */
79 void generate(std::ostream &ostr) const override;
80
81 /**
82 * @brief Generate diagram model
83 *
84 * This method must be implemented in subclasses for specific diagram
85 * types.
86 *
87 * @param ostr Output stream
88 */
89 virtual void generate_diagram(nlohmann::json &parent) const = 0;
90
91 /**
92 * @brief Generate metadata element with diagram metadata
93 *
94 * @param parent Root JSON object
95 */
96 void generate_metadata(nlohmann::json &parent) const;
97};
98
99template <typename DiagramModel, typename DiagramConfig>
100std::ostream &operator<<(
101 std::ostream &os, const generator<DiagramModel, DiagramConfig> &g)
102{
103 g.generate(os);
104 return os;
105}
106
107template <typename C, typename D>
108void generator<C, D>::generate(std::ostream &ostr) const
109{
110 const auto &config = generators::generator<C, D>::config();
111 const auto &model = generators::generator<C, D>::model();
112
113 if (!config.allow_empty_diagrams() && model.is_empty()) {
114 throw clanguml::error::empty_diagram_error{model.type(), model.name(),
115 "Diagram configuration resulted in empty diagram."};
116 }
117
118 nlohmann::json j;
119 j["name"] = model.name();
120 j["diagram_type"] = to_string(model.type());
121 if (config.title) {
122 j["title"] = config.title();
123 }
124
126
127 generate_metadata(j);
128
129 ostr << j;
130}
131
132template <typename C, typename D>
133void generator<C, D>::generate_metadata(nlohmann::json &parent) const
134{
135 if (generators::generator<C, D>::config().generate_metadata()) {
136 parent["metadata"]["clang_uml_version"] = clanguml::version::version();
137 parent["metadata"]["schema_version"] =
139 parent["metadata"]["llvm_version"] = clang::getClangFullVersion();
140 }
141}
142
143} // namespace clanguml::common::generators::json