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/plantuml/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
23#include "util/error.h"
24
26
29 , together_group_stack_{false}
30{
31}
32
34 const package &p, std::ostream &ostr) const
35{
36 LOG_DBG("Generating relationships for package {}", p.full_name(true));
37
38 // Generate this packages relationship
39 if (model().should_include(relationship_t::kDependency)) {
40 for (const auto &r : p.relationships()) {
41 std::stringstream relstr;
42 try {
43 auto destination_package = model().get(r.destination());
44 if (!destination_package)
45 continue;
46
47 auto destination_alias = model().to_alias(r.destination());
48
49 if (!destination_alias.empty()) {
50 print_debug(r, ostr);
51 relstr << p.alias() << " ..> " << destination_alias;
52
53 if (config().generate_links) {
54 generate_link(relstr, r);
55 }
56
57 relstr << '\n';
58 ostr << 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 print_debug(p, ostr);
90
91 ostr << "package [" << display_name_adapter(p).with_packages().name()
92 << "] ";
93 ostr << "as " << p.alias();
94
95 if (p.is_deprecated())
96 ostr << " <<deprecated>>";
97
98 if (config().generate_links) {
99 generate_link(ostr, p);
100 }
101
102 generate_style(ostr, p.type_name(), p);
103
104 ostr << " {" << '\n';
105 }
106
107 for (const auto &subpackage : p) {
108 auto &pkg = dynamic_cast<package &>(*subpackage);
109 auto together_group = config().get_together_group(pkg.full_name(false));
110 if (together_group) {
111 together_group_stack_.group_together(together_group.value(), &pkg);
112 }
113 else {
114 generate(pkg, ostr);
115 }
116 }
117
118 generate_groups(ostr);
119
120 if (!uns.starts_with({p.full_name(false)})) {
121 ostr << "}" << '\n';
122 }
123
124 generate_notes(ostr, p);
125
126 together_group_stack_.leave();
127}
128
129void generator::generate_groups(std::ostream &ostr) const
130{
131 for (const auto &[group_name, group_elements] :
132 together_group_stack_.get_current_groups()) {
133 ostr << "together {\n";
134
135 for (auto *pkg : group_elements) {
136 generate(*pkg, ostr);
137 }
138
139 ostr << "}\n";
140 }
141}
142
143void generator::generate_diagram(std::ostream &ostr) const
144{
145 for (const auto &p : model()) {
146 auto &pkg = dynamic_cast<package &>(*p);
147 auto together_group = config().get_together_group(pkg.full_name(false));
148 if (together_group) {
149 together_group_stack_.group_together(together_group.value(), &pkg);
150 }
151 else {
152 generate(pkg, ostr);
153 }
154 }
155
156 generate_groups(ostr);
157
158 // Process package relationships
159 for (const auto &p : model()) {
160 generate_relationships(dynamic_cast<package &>(*p), ostr);
161 }
162
164}
165
166} // namespace clanguml::package_diagram::generators::plantuml