| 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 |  | 
|---|
| 13 |  | 
|---|
| 14 | // For more information, see http://www.boost.org | 
|---|
| 15 | #ifndef ORXONOX_TRIBOOL_H | 
|---|
| 16 | #define ORXONOX_TRIBOOL_H | 
|---|
| 17 |  | 
|---|
| 18 | namespace orxonox { | 
|---|
| 19 |  | 
|---|
| 20 | /** | 
|---|
| 21 | * INTERNAL ONLY | 
|---|
| 22 | * The type of the 'dontcare' keyword. | 
|---|
| 23 | */ | 
|---|
| 24 | struct dontcare_keyword_t { }; | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 27 | * \brief Keyword for the dontcare tribool value | 
|---|
| 28 | */ | 
|---|
| 29 | const dontcare_keyword_t dontcare = dontcare_keyword_t(); | 
|---|
| 30 |  | 
|---|
| 31 | /** | 
|---|
| 32 | * \brief A 3-state boolean type. | 
|---|
| 33 | * | 
|---|
| 34 | * 3-state boolean values are either true, false, or | 
|---|
| 35 | * dontcare. | 
|---|
| 36 | */ | 
|---|
| 37 | class tribool | 
|---|
| 38 | { | 
|---|
| 39 | public: | 
|---|
| 40 | /** | 
|---|
| 41 | * Construct a new 3-state boolean value with the value 'false'. | 
|---|
| 42 | * | 
|---|
| 43 | * \throws nothrow | 
|---|
| 44 | */ | 
|---|
| 45 | tribool() : value(false_value) {} | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 | * Construct a new 3-state boolean value with the given boolean | 
|---|
| 49 | * value, which may be \c true or \c false. | 
|---|
| 50 | * | 
|---|
| 51 | * \throws nothrow | 
|---|
| 52 | */ | 
|---|
| 53 | tribool(bool value) : value(value? true_value : false_value) {} | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 56 | * Construct a new 3-state boolean value with an dontcare value. | 
|---|
| 57 | * | 
|---|
| 58 | * \throws nothrow | 
|---|
| 59 | */ | 
|---|
| 60 | tribool(dontcare_keyword_t) : value(dontcare_value) {} | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 | * \brief Compare tribools for equality | 
|---|
| 64 | * | 
|---|
| 65 | * \returns the result of comparing two tribool values. | 
|---|
| 66 | * \throws nothrow | 
|---|
| 67 | */ | 
|---|
| 68 | inline bool operator==(tribool y) | 
|---|
| 69 | { | 
|---|
| 70 | return (this->value == y.value); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | /** | 
|---|
| 74 | * \overload | 
|---|
| 75 | */ | 
|---|
| 76 | inline bool operator==(bool y) { return (*this) == tribool(y); } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | * \overload | 
|---|
| 80 | */ | 
|---|
| 81 | inline bool operator==(dontcare_keyword_t) | 
|---|
| 82 | { return tribool(dontcare) == (*this); } | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * \brief Compare tribools for inequality | 
|---|
| 86 | * | 
|---|
| 87 | * \returns the result of comparing two tribool values for inequality. | 
|---|
| 88 | * \throws nothrow | 
|---|
| 89 | */ | 
|---|
| 90 | inline bool operator!=(tribool y) | 
|---|
| 91 | { | 
|---|
| 92 | return !((*this) == y); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /** | 
|---|
| 96 | * \overload | 
|---|
| 97 | */ | 
|---|
| 98 | inline bool operator!=(bool y) { return (*this) != tribool(y); } | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * \overload | 
|---|
| 102 | */ | 
|---|
| 103 | inline bool operator!=(dontcare_keyword_t) | 
|---|
| 104 | { return (*this) != tribool(dontcare); } | 
|---|
| 105 |  | 
|---|
| 106 | /** | 
|---|
| 107 | * The actual stored value in this 3-state boolean, which may be false, true, | 
|---|
| 108 | * or dontcare. | 
|---|
| 109 | */ | 
|---|
| 110 | enum value_t { false_value, true_value, dontcare_value } value; | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | /** | 
|---|
| 114 | * \overload | 
|---|
| 115 | */ | 
|---|
| 116 | inline bool operator==(bool x, tribool y) { return tribool(x) == y; } | 
|---|
| 117 |  | 
|---|
| 118 | /** | 
|---|
| 119 | * \overload | 
|---|
| 120 | */ | 
|---|
| 121 | inline bool operator==(dontcare_keyword_t, tribool x) | 
|---|
| 122 | { return tribool(dontcare) == x; } | 
|---|
| 123 |  | 
|---|
| 124 | /** | 
|---|
| 125 | * \overload | 
|---|
| 126 | */ | 
|---|
| 127 | inline bool operator!=(bool x, tribool y) { return tribool(x) != y; } | 
|---|
| 128 |  | 
|---|
| 129 | /** | 
|---|
| 130 | * \overload | 
|---|
| 131 | */ | 
|---|
| 132 | inline bool operator!=(dontcare_keyword_t, tribool x) | 
|---|
| 133 | { return tribool(dontcare) != x; } | 
|---|
| 134 |  | 
|---|
| 135 | } // end namespace orxonox | 
|---|
| 136 |  | 
|---|
| 137 | #endif // ORXONOX_TRIBOOL_H | 
|---|
| 138 |  | 
|---|