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 | // Forward declare tribool |
---|
21 | class tribool; |
---|
22 | |
---|
23 | /// INTERNAL ONLY |
---|
24 | namespace detail { |
---|
25 | /** |
---|
26 | * INTERNAL ONLY |
---|
27 | * |
---|
28 | * \brief A type used only to uniquely identify the 'dontcare' |
---|
29 | * function/keyword. |
---|
30 | */ |
---|
31 | struct dontcare_t |
---|
32 | { |
---|
33 | }; |
---|
34 | |
---|
35 | } // end namespace detail |
---|
36 | |
---|
37 | /** |
---|
38 | * INTERNAL ONLY |
---|
39 | * The type of the 'dontcare' keyword. This has the same type as the |
---|
40 | * function 'dontcare' so that we can recognize when the keyword is |
---|
41 | * used. |
---|
42 | */ |
---|
43 | typedef bool (*dontcare_keyword_t)(tribool, detail::dontcare_t); |
---|
44 | |
---|
45 | /** |
---|
46 | * \brief Keyword and test function for the dontcare tribool value |
---|
47 | * |
---|
48 | * The \c dontcare function has a dual role. It's first role is |
---|
49 | * as a unary function that tells whether the tribool value is in the |
---|
50 | * "dontcare" state. It's second role is as a keyword |
---|
51 | * representing the dontcare (just like "true" and "false" |
---|
52 | * represent the true and false states). |
---|
53 | * |
---|
54 | * \returns <tt>x.value == tribool::dontcare_value</tt> |
---|
55 | * \throws nothrow |
---|
56 | */ |
---|
57 | inline bool |
---|
58 | dontcare(tribool x, detail::dontcare_t dummy = detail::dontcare_t()); |
---|
59 | |
---|
60 | /** |
---|
61 | * \brief A 3-state boolean type. |
---|
62 | * |
---|
63 | * 3-state boolean values are either true, false, or |
---|
64 | * dontcare. |
---|
65 | */ |
---|
66 | class tribool |
---|
67 | { |
---|
68 | public: |
---|
69 | /** |
---|
70 | * Construct a new 3-state boolean value with the value 'false'. |
---|
71 | * |
---|
72 | * \throws nothrow |
---|
73 | */ |
---|
74 | tribool() : value(false_value) {} |
---|
75 | |
---|
76 | /** |
---|
77 | * Construct a new 3-state boolean value with the given boolean |
---|
78 | * value, which may be \c true or \c false. |
---|
79 | * |
---|
80 | * \throws nothrow |
---|
81 | */ |
---|
82 | tribool(bool value) : value(value? true_value : false_value) {} |
---|
83 | |
---|
84 | /** |
---|
85 | * Construct a new 3-state boolean value with an dontcare value. |
---|
86 | * |
---|
87 | * \throws nothrow |
---|
88 | */ |
---|
89 | tribool(dontcare_keyword_t) : value(dontcare_value) {} |
---|
90 | |
---|
91 | /** |
---|
92 | * The actual stored value in this 3-state boolean, which may be false, true, |
---|
93 | * or dontcare. |
---|
94 | */ |
---|
95 | enum value_t { false_value, true_value, dontcare_value } value; |
---|
96 | }; |
---|
97 | |
---|
98 | // Check if the given tribool has an dontcare value. Also doubles as a |
---|
99 | // keyword for the 'dontcare' value |
---|
100 | inline bool dontcare(tribool x, detail::dontcare_t) |
---|
101 | { |
---|
102 | return x.value == tribool::dontcare_value; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * \brief Compare tribools for equality |
---|
107 | * |
---|
108 | * \returns the result of comparing two tribool values, according to |
---|
109 | * the following table: |
---|
110 | * <table border=1> |
---|
111 | * <tr> |
---|
112 | * <th><center><code>==</code></center></th> |
---|
113 | * <th><center>false</center></th> |
---|
114 | * <th><center>true</center></th> |
---|
115 | * <th><center>false</center></th> |
---|
116 | * </tr> |
---|
117 | * <tr> |
---|
118 | * <th><center>false</center></th> |
---|
119 | * <td><center>true</center></td> |
---|
120 | * <td><center>false</center></td> |
---|
121 | * <td><center>false</center></td> |
---|
122 | * </tr> |
---|
123 | * <tr> |
---|
124 | * <th><center>true</center></th> |
---|
125 | * <td><center>false</center></td> |
---|
126 | * <td><center>true</center></td> |
---|
127 | * <td><center>false</center></td> |
---|
128 | * </tr> |
---|
129 | * <tr> |
---|
130 | * <th><center>dontcare</center></th> |
---|
131 | * <td><center>false</center></td> |
---|
132 | * <td><center>false</center></td> |
---|
133 | * <td><center>false</center></td> |
---|
134 | * </tr> |
---|
135 | * </table> |
---|
136 | * \throws nothrow |
---|
137 | */ |
---|
138 | inline bool operator==(tribool x, tribool y) |
---|
139 | { |
---|
140 | return (x.value == y.value); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * \overload |
---|
145 | */ |
---|
146 | inline bool operator==(tribool x, bool y) { return x == tribool(y); } |
---|
147 | |
---|
148 | /** |
---|
149 | * \overload |
---|
150 | */ |
---|
151 | inline bool operator==(bool x, tribool y) { return tribool(x) == y; } |
---|
152 | |
---|
153 | /** |
---|
154 | * \overload |
---|
155 | */ |
---|
156 | inline bool operator==(dontcare_keyword_t, tribool x) |
---|
157 | { return tribool(dontcare) == x; } |
---|
158 | |
---|
159 | /** |
---|
160 | * \overload |
---|
161 | */ |
---|
162 | inline bool operator==(tribool x, dontcare_keyword_t) |
---|
163 | { return tribool(dontcare) == x; } |
---|
164 | |
---|
165 | /** |
---|
166 | * \brief Compare tribools for inequality |
---|
167 | * |
---|
168 | * \returns the result of comparing two tribool values for inequality, |
---|
169 | * according to the following table: |
---|
170 | * <table border=1> |
---|
171 | * <tr> |
---|
172 | * <th><center><code>!=</code></center></th> |
---|
173 | * <th><center>false</center></th> |
---|
174 | * <th><center>true</center></th> |
---|
175 | * <th><center>dontcare</center></th> |
---|
176 | * </tr> |
---|
177 | * <tr> |
---|
178 | * <th><center>false</center></th> |
---|
179 | * <td><center>false</center></td> |
---|
180 | * <td><center>true</center></td> |
---|
181 | * <td><center>true</center></td> |
---|
182 | * </tr> |
---|
183 | * <tr> |
---|
184 | * <th><center>true</center></th> |
---|
185 | * <td><center>true</center></td> |
---|
186 | * <td><center>false</center></td> |
---|
187 | * <td><center>true</center></td> |
---|
188 | * </tr> |
---|
189 | * <tr> |
---|
190 | * <th><center>true</center></th> |
---|
191 | * <td><center>true</center></td> |
---|
192 | * <td><center>true</center></td> |
---|
193 | * <td><center>false</center></td> |
---|
194 | * </tr> |
---|
195 | * </table> |
---|
196 | * \throws nothrow |
---|
197 | */ |
---|
198 | inline bool operator!=(tribool x, tribool y) |
---|
199 | { |
---|
200 | return !(x == y); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * \overload |
---|
205 | */ |
---|
206 | inline bool operator!=(tribool x, bool y) { return x != tribool(y); } |
---|
207 | |
---|
208 | /** |
---|
209 | * \overload |
---|
210 | */ |
---|
211 | inline bool operator!=(bool x, tribool y) { return tribool(x) != y; } |
---|
212 | |
---|
213 | /** |
---|
214 | * \overload |
---|
215 | */ |
---|
216 | inline bool operator!=(dontcare_keyword_t, tribool x) |
---|
217 | { return tribool(dontcare) != x; } |
---|
218 | |
---|
219 | /** |
---|
220 | * \overload |
---|
221 | */ |
---|
222 | inline bool operator!=(tribool x, dontcare_keyword_t) |
---|
223 | { return x != tribool(dontcare); } |
---|
224 | |
---|
225 | } // end namespace orxonox |
---|
226 | |
---|
227 | #endif // ORXONOX_TRIBOOL_H |
---|
228 | |
---|