0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
nested_element_stack.h
Go to the documentation of this file.
1/**
2 * @file src/common/generators/nested_element_stack.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 <cstdint>
21#include <map>
22#include <string>
23#include <vector>
24
26
27/**
28 * This is a helper class for generating nested groups of elements
29 * in the diagrams, e.g. PlantUML `together` option.
30 *
31 * @tparam T Type of stack elements
32 */
33template <typename T> class nested_element_stack {
34public:
36 : is_flat_{is_flat}
37 {
38 current_level_groups_.push_back({});
39 }
40
41 /**
42 * Switch to next level in the element stack
43 */
44 void enter()
45 {
46 if (!is_flat_)
48
49 current_level_groups_.push_back({});
50 }
51
52 /**
53 * Switch to previous level in the element stack
54 */
55 void leave()
56 {
57 if (!is_flat_)
59
60 current_level_groups_.pop_back();
61 }
62
63 /**
64 * Add element pointer to a specified group at the current level
65 */
66 void group_together(const std::string &group_name, T *e)
67 {
68 current_level_groups_[current_level_][group_name].push_back(e);
69 }
70
71 /**
72 * Get map of element groups at the current level.
73 *
74 * @return Reference to element groups.
75 */
76 const std::map<std::string, std::vector<T *>> &get_current_groups()
77 {
79 }
80
81 /**
82 * Get element group by name - the group must exist at the current level.
83 *
84 * @param group_name Element group name
85 * @return
86 */
87 const std::vector<T *> &get_group(const std::string &group_name)
88 {
89 return get_current_groups().at(group_name);
90 }
91
92private:
94
95 uint32_t current_level_{0};
96
97 std::vector<std::map<std::string, std::vector<T *>>> current_level_groups_;
98};
99
100} // namespace clanguml::common::generators