0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
class.cc
Go to the documentation of this file.
1/**
2 * @file src/class_diagram/model/class.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 "class.h"
20
22#include "util/util.h"
23
24#include <sstream>
25
27
29 : template_element{using_namespace}
30{
31}
32
33bool class_::is_struct() const { return is_struct_; }
34
35void class_::is_struct(bool is_struct) { is_struct_ = is_struct; }
36
37bool class_::is_union() const { return is_union_; }
38
39void class_::is_union(bool is_union) { is_union_ = is_union; }
40
42{
43 members_.emplace_back(std::move(member));
44}
45
47{
48 methods_.emplace_back(std::move(method));
49}
50
52{
53 for (const auto &p : bases_) {
54 if (p.id() == parent.id()) {
55 return;
56 }
57 }
58
59 bases_.emplace_back(std::move(parent));
60}
61
62const std::vector<class_member> &class_::members() const { return members_; }
63
64const std::vector<class_method> &class_::methods() const { return methods_; }
65
66const std::vector<class_parent> &class_::parents() const { return bases_; }
67std::vector<class_parent> &class_::parents() { return bases_; }
68
69bool operator==(const class_ &l, const class_ &r) { return l.id() == r.id(); }
70
71std::string class_::full_name_no_ns() const
72{
73 using namespace clanguml::util;
74
75 std::ostringstream ostr;
76
77 ostr << name();
78
80
81 return ostr.str();
82}
83
84std::string class_::full_name(bool relative) const
85{
86 using namespace clanguml::util;
88
89 std::ostringstream ostr;
90
91 ostr << name_and_ns();
92
93 render_template_params(ostr, using_namespace(), relative);
94
95 std::string res;
96
97 if (relative)
98 res = using_namespace().relative(ostr.str());
99 else
100 res = ostr.str();
101
102 if (res.empty())
103 return "<<anonymous>>";
104
105 return res;
106}
107
109{
110 // TODO check if all base abstract methods are overriden
111 // with non-abstract methods
112 return std::any_of(methods_.begin(), methods_.end(),
113 [](const auto &method) { return method.is_pure_virtual(); });
114}
115
117 const common::model::diagram_filter &filter, const std::set<eid_t> &removed)
118{
119 diagram_element::apply_filter(filter, removed);
120
123
124 // Remove class bases which are no longer in the diagram
125 parents().erase(
126 std::remove_if(parents().begin(), parents().end(),
127 [&removed](auto &&p) { return removed.count(p.id()) > 0; }),
128 parents().end());
129}
130
131std::optional<std::string> class_::doxygen_link() const
132{
133 const auto *type = is_struct() ? "struct" : "class";
134
135 auto name = name_and_ns();
136 util::replace_all(name, "_", "__");
137 util::replace_all(name, "::", "_1_1");
138 util::replace_all(name, "##", "_1_1"); // nested classes
139 return fmt::format("{}{}.html", type, name);
140}
141} // namespace clanguml::class_diagram::model