0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
diagram.cc
Go to the documentation of this file.
1/**
2 * @file src/include_diagram/model/diagram.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
19#include "diagram.h"
20
22#include "util/error.h"
23#include "util/util.h"
24
26
28{
30}
31
33 const std::string &full_name) const
34{
35 return find<source_file>(full_name);
36}
37
39 const eid_t id) const
40{
41 return find<source_file>(id);
42}
43
44void diagram::add_file(std::unique_ptr<common::model::source_file> &&f)
45{
46 // Don't add the same file more than once
47 if (find<source_file>(f->id()))
48 return;
49
50 LOG_DBG("Adding source file: {}, {}", f->name(), f->fs_path().string());
51
52 auto &ff = *f;
53
54 assert(!ff.name().empty());
55 assert(ff.id().value() != 0);
56
57 auto p = ff.path();
58
59 if (!f->path().is_empty()) {
60 // If the parent path is not empty, ensure relative parent directories
61 // of this source_file are in the diagram
62 common::model::filesystem_path parent_path_so_far{
64 for (const auto &directory : f->path()) {
65 auto source_file_path = parent_path_so_far | directory;
66 if (parent_path_so_far.is_empty())
67 source_file_path = {directory};
68
69 auto dir = std::make_unique<common::model::source_file>(
70 std::filesystem::path{source_file_path.to_string()});
72
73 assert(!dir->name().empty());
74
75 if (!get_element(source_file_path).has_value()) {
76 add_file(std::move(dir));
77
78 LOG_DBG("Added directory '{}' at path '{}'", directory,
79 parent_path_so_far.to_string());
80 }
81
82 parent_path_so_far.append(directory);
83 }
84 }
85
86 assert(p.type() == common::model::path_type::kFilesystem);
87
88 if (add_element(p, std::move(f)))
89 element_view<source_file>::add(ff);
90}
91
94{
95 return element_view<source_file>::view();
96}
97
100 const std::string &name, const common::model::namespace_ &ns) const
101{
102 // Convert to preferred OS path
103 std::filesystem::path namePath{name};
104 auto namePreferred = namePath.make_preferred().string();
105
106 auto element_opt = get(namePreferred);
107
108 if (!element_opt) {
109 // If no element matches, try to prepend the 'using_namespace'
110 // value to the element and search again
111 auto fully_qualified_name = ns | namePreferred;
112 element_opt = get(fully_qualified_name.to_string());
113 }
114
115 return element_opt;
116}
117
118inja::json diagram::context() const
119{
120 inja::json ctx;
121 ctx["name"] = name();
122 ctx["type"] = "include";
123
124 inja::json::array_t elements{};
125
126 // Add files and directories
127 for (const auto &f : files()) {
128 elements.emplace_back(f.get().context());
129 }
130
131 ctx["elements"] = elements;
132
133 return ctx;
134}
135
137{
138 // First find all element ids which should be removed
139 std::set<eid_t> to_remove;
140
141 for (auto &f : element_view<source_file>::view())
142 if (f.get().type() != common::model::source_file_t::kDirectory &&
143 !filter().should_include(f.get())) {
144 to_remove.emplace(f.get().id());
145 }
146
147 for (auto &sf : element_view<source_file>::view())
148 sf.get().apply_filter(filter(), to_remove);
149
150 element_view<source_file>::remove(to_remove);
151
153}
154
155bool diagram::is_empty() const { return element_view<source_file>::is_empty(); }
156
157} // namespace clanguml::include_diagram::model
158
159namespace clanguml::common::model {
160template <>
161bool check_diagram_type<include_diagram::model::diagram>(diagram_t t)
162{
163 return t == diagram_t::kInclude;
164}
165}