[8687] | 1 | // Three-state boolean logic library |
---|
| 2 | |
---|
| 3 | // Copyright Douglas Gregor 2002-2004. Use, modification and |
---|
| 4 | // distribution is subject to the Boost Software License, Version |
---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
| 7 | |
---|
| 8 | // Modifications by Orxonox to shape the third state into 'dontcare' instead |
---|
| 9 | // of 'indeterminate'. The difference is that 'dontcare' is actually a value |
---|
| 10 | // so that (dontcare == dontcare). |
---|
| 11 | // Also removed all logic operators except for == and != |
---|
[11096] | 12 | // Added C++11 constexpr |
---|
[8687] | 13 | |
---|
| 14 | |
---|
| 15 | // For more information, see http://www.boost.org |
---|
| 16 | #ifndef ORXONOX_TRIBOOL_H |
---|
| 17 | #define ORXONOX_TRIBOOL_H |
---|
| 18 | |
---|
| 19 | namespace orxonox { |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * INTERNAL ONLY |
---|
| 23 | * The type of the 'dontcare' keyword. |
---|
| 24 | */ |
---|
[11096] | 25 | struct dontcare_keyword_t {}; |
---|
[8687] | 26 | |
---|
| 27 | /** |
---|
| 28 | * \brief Keyword for the dontcare tribool value |
---|
| 29 | */ |
---|
[11096] | 30 | constexpr dontcare_keyword_t dontcare = dontcare_keyword_t(); |
---|
[8687] | 31 | |
---|
| 32 | /** |
---|
| 33 | * \brief A 3-state boolean type. |
---|
| 34 | * |
---|
| 35 | * 3-state boolean values are either true, false, or |
---|
| 36 | * dontcare. |
---|
| 37 | */ |
---|
| 38 | class tribool |
---|
| 39 | { |
---|
| 40 | public: |
---|
| 41 | /** |
---|
| 42 | * Construct a new 3-state boolean value with the value 'false'. |
---|
| 43 | * |
---|
| 44 | * \throws nothrow |
---|
| 45 | */ |
---|
[11096] | 46 | constexpr tribool() : value(false_value) {} |
---|
[8687] | 47 | |
---|
| 48 | /** |
---|
| 49 | * Construct a new 3-state boolean value with the given boolean |
---|
| 50 | * value, which may be \c true or \c false. |
---|
| 51 | * |
---|
| 52 | * \throws nothrow |
---|
| 53 | */ |
---|
[11096] | 54 | constexpr tribool(bool value) : value(value? true_value : false_value) {} |
---|
[8687] | 55 | |
---|
| 56 | /** |
---|
| 57 | * Construct a new 3-state boolean value with an dontcare value. |
---|
| 58 | * |
---|
| 59 | * \throws nothrow |
---|
| 60 | */ |
---|
[11096] | 61 | constexpr tribool(dontcare_keyword_t) : value(dontcare_value) {} |
---|
[8687] | 62 | |
---|
| 63 | /** |
---|
| 64 | * \brief Compare tribools for equality |
---|
| 65 | * |
---|
| 66 | * \returns the result of comparing two tribool values. |
---|
| 67 | * \throws nothrow |
---|
| 68 | */ |
---|
[11096] | 69 | constexpr bool operator==(tribool y) const |
---|
[8687] | 70 | { |
---|
| 71 | return (this->value == y.value); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * \overload |
---|
| 76 | */ |
---|
[11096] | 77 | constexpr bool operator==(bool y) const { return (*this) == tribool(y); } |
---|
[8687] | 78 | |
---|
| 79 | /** |
---|
| 80 | * \overload |
---|
| 81 | */ |
---|
[11096] | 82 | constexpr bool operator==(dontcare_keyword_t) const |
---|
[8687] | 83 | { return tribool(dontcare) == (*this); } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * \brief Compare tribools for inequality |
---|
| 87 | * |
---|
| 88 | * \returns the result of comparing two tribool values for inequality. |
---|
| 89 | * \throws nothrow |
---|
| 90 | */ |
---|
[11096] | 91 | constexpr bool operator!=(tribool y) const |
---|
[8687] | 92 | { |
---|
| 93 | return !((*this) == y); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * \overload |
---|
| 98 | */ |
---|
[11096] | 99 | constexpr bool operator!=(bool y) const { return (*this) != tribool(y); } |
---|
[8687] | 100 | |
---|
| 101 | /** |
---|
| 102 | * \overload |
---|
| 103 | */ |
---|
[11096] | 104 | constexpr bool operator!=(dontcare_keyword_t) const |
---|
[8687] | 105 | { return (*this) != tribool(dontcare); } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * The actual stored value in this 3-state boolean, which may be false, true, |
---|
| 109 | * or dontcare. |
---|
| 110 | */ |
---|
| 111 | enum value_t { false_value, true_value, dontcare_value } value; |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * \overload |
---|
| 116 | */ |
---|
[11096] | 117 | constexpr bool operator==(bool x, tribool y) { return tribool(x) == y; } |
---|
[8687] | 118 | |
---|
| 119 | /** |
---|
| 120 | * \overload |
---|
| 121 | */ |
---|
[11096] | 122 | constexpr bool operator==(dontcare_keyword_t, tribool x) |
---|
[8687] | 123 | { return tribool(dontcare) == x; } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * \overload |
---|
| 127 | */ |
---|
[11096] | 128 | constexpr bool operator!=(bool x, tribool y) { return tribool(x) != y; } |
---|
[8687] | 129 | |
---|
| 130 | /** |
---|
| 131 | * \overload |
---|
| 132 | */ |
---|
[11096] | 133 | constexpr bool operator!=(dontcare_keyword_t, tribool x) |
---|
[8687] | 134 | { return tribool(dontcare) != x; } |
---|
| 135 | |
---|
| 136 | } // end namespace orxonox |
---|
| 137 | |
---|
| 138 | #endif // ORXONOX_TRIBOOL_H |
---|
| 139 | |
---|