0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
types.cc
Go to the documentation of this file.
1/**
2 * @file src/common/types.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 "types.h"
20
21namespace clanguml::common {
22
24 : value_{0ULL}
25 , is_global_{true}
26{
27}
28
29eid_t::eid_t(int64_t id)
30 : value_{static_cast<type>(id)}
31 , is_global_{false}
32{
33}
34
36 : value_{id}
37 , is_global_{true}
38{
39}
40
41eid_t &eid_t::operator=(int64_t ast_id)
42{
43 // Can't assign ast_id if the id is already a global one
44 assert(!is_global_);
45
46 value_ = static_cast<uint64_t>(ast_id);
47 return *this;
48}
49
50bool eid_t::is_global() const { return is_global_; }
51
52bool operator==(const eid_t &lhs, const eid_t &rhs)
53{
54 return (lhs.is_global_ == rhs.is_global_) && (lhs.value_ == rhs.value_);
55}
56
57bool operator==(const eid_t &lhs, const uint64_t &v) { return lhs.value_ == v; }
58
59bool operator!=(const eid_t &lhs, const uint64_t &v)
60{
61 // This is sadly necessary to catch accidental comparisons to empty
62 // std::optional<id_t>:
63 //
64 // std::optional<id_t> id{};
65 // if(id != 0) { /* id is nullopt, not 0 - so this executes... */ }
66 //
67 assert(v != 0);
68
69 return lhs.value_ != v;
70}
71
72bool operator!=(const eid_t &lhs, const eid_t &rhs) { return !(lhs == rhs); }
73
74bool operator<(const eid_t &lhs, const eid_t &rhs)
75{
76 if (lhs.is_global_ != rhs.is_global_) {
77 return lhs.value_ < rhs.value_ + 1;
78 }
79
80 return lhs.value_ < rhs.value_; // Compare values if is_global_ are the same
81}
82
83eid_t::type eid_t::value() const { return value_; }
84
86{
87 assert(!is_global_);
88
89 return static_cast<int64_t>(value_);
90}
91
92std::string to_string(const std::string &s) { return s; }
93
94std::string to_string(const string_or_regex &sr) { return sr.to_string(); }
95
96std::string to_string(const generator_type_t type)
97{
98 switch (type) {
100 return "plantuml";
102 return "mermaid";
104 return "json";
105 default:
106 return "<unknown>";
107 }
108}
109} // namespace clanguml::common