0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
clanguml::common::model::path Class Reference

Diagram path. More...

Detailed Description

Diagram path.

This class stores a diagram path, such as a namespace or directory structure.

Definition at line 48 of file path.h.

#include <path.h>

Public Types

using container_type = std::vector< std::string >
 

Public Member Functions

 path (path_type pt=path_type::kNamespace)
 
 path (const std::string &ns, path_type pt=path_type::kNamespace)
 
virtual ~path ()=default
 
 path (container_type::const_iterator begin, container_type::const_iterator end, path_type pt=path_type::kNamespace)
 
 path (const path &right)=default
 
pathoperator= (const path &right)
 
 path (path &&right) noexcept=default
 
pathoperator= (path &&right) noexcept=default
 
 path (std::initializer_list< std::string > ns, path_type pt=path_type::kNamespace)
 
 path (const std::vector< std::string > &ns, path_type pt=path_type::kNamespace)
 
std::string to_string () const
 
bool is_empty () const
 
size_t size () const
 
path operator| (const path &right) const
 
void operator|= (const path &right)
 
path operator| (const std::string &right) const
 
void operator|= (const std::string &right)
 
std::string & operator[] (const unsigned int index)
 
const std::string & operator[] (const unsigned int index) const
 
void append (const std::string &name)
 
void append (const path &ns)
 
void pop_back ()
 
std::optional< pathparent () const
 
bool starts_with (const path &prefix) const
 
bool ends_with (const path &suffix) const
 
path common_path (const path &right) const
 Returns the common prefix of 2 paths.
 
path relative_to (const path &right) const
 
std::string relative (const std::string &ns) const
 
std::string name () const
 
path::container_type::iterator begin ()
 
path::container_type::iterator end ()
 
path::container_type::const_iterator cbegin () const
 
path::container_type::const_iterator cend () const
 
path::container_type::const_iterator begin () const
 
path::container_type::const_iterator end () const
 
path_type type () const
 
const container_typetokens () const
 

Static Public Member Functions

static container_type split (const std::string &ns, path_type pt=path_type::kNamespace)
 

Private Member Functions

const char * separator () const
 

Static Private Member Functions

static const char * separator (path_type pt)
 

Private Attributes

path_type path_type_
 
container_type path_
 

Friends

bool operator== (const path &left, const path &right)
 
bool operator< (const path &left, const path &right)
 

Member Typedef Documentation

◆ container_type

using clanguml::common::model::path::container_type = std::vector<std::string>

Definition at line 81 of file path.h.

Constructor & Destructor Documentation

◆ path() [1/7]

clanguml::common::model::path::path ( path_type  pt = path_type::kNamespace)
inline

Definition at line 105 of file path.h.

106 : path_type_{pt}
107 {
108 }

◆ path() [2/7]

clanguml::common::model::path::path ( const std::string &  ns,
path_type  pt = path_type::kNamespace 
)
inline

Definition at line 110 of file path.h.

111 : path_type_{pt}
112 {
113 if (ns.empty())
114 return;
115
116 path_ = split(ns, pt);
117 }

◆ ~path()

virtual clanguml::common::model::path::~path ( )
virtualdefault

◆ path() [3/7]

clanguml::common::model::path::path ( container_type::const_iterator  begin,
container_type::const_iterator  end,
path_type  pt = path_type::kNamespace 
)
inline

Definition at line 121 of file path.h.

124 : path(pt)
125 {
126 if (begin == end)
127 return;
128
129 std::copy(begin, end, std::back_inserter(path_));
130 }

◆ path() [4/7]

clanguml::common::model::path::path ( const path right)
default

◆ path() [5/7]

clanguml::common::model::path::path ( path &&  right)
defaultnoexcept

◆ path() [6/7]

clanguml::common::model::path::path ( std::initializer_list< std::string >  ns,
path_type  pt = path_type::kNamespace 
)
inline

Definition at line 153 of file path.h.

155 : path(pt)
156 {
157 if ((ns.size() == 1) &&
158 util::contains(*ns.begin(), std::string{separator()})) {
159 path_ = util::split(*ns.begin(), separator());
160 }
161 else if ((ns.size() == 1) && ns.begin()->empty()) {
162 }
163 else
164 path_ = ns;
165 }

◆ path() [7/7]

clanguml::common::model::path::path ( const std::vector< std::string > &  ns,
path_type  pt = path_type::kNamespace 
)
inlineexplicit

Definition at line 167 of file path.h.

169 : path(pt)
170 {
171 if ((ns.size() == 1) &&
172 util::contains(*ns.begin(), std::string{separator()})) {
173 path_ = util::split(*ns.begin(), separator());
174 }
175 else if ((ns.size() == 1) && ns.begin()->empty()) {
176 }
177 else
178 path_ = ns;
179 }

Member Function Documentation

◆ append() [1/2]

void clanguml::common::model::path::append ( const path ns)
inline

Append path to current path.

Parameters
nsPath to append.

Definition at line 281 of file path.h.

282 {
283 for (const auto &n : ns) {
284 append(n);
285 }
286 }

◆ append() [2/2]

void clanguml::common::model::path::append ( const std::string &  name)
inline

Append path element to path.

Returns
New path.

Definition at line 274 of file path.h.

274{ path_.push_back(name); }

◆ begin() [1/2]

path::container_type::iterator clanguml::common::model::path::begin ( )
inline

Definition at line 410 of file path.h.

410{ return path_.begin(); }

◆ begin() [2/2]

path::container_type::const_iterator clanguml::common::model::path::begin ( ) const
inline

Definition at line 419 of file path.h.

419{ return path_.begin(); }

◆ cbegin()

path::container_type::const_iterator clanguml::common::model::path::cbegin ( ) const
inline

Definition at line 413 of file path.h.

414 {
415 return path_.cbegin();
416 }

◆ cend()

path::container_type::const_iterator clanguml::common::model::path::cend ( ) const
inline

Definition at line 417 of file path.h.

417{ return path_.cend(); }

◆ common_path()

path clanguml::common::model::path::common_path ( const path right) const
inline

Returns the common prefix of 2 paths.

If no common prefix exists between 2 paths, the result is an empty path.

Parameters
rightPath to compare
Returns
Common path prefix

Definition at line 342 of file path.h.

343 {
344 path res{};
345 for (auto i = 0U; i < std::min(size(), right.size()); i++) {
346 if (path_[i] == right[i])
347 res |= path_[i];
348 else
349 break;
350 }
351 return res;
352 }

◆ end() [1/2]

path::container_type::iterator clanguml::common::model::path::end ( )
inline

Definition at line 411 of file path.h.

411{ return path_.end(); }

◆ end() [2/2]

path::container_type::const_iterator clanguml::common::model::path::end ( ) const
inline

Definition at line 420 of file path.h.

420{ return path_.end(); }

◆ ends_with()

bool clanguml::common::model::path::ends_with ( const path suffix) const
inline

Returns true if path ends with suffix

Parameters
suffixPath suffix to check
Returns

Definition at line 329 of file path.h.

330 {
331 return util::ends_with(path_, suffix.path_);
332 }

◆ is_empty()

bool clanguml::common::model::path::is_empty ( ) const
inline

Whether the path is empty.

Returns

Definition at line 213 of file path.h.

213{ return path_.empty(); }

◆ name()

std::string clanguml::common::model::path::name ( ) const
inline

Return the name of the last element in the path.

Returns
Name of the last element in the path.

Definition at line 403 of file path.h.

404 {
405 assert(size() > 0);
406
407 return path_.back();
408 }

◆ operator=() [1/2]

path & clanguml::common::model::path::operator= ( const path right)
inline

Definition at line 134 of file path.h.

135 {
136 if (&right == this)
137 return *this;
138
139 if (path_type_ != right.path_type_)
140 throw std::runtime_error(
141 "Cannot assign a path to a path with another path type.");
142
143 path_type_ = right.path_type_;
144 path_ = right.path_;
145
146 return *this;
147 }

◆ operator=() [2/2]

path & clanguml::common::model::path::operator= ( path &&  right)
defaultnoexcept

◆ operator[]() [1/2]

std::string & clanguml::common::model::path::operator[] ( const unsigned int  index)
inline

Definition at line 262 of file path.h.

262{ return path_[index]; }

◆ operator[]() [2/2]

const std::string & clanguml::common::model::path::operator[] ( const unsigned int  index) const
inline

Definition at line 264 of file path.h.

265 {
266 return path_[index];
267 }

◆ operator|() [1/2]

path clanguml::common::model::path::operator| ( const path right) const
inline

Append path to path.

Parameters
rightPath to append at the end.
Returns
New merged path.

Definition at line 228 of file path.h.

229 {
230 path res{*this};
231 res.path_type_ = right.path_type_;
232 res.append(right);
233 return res;
234 }

◆ operator|() [2/2]

path clanguml::common::model::path::operator| ( const std::string &  right) const
inline

Append path element to path.

Returns
New path.

Definition at line 248 of file path.h.

249 {
250 path res{*this};
251 res.append(right);
252 return res;
253 }

◆ operator|=() [1/2]

void clanguml::common::model::path::operator|= ( const path right)
inline

Append path to the current path.

Parameters
right

Definition at line 241 of file path.h.

241{ append(right); }

◆ operator|=() [2/2]

void clanguml::common::model::path::operator|= ( const std::string &  right)
inline

Append path element to the current path.

Parameters
rightPath element to append.

Definition at line 260 of file path.h.

260{ append(right); }

◆ parent()

std::optional< path > clanguml::common::model::path::parent ( ) const
inline

Get the parent of the last element in the path.

Returns
Path to the parent of the last element, or nullopt.

Definition at line 303 of file path.h.

304 {
305 if (size() <= 1) {
306 return {};
307 }
308
309 path res{*this};
310 res.pop_back();
311 return {std::move(res)};
312 }

◆ pop_back()

void clanguml::common::model::path::pop_back ( )
inline

Drop the last element of the path.

Definition at line 291 of file path.h.

292 {
293 if (!path_.empty()) {
294 path_.pop_back();
295 }
296 }

◆ relative()

std::string clanguml::common::model::path::relative ( const std::string &  ns) const
inline

Make path represented as a string relative to the current path.

Parameters
nsPath to make relative against *this.
Returns
Relative path.

Definition at line 378 of file path.h.

379 {
380 if (is_empty())
381 return ns;
382
383 if (ns == to_string())
384 return ns;
385
386 auto res = ns;
387 auto ns_prefix = to_string() + std::string{separator()};
388
389 auto it = res.find(ns_prefix);
390 while (it != std::string::npos) {
391 res.erase(it, ns_prefix.size());
392 it = res.find(ns_prefix);
393 }
394
395 return res;
396 }

◆ relative_to()

path clanguml::common::model::path::relative_to ( const path right) const
inline

Make the current path relative to the other path, if possible.

If not, return the original path.

Parameters
rightParent path
Returns
Path relative to right

Definition at line 362 of file path.h.

363 {
364 path res{*this};
365
366 if (res.starts_with(right))
367 util::remove_prefix(res.path_, right.path_);
368
369 return res;
370 }

◆ separator() [1/2]

const char * clanguml::common::model::path::separator ( ) const
inlineprivate

Returns the path separator based on the type of the instance path.

Returns
Path separator

Definition at line 78 of file path.h.

78{ return separator(path_type_); }

◆ separator() [2/2]

static const char * clanguml::common::model::path::separator ( path_type  pt)
inlinestaticprivate

Returns the path separator based on the path type.

Returns
Path separator

Definition at line 55 of file path.h.

56 {
57 switch (pt) {
59 return "::";
61 return ".";
63#ifdef _WIN32
64 return "\\";
65#else
66 return "/";
67#endif
68 }
69
70 return "::";
71 }

◆ size()

size_t clanguml::common::model::path::size ( ) const
inline

Return the number of elements in the path.

Returns
Size of path.

Definition at line 220 of file path.h.

220{ return path_.size(); }

◆ split()

static container_type clanguml::common::model::path::split ( const std::string &  ns,
path_type  pt = path_type::kNamespace 
)
inlinestatic

Definition at line 83 of file path.h.

85 {
86 container_type result;
87 if (pt == path_type::kModule) {
88 auto path_toks = util::split(ns, separator(pt));
89 for (const auto &pt : path_toks) {
90 const auto subtoks = util::split(pt, ":");
91 if (subtoks.size() == 2) {
92 result.push_back(subtoks.at(0));
93 result.push_back(fmt::format(":{}", subtoks.at(1)));
94 }
95 else
96 result.push_back(subtoks.at(0));
97 }
98 }
99 else
100 result = util::split(ns, separator(pt));
101
102 return result;
103 }

◆ starts_with()

bool clanguml::common::model::path::starts_with ( const path prefix) const
inline

Returns true if path starts with specified prefix.

Parameters
prefixPath prefix to check.
Returns

Definition at line 319 of file path.h.

320 {
321 return util::starts_with(path_, prefix.path_);
322 }

◆ to_string()

std::string clanguml::common::model::path::to_string ( ) const
inline

Render the path as string.

Returns
String representation of the path.

Definition at line 196 of file path.h.

197 {
198 auto result =
199 fmt::format("{}", fmt::join(path_, std::string{separator()}));
200
202 util::replace_all(result, ".:", ":");
203 }
204
205 return result;
206 }

◆ tokens()

const container_type & clanguml::common::model::path::tokens ( ) const
inline

Definition at line 429 of file path.h.

429{ return path_; }

◆ type()

path_type clanguml::common::model::path::type ( ) const
inline

Get path type.

Returns
Path type.

Definition at line 427 of file path.h.

427{ return path_type_; }

Friends And Related Symbol Documentation

◆ operator<

bool operator< ( const path left,
const path right 
)
friend

Definition at line 186 of file path.h.

187 {
188 return left.to_string() < right.to_string();
189 }

◆ operator==

bool operator== ( const path left,
const path right 
)
friend

Definition at line 181 of file path.h.

182 {
183 return left.path_ == right.path_;
184 }

Member Data Documentation

◆ path_

container_type clanguml::common::model::path::path_
private

Definition at line 433 of file path.h.

◆ path_type_

path_type clanguml::common::model::path::path_type_
private

Definition at line 432 of file path.h.


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