| 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 != | 
|---|
| 12 | // Added C++11 constexpr | 
|---|
| 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 |  */ | 
|---|
| 25 | struct dontcare_keyword_t {}; | 
|---|
| 26 |  | 
|---|
| 27 | /** | 
|---|
| 28 |  * \brief Keyword for the dontcare tribool value | 
|---|
| 29 |  */ | 
|---|
| 30 | constexpr dontcare_keyword_t dontcare = dontcare_keyword_t(); | 
|---|
| 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 |    */ | 
|---|
| 46 |   constexpr tribool() : value(false_value) {} | 
|---|
| 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 |    */ | 
|---|
| 54 |   constexpr tribool(bool value) : value(value? true_value : false_value) {} | 
|---|
| 55 |  | 
|---|
| 56 |   /** | 
|---|
| 57 |    * Construct a new 3-state boolean value with an dontcare value. | 
|---|
| 58 |    * | 
|---|
| 59 |    * \throws nothrow | 
|---|
| 60 |    */ | 
|---|
| 61 |   constexpr tribool(dontcare_keyword_t) : value(dontcare_value) {} | 
|---|
| 62 |  | 
|---|
| 63 |   /** | 
|---|
| 64 |    * \brief Compare tribools for equality | 
|---|
| 65 |    * | 
|---|
| 66 |    * \returns the result of comparing two tribool values. | 
|---|
| 67 |    * \throws nothrow | 
|---|
| 68 |    */ | 
|---|
| 69 |   constexpr bool operator==(tribool y) const | 
|---|
| 70 |   { | 
|---|
| 71 |     return (this->value == y.value); | 
|---|
| 72 |   } | 
|---|
| 73 |  | 
|---|
| 74 |   /** | 
|---|
| 75 |    * \overload | 
|---|
| 76 |    */ | 
|---|
| 77 |   constexpr bool operator==(bool y) const { return (*this) == tribool(y); } | 
|---|
| 78 |  | 
|---|
| 79 |   /** | 
|---|
| 80 |    * \overload | 
|---|
| 81 |    */ | 
|---|
| 82 |   constexpr bool operator==(dontcare_keyword_t) const | 
|---|
| 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 |    */ | 
|---|
| 91 |   constexpr bool operator!=(tribool y) const | 
|---|
| 92 |   { | 
|---|
| 93 |     return !((*this) == y); | 
|---|
| 94 |   } | 
|---|
| 95 |  | 
|---|
| 96 |   /** | 
|---|
| 97 |    * \overload | 
|---|
| 98 |    */ | 
|---|
| 99 |   constexpr bool operator!=(bool y) const { return (*this) != tribool(y); } | 
|---|
| 100 |  | 
|---|
| 101 |   /** | 
|---|
| 102 |    * \overload | 
|---|
| 103 |    */ | 
|---|
| 104 |   constexpr bool operator!=(dontcare_keyword_t) const | 
|---|
| 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 |  */ | 
|---|
| 117 | constexpr bool operator==(bool x, tribool y) { return tribool(x) == y; } | 
|---|
| 118 |  | 
|---|
| 119 | /** | 
|---|
| 120 |  * \overload | 
|---|
| 121 |  */ | 
|---|
| 122 | constexpr bool operator==(dontcare_keyword_t, tribool x) | 
|---|
| 123 | { return tribool(dontcare) == x; } | 
|---|
| 124 |  | 
|---|
| 125 | /** | 
|---|
| 126 |  * \overload | 
|---|
| 127 |  */ | 
|---|
| 128 | constexpr bool operator!=(bool x, tribool y) { return tribool(x) != y; } | 
|---|
| 129 |  | 
|---|
| 130 | /** | 
|---|
| 131 |  * \overload | 
|---|
| 132 |  */ | 
|---|
| 133 | constexpr bool operator!=(dontcare_keyword_t, tribool x) | 
|---|
| 134 | { return tribool(dontcare) != x; } | 
|---|
| 135 |  | 
|---|
| 136 | } // end namespace orxonox | 
|---|
| 137 |  | 
|---|
| 138 | #endif // ORXONOX_TRIBOOL_H | 
|---|
| 139 |  | 
|---|