0.5.4
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-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
27 , together_group_stack_{false}
28{
29}
30
32 const package &p, std::ostream &ostr) const
33{
34 LOG_DBG("Generating relationships for package {}", p.full_name(true));
35
36 // Generate this packages relationship
37 if (model().should_include(relationship_t::kDependency)) {
38 for (const auto &r : p.relationships()) {
39 std::stringstream relstr;
40 try {
41 auto destination_package = model().get(r.destination());
42 if (!destination_package)
43 continue;
44
45 auto destination_alias = model().to_alias(r.destination());
46
47 if (!destination_alias.empty()) {
48 relstr << p.alias() << " ..> " << destination_alias << '\n';
49 ostr << relstr.str();
50 }
51 }
52 catch (error::uml_alias_missing &e) {
53 LOG_DBG("=== Skipping dependency relation from {} to {} due "
54 "to: {}",
55 p.full_name(false), r.destination(), e.what());
56 }
57 }
58 }
59
60 // Process it's subpackages relationships
61 for (const auto &subpackage : p) {
63 dynamic_cast<const package &>(*subpackage), ostr);
64 }
65}
66
67void generator::generate(const package &p, std::ostream &ostr) const
68{
69 LOG_DBG("Generating package {}", p.name());
70
72
73 const auto &uns = config().using_namespace();
74
75 // Don't generate packages from namespaces filtered out by
76 // using_namespace
77 if (!uns.starts_with({p.full_name(false)})) {
78 ostr << "package [" << p.name() << "] ";
79 ostr << "as " << p.alias();
80
81 if (p.is_deprecated())
82 ostr << " <<deprecated>>";
83
84 if (config().generate_links) {
85 generate_link(ostr, p);
86 }
87
88 generate_style(ostr, p.type_name(), p);
89
90 ostr << " {" << '\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 << "}" << '\n';
108 }
109
110 generate_notes(ostr, p);
111
112 together_group_stack_.leave();
113}
114
115void generator::generate_groups(std::ostream &ostr) const
116{
117 for (const auto &[group_name, group_elements] :
118 together_group_stack_.get_current_groups()) {
119 ostr << "together {\n";
120
121 for (auto *pkg : group_elements) {
122 generate(*pkg, ostr);
123 }
124
125 ostr << "}\n";
126 }
127}
128
129void generator::generate_diagram(std::ostream &ostr) const
130{
131 for (const auto &p : model()) {
132 auto &pkg = dynamic_cast<package &>(*p);
133 auto together_group = config().get_together_group(pkg.full_name(false));
134 if (together_group) {
135 together_group_stack_.group_together(together_group.value(), &pkg);
136 }
137 else {
138 generate(pkg, ostr);
139 }
140 }
141
142 generate_groups(ostr);
143
144 // Process package relationships
145 for (const auto &p : model()) {
146 generate_relationships(dynamic_cast<package &>(*p), ostr);
147 }
148
150}
151
152} // namespace clanguml::package_diagram::generators::plantuml