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