0.5.4
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-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#pragma once
19
22#include "config/config.h"
23#include "util/error.h"
24#include "util/util.h"
25#include "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
54std::string render_name(std::string name);
55
56/**
57 * @brief Base class for diagram generators
58 *
59 * @tparam ConfigType Configuration type
60 * @tparam DiagramType Diagram model type
61 */
62template <typename ConfigType, typename DiagramType>
64 : public clanguml::common::generators::generator<ConfigType, DiagramType> {
65public:
67 DiagramType>::generator;
68
69 ~generator() override = default;
70
71 /**
72 * @brief Generate diagram
73 *
74 * This is the main diagram generation entrypoint. It is responsible for
75 * calling other methods in appropriate order to generate the diagram into
76 * the output stream. It generates diagram elements, that are common
77 * to all types of diagrams in a given generator.
78 *
79 * @param ostr Output stream
80 */
81 void generate(std::ostream &ostr) const override;
82
83 /**
84 * @brief Generate diagram model
85 *
86 * This method must be implemented in subclasses for specific diagram
87 * types.
88 *
89 * @param ostr Output stream
90 */
91 virtual void generate_diagram(nlohmann::json &parent) const = 0;
92
93 /**
94 * @brief Generate metadata element with diagram metadata
95 *
96 * @param parent Root JSON object
97 */
98 void generate_metadata(nlohmann::json &parent) const;
99};
100
101template <typename DiagramModel, typename DiagramConfig>
102std::ostream &operator<<(
103 std::ostream &os, const generator<DiagramModel, DiagramConfig> &g)
104{
105 g.generate(os);
106 return os;
107}
108
109template <typename C, typename D>
110void generator<C, D>::generate(std::ostream &ostr) const
111{
112 const auto &config = generators::generator<C, D>::config();
113 const auto &model = generators::generator<C, D>::model();
114
115 if (!config.allow_empty_diagrams() && model.is_empty()) {
117 "Diagram configuration resulted in empty diagram."};
118 }
119
120 nlohmann::json j;
121 j["name"] = model.name();
122 j["diagram_type"] = to_string(model.type());
123 if (config.title) {
124 j["title"] = config.title();
125 }
126
128
129 generate_metadata(j);
130
131 ostr << j;
132}
133
134template <typename C, typename D>
135void generator<C, D>::generate_metadata(nlohmann::json &parent) const
136{
137 if (generators::generator<C, D>::config().generate_metadata()) {
138 parent["metadata"]["clang_uml_version"] =
139 clanguml::version::CLANG_UML_VERSION;
140 parent["metadata"]["schema_version"] =
141 clanguml::version::CLANG_UML_JSON_GENERATOR_SCHEMA_VERSION;
142 parent["metadata"]["llvm_version"] = clang::getClangFullVersion();
143 }
144}
145
146} // namespace clanguml::common::generators::json