OGLplus  (0.59.0) a C++ wrapper for rendering APIs

tribool.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_TRIBOOL_HPP
10 #define EAGINE_TRIBOOL_HPP
11 
12 namespace eagine {
13 
14 enum class _tribool_value_t : unsigned char {
15  _false = 0x00,
16  _true = 0x01,
17  _unknown = 0x02
18 };
19 
21 struct indeterminate_t {};
22 
24 constexpr static const indeterminate_t indeterminate = {};
25 
26 class tribool;
27 
31 class weakbool {
32 private:
33  using _value_t = _tribool_value_t;
34  _value_t _value;
35 
36  friend class tribool;
37 
38  constexpr explicit weakbool(_value_t value) noexcept
39  : _value(value) {}
40 
41 public:
43  constexpr explicit operator bool() const noexcept {
44  return _value != _value_t::_false;
45  }
46 
48  constexpr auto operator!() const noexcept {
49  return _value != _value_t::_true;
50  }
51 
53  constexpr auto is(indeterminate_t) const noexcept {
54  return _value == _value_t::_unknown;
55  }
56 };
57 
61 class tribool {
62 private:
63  using _value_t = _tribool_value_t;
64  _value_t _value{_value_t::_false};
65 
66 public:
68  constexpr tribool() noexcept = default;
69 
71  constexpr tribool(bool value) noexcept
72  : _value(value ? _value_t::_true : _value_t::_false) {}
73 
75  constexpr tribool(indeterminate_t) noexcept
76  : _value(_value_t::_unknown) {}
77 
79  constexpr tribool(bool value, bool is_unknown) noexcept
80  : _value(
81  is_unknown ? _value_t::_unknown
82  : value ? _value_t::_true : _value_t::_false) {}
83 
85  constexpr explicit operator bool() const noexcept {
86  return _value == _value_t::_true;
87  }
88 
90  constexpr auto operator!() const noexcept {
91  return _value == _value_t::_false;
92  }
93 
95  constexpr auto operator*() const noexcept -> bool {
96  return _value == _value_t::_unknown;
97  }
98 
100  constexpr auto operator~() const noexcept -> weakbool {
101  return weakbool{_value};
102  }
103 
105  constexpr auto is(bool value) const noexcept -> bool {
106  return _value == (value ? _value_t::_true : _value_t::_false);
107  }
108 
110  constexpr auto is(indeterminate_t) const noexcept -> bool {
111  return *(*this);
112  }
113 
115  friend constexpr auto operator==(tribool a, tribool b) noexcept -> tribool {
116  return {a._value == b._value, (*a || *b)};
117  }
118 
120  friend constexpr auto operator!=(tribool a, tribool b) noexcept -> tribool {
121  return {a._value != b._value, (*a || *b)};
122  }
123 };
124 
126 constexpr auto operator&&(tribool a, tribool b) noexcept {
127  return !a ? tribool{false}
128  : a ? b : !b ? tribool{false} : tribool{indeterminate};
129 }
130 
132 constexpr auto operator||(tribool a, tribool b) noexcept {
133  return a ? tribool{true}
134  : !a ? b : b ? tribool{true} : tribool{indeterminate};
135 }
136 
137 } // namespace eagine
138 
139 #endif // EAGINE_TRIBOOL_HPP
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr tribool(bool value, bool is_unknown) noexcept
Construction with separate true/false, known/unknown arguments.
Definition: tribool.hpp:79
constexpr static const indeterminate_t indeterminate
Constant representing unspecified tribool value.
Definition: tribool.hpp:24
Tri-state boolean value.
Definition: tribool.hpp:61
constexpr auto operator&&(tribool a, tribool b) noexcept
Tri-state boolean and operator.
Definition: tribool.hpp:126
constexpr auto operator!() const noexcept
Returns true, if the stored value is false.
Definition: tribool.hpp:90
Type of the indeterminate constant assignable to tribool.
Definition: tribool.hpp:21
Weak tri-state boolean value.
Definition: tribool.hpp:31
constexpr auto is(indeterminate_t) const noexcept
@ brief Checks if the stored value is indeterminate.
Definition: tribool.hpp:53
constexpr auto operator~() const noexcept -> weakbool
Converts this value to weakbool.
Definition: tribool.hpp:100
constexpr tribool(indeterminate_t) noexcept
Constructions from indeterminate value.
Definition: tribool.hpp:75
constexpr auto operator||(tribool a, tribool b) noexcept
Tri-state boolean or operator.
Definition: tribool.hpp:132
constexpr auto operator!() const noexcept
Returns true, if the stored value is not true.
Definition: tribool.hpp:48
constexpr tribool(bool value) noexcept
Constructions from boolean value.
Definition: tribool.hpp:71
constexpr tribool() noexcept=default
Default constructor.
constexpr auto is(bool value) const noexcept -> bool
Returns true if the stored value is known and equal to value.
Definition: tribool.hpp:105
constexpr auto is(indeterminate_t) const noexcept -> bool
Returns true if the stored value is indeterminate.
Definition: tribool.hpp:110
constexpr friend auto operator==(tribool a, tribool b) noexcept -> tribool
Equality comparison.
Definition: tribool.hpp:115
constexpr auto operator*() const noexcept -> bool
Returns true, if the stored value is indeterminate.
Definition: tribool.hpp:95
constexpr friend auto operator!=(tribool a, tribool b) noexcept -> tribool
Non-equality comparison.
Definition: tribool.hpp:120

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).