0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
clanguml::common::model::nested_trait< T, Path > Class Template Reference

Base class for elements nested in the diagram. More...

Detailed Description

template<typename T, typename Path>
class clanguml::common::model::nested_trait< T, Path >

Base class for elements nested in the diagram.

This class provides a common trait for diagram elements which can contain other nested elements, e.g. packages.

Template Parameters
TType of element
PathType of nested path (e.g. namespace or directory path)

Definition at line 41 of file nested_trait.h.

#include <nested_trait.h>

Public Member Functions

 nested_trait ()=default
 
 nested_trait (const nested_trait &)=delete
 
 nested_trait (nested_trait &&) noexcept=default
 
nested_traitoperator= (const nested_trait &)=delete
 
nested_traitoperator= (nested_trait &&) noexcept=default
 
virtual ~nested_trait ()=default
 
template<typename V = T>
bool add_element (std::unique_ptr< V > p)
 
template<typename V = T>
bool add_element (const Path &path, std::unique_ptr< V > p)
 
template<typename V = T>
auto get_element (const Path &path) const
 
template<typename V = T>
auto get_element (const std::string &name) const
 
bool has_element (const std::string &name) const
 
template<typename F >
bool all_of (F &&f) const
 
bool is_empty () const
 
auto begin ()
 
auto end ()
 
auto cbegin () const
 
auto cend () const
 
auto begin () const
 
auto end () const
 
void print_tree (const int level)
 
void remove (const std::set< eid_t > &element_ids)
 

Private Attributes

std::vector< std::unique_ptr< T > > elements_
 

Constructor & Destructor Documentation

◆ nested_trait() [1/3]

template<typename T , typename Path >
clanguml::common::model::nested_trait< T, Path >::nested_trait ( )
default

◆ nested_trait() [2/3]

template<typename T , typename Path >
clanguml::common::model::nested_trait< T, Path >::nested_trait ( const nested_trait< T, Path > &  )
delete

◆ nested_trait() [3/3]

template<typename T , typename Path >
clanguml::common::model::nested_trait< T, Path >::nested_trait ( nested_trait< T, Path > &&  )
defaultnoexcept

◆ ~nested_trait()

template<typename T , typename Path >
virtual clanguml::common::model::nested_trait< T, Path >::~nested_trait ( )
virtualdefault

Member Function Documentation

◆ add_element() [1/2]

template<typename T , typename Path >
template<typename V = T>
bool clanguml::common::model::nested_trait< T, Path >::add_element ( const Path &  path,
std::unique_ptr< V >  p 
)
inline

Add element at a nested path.

Template Parameters
VType of element
Parameters
pathNested path (e.g. list of namespaces)
pElement
Returns
True, if element was added.

Definition at line 85 of file nested_trait.h.

86 {
87 assert(p);
88
89 LOG_DBG("Adding nested element {} at path '{}'", p->name(),
90 path.to_string());
91
92 if (path.is_empty()) {
93 return add_element(std::move(p));
94 }
95
96 auto parent = get_element(path);
97
98 if (parent && dynamic_cast<nested_trait<T, Path> *>(&parent.value())) {
99 p->set_parent_element_id(parent.value().id());
100 return dynamic_cast<nested_trait<T, Path> &>(parent.value())
101 .template add_element<V>(std::move(p));
102 }
103
104 LOG_INFO("No parent element found at: {}", path.to_string());
105
106 throw std::runtime_error(
107 "No parent element found for " + path.to_string());
108 }

◆ add_element() [2/2]

template<typename T , typename Path >
template<typename V = T>
bool clanguml::common::model::nested_trait< T, Path >::add_element ( std::unique_ptr< V >  p)
inline

Add element at the current nested level.

Template Parameters
VType of element
Parameters
pElement
Returns
True, if element was added.

Definition at line 61 of file nested_trait.h.

62 {
63 auto it = std::find_if(elements_.begin(), elements_.end(),
64 [&p](const auto &e) { return *e == *p; });
65
66 if (it != elements_.end()) {
67 // Element already in element tree
68 return false;
69 }
70
71 elements_.emplace_back(std::move(p));
72
73 return true;
74 }

◆ all_of()

template<typename T , typename Path >
template<typename F >
bool clanguml::common::model::nested_trait< T, Path >::all_of ( F &&  f) const
inline

Return result of functor f applied to all_of elements.

Template Parameters
FFunctor type
Parameters
fFunctor value
Returns
True, if functor return true for elements, including nested ones.

Definition at line 185 of file nested_trait.h.

186 {
187 return std::all_of(
188 elements_.cbegin(), elements_.cend(), [f](const auto &e) {
189 const auto *package_ptr =
190 dynamic_cast<nested_trait<T, Path> *>(e.get());
191
192 if (package_ptr != nullptr)
193 return package_ptr->all_of(f);
194
195 return f(*e);
196 });
197 }

◆ begin() [1/2]

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::begin ( )
inline

Definition at line 214 of file nested_trait.h.

214{ return elements_.begin(); }

◆ begin() [2/2]

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::begin ( ) const
inline

Definition at line 220 of file nested_trait.h.

220{ return elements_.begin(); }

◆ cbegin()

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::cbegin ( ) const
inline

Definition at line 217 of file nested_trait.h.

217{ return elements_.cbegin(); }

◆ cend()

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::cend ( ) const
inline

Definition at line 218 of file nested_trait.h.

218{ return elements_.cend(); }

◆ end() [1/2]

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::end ( )
inline

Definition at line 215 of file nested_trait.h.

215{ return elements_.end(); }

◆ end() [2/2]

template<typename T , typename Path >
auto clanguml::common::model::nested_trait< T, Path >::end ( ) const
inline

Definition at line 221 of file nested_trait.h.

221{ return elements_.end(); }

◆ get_element() [1/2]

template<typename T , typename Path >
template<typename V = T>
auto clanguml::common::model::nested_trait< T, Path >::get_element ( const Path &  path) const
inline

Get element at path, if exists.

Template Parameters
VElement type.
Parameters
pathPath to the element.
Returns
Optional reference to the element.

Definition at line 117 of file nested_trait.h.

118 {
119 if (path.is_empty() || !has_element(path[0])) {
120 LOG_DBG("Nested element {} not found in element", path.to_string());
121 return optional_ref<V>{};
122 }
123
124 if (path.size() == 1) {
125 return get_element<V>(path[0]);
126 }
127
128 auto p = get_element<T>(path[0]);
129
130 if (!p)
131 return optional_ref<V>{};
132
133 if (dynamic_cast<nested_trait<T, Path> *>(&p.value()))
134 return dynamic_cast<nested_trait<T, Path> &>(p.value())
135 .get_element<V>(Path{path.begin() + 1, path.end()});
136
137 return optional_ref<V>{};
138 }

◆ get_element() [2/2]

template<typename T , typename Path >
template<typename V = T>
auto clanguml::common::model::nested_trait< T, Path >::get_element ( const std::string &  name) const
inline

Get element by name at the current nested level.

Template Parameters
VType of element.
Parameters
nameName of the element (cannot contain namespace or path)
Returns
Optional reference to the element.

Definition at line 147 of file nested_trait.h.

148 {
149 assert(!util::contains(name, "::"));
150
151 auto it = std::find_if(elements_.cbegin(), elements_.cend(),
152 [&](const auto &p) { return name == p->name(); });
153
154 if (it == elements_.end())
155 return optional_ref<V>{};
156
157 assert(it->get() != nullptr);
158
159 if (dynamic_cast<V *>(it->get()))
160 return optional_ref<V>{std::ref<V>(dynamic_cast<V &>(*it->get()))};
161
162 return optional_ref<V>{};
163 }

◆ has_element()

template<typename T , typename Path >
bool clanguml::common::model::nested_trait< T, Path >::has_element ( const std::string &  name) const
inline

Returns true of this nested level contains an element with specified name.

Parameters
nameName of the element.
Returns
True if element exists.

Definition at line 172 of file nested_trait.h.

173 {
174 return std::find_if(elements_.cbegin(), elements_.cend(),
175 [&](const auto &p) { return name == p->name(); }) !=
176 elements_.end();
177 }

◆ is_empty()

template<typename T , typename Path >
bool clanguml::common::model::nested_trait< T, Path >::is_empty ( ) const
inline

Check if nested element is empty.

Returns
True if this nested element is empty.

Definition at line 204 of file nested_trait.h.

205 {
206 return elements_.empty() ||
207 std::all_of(elements_.cbegin(), elements_.cend(), [](auto &e) {
208 const auto *package_ptr =
209 dynamic_cast<nested_trait<T, Path> *>(e.get());
210 return package_ptr != nullptr && package_ptr->is_empty();
211 });
212 }

◆ operator=() [1/2]

template<typename T , typename Path >
nested_trait & clanguml::common::model::nested_trait< T, Path >::operator= ( const nested_trait< T, Path > &  )
delete

◆ operator=() [2/2]

template<typename T , typename Path >
nested_trait & clanguml::common::model::nested_trait< T, Path >::operator= ( nested_trait< T, Path > &&  )
defaultnoexcept

◆ print_tree()

template<typename T , typename Path >
void clanguml::common::model::nested_trait< T, Path >::print_tree ( const int  level)
inline

Print the nested trait in the form of a tree.

This method is used for debugging only.

Parameters
levelTree level

Definition at line 230 of file nested_trait.h.

231 {
232 const auto &d = *this;
233
234 if (level == 0) {
235 std::cout << "--- Printing tree:\n";
236 }
237 for (const auto &e : d) {
238 if (dynamic_cast<nested_trait<T, Path> *>(e.get())) {
239 std::cout << std::string(level, ' ') << "[" << *e << "]\n";
240 dynamic_cast<nested_trait<T, Path> *>(e.get())->print_tree(
241 level + 1);
242 }
243 else {
244 std::cout << std::string(level, ' ') << "- " << *e << "]\n";
245 }
246 }
247 }

◆ remove()

template<typename T , typename Path >
void clanguml::common::model::nested_trait< T, Path >::remove ( const std::set< eid_t > &  element_ids)
inline

Definition at line 249 of file nested_trait.h.

250 {
251 // First remove all matching elements on this level
252 elements_.erase(std::remove_if(elements_.begin(), elements_.end(),
253 [&element_ids](auto &&e) {
254 return element_ids.count(e->id()) > 0;
255 }),
256 elements_.end());
257
258 // Now recurse to any packages on this level
259 for (auto &p : elements_) {
260 if (dynamic_cast<nested_trait<T, Path> *>(p.get()))
261 dynamic_cast<nested_trait<T, Path> *>(p.get())->remove(
262 element_ids);
263 }
264 }

Member Data Documentation

◆ elements_

template<typename T , typename Path >
std::vector<std::unique_ptr<T> > clanguml::common::model::nested_trait< T, Path >::elements_
private

Definition at line 267 of file nested_trait.h.


The documentation for this class was generated from the following file: