0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
package_diagram_generator.cc
Go to the documentation of this file.
1/**
2 * @file src/package_diagram/generators/mermaid/package_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
22#include "util/error.h"
23
25
27
30 , together_group_stack_{false}
31{
32}
33
34void generator::generate_diagram_type(std::ostream &ostr) const
35{
36 ostr << "flowchart\n";
37}
38
40 const package &p, std::ostream &ostr) const
41{
42 LOG_DBG("Generating relationships for package {}", p.full_name(true));
43
44 // Generate this packages relationship
45 if (model().should_include(relationship_t::kDependency)) {
46 for (const auto &r : p.relationships()) {
47 std::stringstream relstr;
48 try {
49 auto destination_package = model().get(r.destination());
50
51 if (!destination_package)
52 continue;
53
54 auto destination_alias = model().to_alias(r.destination());
55 if (!destination_alias.empty()) {
56 relstr << p.alias() << " -.-> " << destination_alias
57 << '\n';
58 ostr << indent(1) << relstr.str();
59 }
60 }
61 catch (error::uml_alias_missing &e) {
62 LOG_DBG("=== Skipping dependency relation from {} to {} due "
63 "to: {}",
64 p.full_name(false), r.destination(), e.what());
65 }
66 }
67 }
68
69 // Process it's subpackages relationships
70 for (const auto &subpackage : p) {
72 dynamic_cast<const package &>(*subpackage), ostr);
73 }
74}
75
76void generator::generate(const package &p, std::ostream &ostr) const
77{
79
80 LOG_DBG("Generating package {}", p.name());
81
83
84 const auto &uns = config().using_namespace();
85
86 // Don't generate packages from namespaces filtered out by
87 // using_namespace
88 if (!uns.starts_with({p.full_name(false)})) {
89 ostr << indent(1) << "subgraph " << p.alias() << "["
90 << display_name_adapter(p).with_packages().name() << "]\n";
91
92 if (p.is_deprecated())
93 ostr << indent(1) << "%% <<deprecated>>\n";
94 }
95
96 for (const auto &subpackage : p) {
97 auto &pkg = dynamic_cast<package &>(*subpackage);
98 auto together_group = config().get_together_group(pkg.full_name(false));
99 if (together_group) {
100 together_group_stack_.group_together(together_group.value(), &pkg);
101 }
102 else {
103 generate(pkg, ostr);
104 }
105 }
106
107 generate_groups(ostr);
108
109 if (!uns.starts_with({p.full_name(false)})) {
110 ostr << indent(1) << "end" << '\n';
111 }
112
113 if (config().generate_links) {
115 }
116
117 generate_notes(ostr, p);
118
119 together_group_stack_.leave();
120}
121
122void generator::generate_groups(std::ostream &ostr) const
123{
124 for (const auto &[group_name, group_elements] :
125 together_group_stack_.get_current_groups()) {
126 ostr << indent(1) << "%% together group - not rendered in MermaidJS \n";
127
128 for (auto *pkg : group_elements) {
129 generate(*pkg, ostr);
130 }
131
132 ostr << indent(1) << "%% end together group\n";
133 }
134}
135
137 std::ostream &ostr, const common::model::diagram_element &element) const
138{
139 const auto &config =
141
142 for (const auto &decorator : element.decorators()) {
143 auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
144 if (note && note->applies_to_diagram(config.name)) {
145 auto note_id_str = fmt::format("N_{}", note_id_++);
146
147 ostr << indent(1) << note_id_str << "(" << note->text << ")\n";
148
149 ostr << indent(1) << note_id_str << "-.-" << element.alias()
150 << '\n';
151 }
152 }
153}
154
155void generator::generate_diagram(std::ostream &ostr) const
156{
157 for (const auto &p : model()) {
158 auto &pkg = dynamic_cast<package &>(*p);
159 auto together_group = config().get_together_group(pkg.full_name(false));
160 if (together_group) {
161 together_group_stack_.group_together(together_group.value(), &pkg);
162 }
163 else {
164 generate(pkg, ostr);
165 }
166 }
167
168 generate_groups(ostr);
169
170 // Process package relationships
171 for (const auto &p : model()) {
172 generate_relationships(dynamic_cast<package &>(*p), ostr);
173 }
174}
175
176} // namespace clanguml::package_diagram::generators::mermaid