0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
decorators.h
Go to the documentation of this file.
1/**
2 * @file src/decorators/decorators.h
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#pragma once
19
20#include <functional>
21#include <map>
22#include <memory>
23#include <string>
24#include <string_view>
25#include <vector>
26
27namespace clanguml {
28namespace decorators {
30 std::string label;
31 std::vector<std::string> diagrams;
32 std::string param;
33 std::string text;
34};
35
36/**
37 * @brief Base class for clang-uml comment tags
38 *
39 * This class provides basic interface for `clang-uml` comment tags, called
40 * decorators. All decorators are added to the code using `uml` Doxygen-style
41 * tag.
42 */
43struct decorator {
44 /** List of diagram names to which a given decorator applies */
45 std::vector<std::string> diagrams;
46
47 virtual ~decorator() = default;
48
49 /**
50 * @brief Create decorator of specific type based on it's string
51 * representation.
52 * @param c Decorator string representation extracted from the comment
53 * @return Decorator instance
54 */
55 static std::shared_ptr<decorator> from_string(std::string_view c);
56
57 /**
58 * @brief Check if decorator applies to a specific diagram.
59 *
60 * @param name Name of the diagram
61 * @return True, if this decorator applies to diagram `name`
62 */
63 bool applies_to_diagram(const std::string &name);
64
65protected:
66 decorator_toks tokenize(const std::string &label, std::string_view c);
67};
68
69/**
70 * @brief Represents a note diagram element
71 */
72struct note : public decorator {
73 static inline const std::string label{"note"};
74
75 std::string position{"left"};
76 std::string text;
77
78 static std::shared_ptr<decorator> from_string(std::string_view c);
79};
80
81/**
82 * @brief Whether a decorated element should be skipped from a diagram
83 */
84struct skip : public decorator {
85 static inline const std::string label{"skip"};
86
87 static std::shared_ptr<decorator> from_string(std::string_view c);
88};
89
90/**
91 * @brief Whether a decorated relationships should be skipped from a diagram
92 */
94 static inline const std::string label{"skiprelationship"};
95
96 static std::shared_ptr<decorator> from_string(std::string_view c);
97};
98
99/**
100 * @brief Apply specific style to a decorated diagram element
101 */
102struct style : public decorator {
103 static inline const std::string label{"style"};
104
105 std::string spec;
106 static std::shared_ptr<decorator> from_string(std::string_view c);
107};
108
109/**
110 * @brief Base class for decorators overriding default relationship types
111 */
112struct relationship : public decorator {
113 std::string multiplicity;
114};
115
116/**
117 * @brief Make a member an aggregation relationship
118 */
119struct aggregation : public relationship {
120 static inline const std::string label{"aggregation"};
121
122 static std::shared_ptr<decorator> from_string(std::string_view c);
123};
124
125/**
126 * @brief Make a member a composition relationship
127 */
128struct composition : public relationship {
129 static inline const std::string label{"composition"};
130
131 static std::shared_ptr<decorator> from_string(std::string_view c);
132};
133
134/**
135 * @brief Make a member an association relationship
136 */
137struct association : public relationship {
138 static inline const std::string label{"association"};
139
140 static std::shared_ptr<decorator> from_string(std::string_view c);
141};
142
143/**
144 * @brief Represents a call message in sequence diagram
145 */
146struct call : public decorator {
147 static inline const std::string label{"call"};
148
149 std::string callee;
150
151 static std::shared_ptr<decorator> from_string(std::string_view c);
152};
153
154/**
155 * @brief Parse a documentation block and extract all clang-uml decorators
156 *
157 * @param documentation_block Documentation block extracted from source code
158 * @param clanguml_tag Name of the clanguml tag (default `uml`)
159 * @return Pair of: a list of clang-uml decorators extracted from comment and
160 * a comment stripped of any uml directives
161 */
162std::pair<std::vector<std::shared_ptr<decorator>>, std::string> parse(
163 std::string documentation_block, const std::string &clanguml_tag = "uml");
164
165} // namespace decorators
166} // namespace clanguml