0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
tvl.h
Go to the documentation of this file.
1/**
2 * @file src/common/model/tvl.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 <optional>
21#include <string>
22
23/**
24 * This namespace implements convenience functions to handle 3-value logic
25 * operations needed for applying diagram filters.
26 *
27 * @see clanguml::common::model::diagram_filter
28 * @see clanguml::common::model::filter_visitor
29 */
31
32/**
33 * Alias for 3-value logic values
34 *
35 * If optional holds nullopt, the value is undefined.
36 */
37using value_t = std::optional<bool>;
38
39/**
40 * Calculate 3-value logic AND value.
41 *
42 * @param l Left value
43 * @param r Right value
44 * @return Result of AND operation.
45 */
46inline value_t and_(const value_t &l, const value_t &r)
47{
48 if (!l.has_value())
49 return r;
50
51 if (!r.has_value())
52 return l;
53
54 return r.value() && l.value();
55}
56
57/**
58 * Calculate 3-value logic OR value.
59 *
60 * @param l Left value
61 * @param r Right value
62 * @return Result of OR operation.
63 */
64inline value_t or_(const value_t &l, const value_t &r)
65{
66 if (!l.has_value() && !r.has_value())
67 return {};
68
69 if (l.has_value() && l.value())
70 return true;
71
72 if (r.has_value() && r.value())
73 return true;
74
75 return false;
76}
77
78/**
79 * Whether the value holds true
80 *
81 * @param v Logical value
82 * @return True, if v holds true
83 */
84inline bool is_true(const value_t &v) { return v.has_value() && v.value(); }
85
86/**
87 * Whether the value holds false
88 *
89 * @param v Logical value
90 * @return True, if v holds false
91 */
92inline bool is_false(const value_t &v) { return v.has_value() && !v.value(); }
93
94/**
95 * Whether the value is undefined
96 *
97 * @param v Logical value
98 * @return True, if v has no value
99 */
100inline bool is_undefined(const value_t &v) { return !v.has_value(); }
101
102/**
103 * 3-value logic equivalent of std::all_of
104 *
105 * @tparam InputIterator Iterator type
106 * @tparam Predicate Predicate type
107 * @param first First iterator element
108 * @param last Last iterator element
109 * @param pred Predicate to apply to each element
110 * @return True, if all elements are true or undefined
111 */
112template <typename InputIterator, typename Predicate>
113inline value_t all_of(InputIterator first, InputIterator last, Predicate pred)
114{
115 value_t res{};
116
117 for (InputIterator it = first; it != last; it++) {
118 value_t m = pred(*it);
119 if (m.has_value()) {
120 if (m.value()) {
121 res = true;
122 }
123 else {
124 res = false;
125 break;
126 }
127 }
128 }
129
130 return res;
131}
132
133/**
134 * 3-value logic equivalent of std::any_of
135 *
136 * @tparam InputIterator Iterator type
137 * @tparam Predicate Predicate type
138 * @param first First iterator element
139 * @param last Last iterator element
140 * @param pred Predicate to apply to each element
141 * @return True, if at least 1 element is true
142 */
143template <typename InputIterator, typename Predicate>
144inline value_t any_of(InputIterator first, InputIterator last, Predicate pred)
145{
146 value_t res{};
147
148 for (InputIterator it = first; it != last; it++) {
149 value_t m = pred(*it);
150 if (m.has_value()) {
151 if (m.value()) {
152 res = true;
153 return res;
154 }
155 res = false;
156 }
157 }
158
159 return res;
160}
161} // namespace clanguml::common::model::tvl