]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
9 | // For more information, see http://www.boost.org | |
10 | #ifndef BOOST_LOGIC_TRIBOOL_HPP | |
11 | #define BOOST_LOGIC_TRIBOOL_HPP | |
12 | ||
13 | #include <boost/logic/tribool_fwd.hpp> | |
14 | #include <boost/config.hpp> | |
15 | #include <boost/detail/workaround.hpp> | |
16 | ||
17 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
18 | # pragma once | |
19 | #endif | |
20 | ||
21 | namespace boost { namespace logic { | |
22 | ||
23 | /// INTERNAL ONLY | |
24 | namespace detail { | |
25 | /** | |
26 | * INTERNAL ONLY | |
27 | * | |
28 | * \brief A type used only to uniquely identify the 'indeterminate' | |
29 | * function/keyword. | |
30 | */ | |
31 | struct indeterminate_t | |
32 | { | |
33 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x0600) | |
34 | char dummy_; // BCB would use 8 bytes by default | |
35 | #endif | |
36 | }; | |
37 | ||
38 | } // end namespace detail | |
39 | ||
40 | /** | |
41 | * INTERNAL ONLY | |
42 | * The type of the 'indeterminate' keyword. This has the same type as the | |
43 | * function 'indeterminate' so that we can recognize when the keyword is | |
44 | * used. | |
45 | */ | |
46 | typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t); | |
47 | ||
48 | /** | |
49 | * \brief Keyword and test function for the indeterminate tribool value | |
50 | * | |
51 | * The \c indeterminate function has a dual role. It's first role is | |
52 | * as a unary function that tells whether the tribool value is in the | |
53 | * "indeterminate" state. It's second role is as a keyword | |
54 | * representing the indeterminate (just like "true" and "false" | |
55 | * represent the true and false states). If you do not like the name | |
56 | * "indeterminate", and would prefer to use a different name, see the | |
57 | * macro \c BOOST_TRIBOOL_THIRD_STATE. | |
58 | * | |
59 | * \returns <tt>x.value == tribool::indeterminate_value</tt> | |
60 | * \throws nothrow | |
61 | */ | |
62 | BOOST_CONSTEXPR inline bool | |
63 | indeterminate(tribool x, | |
64 | detail::indeterminate_t dummy = detail::indeterminate_t()) BOOST_NOEXCEPT; | |
65 | ||
66 | /** | |
67 | * \brief A 3-state boolean type. | |
68 | * | |
69 | * 3-state boolean values are either true, false, or | |
70 | * indeterminate. | |
71 | */ | |
72 | class tribool | |
73 | { | |
74 | private: | |
75 | /// INTERNAL ONLY | |
76 | struct dummy { | |
77 | void nonnull() {}; | |
78 | }; | |
79 | ||
80 | typedef void (dummy::*safe_bool)(); | |
81 | ||
82 | public: | |
83 | /** | |
84 | * Construct a new 3-state boolean value with the value 'false'. | |
85 | * | |
86 | * \throws nothrow | |
87 | */ | |
88 | BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : value(false_value) {} | |
89 | ||
90 | /** | |
91 | * Construct a new 3-state boolean value with the given boolean | |
92 | * value, which may be \c true or \c false. | |
93 | * | |
94 | * \throws nothrow | |
95 | */ | |
96 | BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {} | |
97 | ||
98 | /** | |
99 | * Construct a new 3-state boolean value with an indeterminate value. | |
100 | * | |
101 | * \throws nothrow | |
102 | */ | |
103 | BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : value(indeterminate_value) {} | |
104 | ||
105 | /** | |
106 | * Use a 3-state boolean in a boolean context. Will evaluate true in a | |
107 | * boolean context only when the 3-state boolean is definitely true. | |
108 | * | |
109 | * \returns true if the 3-state boolean is true, false otherwise | |
110 | * \throws nothrow | |
111 | */ | |
112 | BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT | |
113 | { | |
114 | return value == true_value? &dummy::nonnull : 0; | |
115 | } | |
116 | ||
117 | /** | |
118 | * The actual stored value in this 3-state boolean, which may be false, true, | |
119 | * or indeterminate. | |
120 | */ | |
121 | enum value_t { false_value, true_value, indeterminate_value } value; | |
122 | }; | |
123 | ||
124 | // Check if the given tribool has an indeterminate value. Also doubles as a | |
125 | // keyword for the 'indeterminate' value | |
126 | BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT | |
127 | { | |
128 | return x.value == tribool::indeterminate_value; | |
129 | } | |
130 | ||
131 | /** @defgroup logical Logical operations | |
132 | */ | |
133 | //@{ | |
134 | /** | |
135 | * \brief Computes the logical negation of a tribool | |
136 | * | |
137 | * \returns the logical negation of the tribool, according to the | |
138 | * table: | |
139 | * <table border=1> | |
140 | * <tr> | |
141 | * <th><center><code>!</code></center></th> | |
142 | * <th/> | |
143 | * </tr> | |
144 | * <tr> | |
145 | * <th><center>false</center></th> | |
146 | * <td><center>true</center></td> | |
147 | * </tr> | |
148 | * <tr> | |
149 | * <th><center>true</center></th> | |
150 | * <td><center>false</center></td> | |
151 | * </tr> | |
152 | * <tr> | |
153 | * <th><center>indeterminate</center></th> | |
154 | * <td><center>indeterminate</center></td> | |
155 | * </tr> | |
156 | * </table> | |
157 | * \throws nothrow | |
158 | */ | |
159 | BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT | |
160 | { | |
161 | return x.value == tribool::false_value? tribool(true) | |
162 | :x.value == tribool::true_value? tribool(false) | |
163 | :tribool(indeterminate); | |
164 | } | |
165 | ||
166 | /** | |
167 | * \brief Computes the logical conjuction of two tribools | |
168 | * | |
169 | * \returns the result of logically ANDing the two tribool values, | |
170 | * according to the following table: | |
171 | * <table border=1> | |
172 | * <tr> | |
173 | * <th><center><code>&&</code></center></th> | |
174 | * <th><center>false</center></th> | |
175 | * <th><center>true</center></th> | |
176 | * <th><center>indeterminate</center></th> | |
177 | * </tr> | |
178 | * <tr> | |
179 | * <th><center>false</center></th> | |
180 | * <td><center>false</center></td> | |
181 | * <td><center>false</center></td> | |
182 | * <td><center>false</center></td> | |
183 | * </tr> | |
184 | * <tr> | |
185 | * <th><center>true</center></th> | |
186 | * <td><center>false</center></td> | |
187 | * <td><center>true</center></td> | |
188 | * <td><center>indeterminate</center></td> | |
189 | * </tr> | |
190 | * <tr> | |
191 | * <th><center>indeterminate</center></th> | |
192 | * <td><center>false</center></td> | |
193 | * <td><center>indeterminate</center></td> | |
194 | * <td><center>indeterminate</center></td> | |
195 | * </tr> | |
196 | * </table> | |
197 | * \throws nothrow | |
198 | */ | |
199 | BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT | |
200 | { | |
201 | return (static_cast<bool>(!x) || static_cast<bool>(!y)) | |
202 | ? tribool(false) | |
203 | : ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate) | |
204 | ; | |
205 | } | |
206 | ||
207 | /** | |
208 | * \overload | |
209 | */ | |
210 | BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT | |
211 | { return y? x : tribool(false); } | |
212 | ||
213 | /** | |
214 | * \overload | |
215 | */ | |
216 | BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT | |
217 | { return x? y : tribool(false); } | |
218 | ||
219 | /** | |
220 | * \overload | |
221 | */ | |
222 | BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT | |
223 | { return !x? tribool(false) : tribool(indeterminate); } | |
224 | ||
225 | /** | |
226 | * \overload | |
227 | */ | |
228 | BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT | |
229 | { return !x? tribool(false) : tribool(indeterminate); } | |
230 | ||
231 | /** | |
232 | * \brief Computes the logical disjunction of two tribools | |
233 | * | |
234 | * \returns the result of logically ORing the two tribool values, | |
235 | * according to the following table: | |
236 | * <table border=1> | |
237 | * <tr> | |
238 | * <th><center><code>||</code></center></th> | |
239 | * <th><center>false</center></th> | |
240 | * <th><center>true</center></th> | |
241 | * <th><center>indeterminate</center></th> | |
242 | * </tr> | |
243 | * <tr> | |
244 | * <th><center>false</center></th> | |
245 | * <td><center>false</center></td> | |
246 | * <td><center>true</center></td> | |
247 | * <td><center>indeterminate</center></td> | |
248 | * </tr> | |
249 | * <tr> | |
250 | * <th><center>true</center></th> | |
251 | * <td><center>true</center></td> | |
252 | * <td><center>true</center></td> | |
253 | * <td><center>true</center></td> | |
254 | * </tr> | |
255 | * <tr> | |
256 | * <th><center>indeterminate</center></th> | |
257 | * <td><center>indeterminate</center></td> | |
258 | * <td><center>true</center></td> | |
259 | * <td><center>indeterminate</center></td> | |
260 | * </tr> | |
261 | * </table> | |
262 | * \throws nothrow | |
263 | */ | |
264 | BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT | |
265 | { | |
266 | return (static_cast<bool>(!x) && static_cast<bool>(!y)) | |
267 | ? tribool(false) | |
268 | : ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate)) | |
269 | ; | |
270 | } | |
271 | ||
272 | /** | |
273 | * \overload | |
274 | */ | |
275 | BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT | |
276 | { return y? tribool(true) : x; } | |
277 | ||
278 | /** | |
279 | * \overload | |
280 | */ | |
281 | BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT | |
282 | { return x? tribool(true) : y; } | |
283 | ||
284 | /** | |
285 | * \overload | |
286 | */ | |
287 | BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT | |
288 | { return x? tribool(true) : tribool(indeterminate); } | |
289 | ||
290 | /** | |
291 | * \overload | |
292 | */ | |
293 | BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT | |
294 | { return x? tribool(true) : tribool(indeterminate); } | |
295 | //@} | |
296 | ||
297 | /** | |
298 | * \brief Compare tribools for equality | |
299 | * | |
300 | * \returns the result of comparing two tribool values, according to | |
301 | * the following table: | |
302 | * <table border=1> | |
303 | * <tr> | |
304 | * <th><center><code>==</code></center></th> | |
305 | * <th><center>false</center></th> | |
306 | * <th><center>true</center></th> | |
307 | * <th><center>indeterminate</center></th> | |
308 | * </tr> | |
309 | * <tr> | |
310 | * <th><center>false</center></th> | |
311 | * <td><center>true</center></td> | |
312 | * <td><center>false</center></td> | |
313 | * <td><center>indeterminate</center></td> | |
314 | * </tr> | |
315 | * <tr> | |
316 | * <th><center>true</center></th> | |
317 | * <td><center>false</center></td> | |
318 | * <td><center>true</center></td> | |
319 | * <td><center>indeterminate</center></td> | |
320 | * </tr> | |
321 | * <tr> | |
322 | * <th><center>indeterminate</center></th> | |
323 | * <td><center>indeterminate</center></td> | |
324 | * <td><center>indeterminate</center></td> | |
325 | * <td><center>indeterminate</center></td> | |
326 | * </tr> | |
327 | * </table> | |
328 | * \throws nothrow | |
329 | */ | |
330 | BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT | |
331 | { | |
332 | return (indeterminate(x) || indeterminate(y)) | |
333 | ? indeterminate | |
334 | : ((x && y) || (!x && !y)) | |
335 | ; | |
336 | } | |
337 | ||
338 | /** | |
339 | * \overload | |
340 | */ | |
341 | BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); } | |
342 | ||
343 | /** | |
344 | * \overload | |
345 | */ | |
346 | BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; } | |
347 | ||
348 | /** | |
349 | * \overload | |
350 | */ | |
351 | BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT | |
352 | { return tribool(indeterminate) == x; } | |
353 | ||
354 | /** | |
355 | * \overload | |
356 | */ | |
357 | BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT | |
358 | { return tribool(indeterminate) == x; } | |
359 | ||
360 | /** | |
361 | * \brief Compare tribools for inequality | |
362 | * | |
363 | * \returns the result of comparing two tribool values for inequality, | |
364 | * according to the following table: | |
365 | * <table border=1> | |
366 | * <tr> | |
367 | * <th><center><code>!=</code></center></th> | |
368 | * <th><center>false</center></th> | |
369 | * <th><center>true</center></th> | |
370 | * <th><center>indeterminate</center></th> | |
371 | * </tr> | |
372 | * <tr> | |
373 | * <th><center>false</center></th> | |
374 | * <td><center>false</center></td> | |
375 | * <td><center>true</center></td> | |
376 | * <td><center>indeterminate</center></td> | |
377 | * </tr> | |
378 | * <tr> | |
379 | * <th><center>true</center></th> | |
380 | * <td><center>true</center></td> | |
381 | * <td><center>false</center></td> | |
382 | * <td><center>indeterminate</center></td> | |
383 | * </tr> | |
384 | * <tr> | |
385 | * <th><center>indeterminate</center></th> | |
386 | * <td><center>indeterminate</center></td> | |
387 | * <td><center>indeterminate</center></td> | |
388 | * <td><center>indeterminate</center></td> | |
389 | * </tr> | |
390 | * </table> | |
391 | * \throws nothrow | |
392 | */ | |
393 | BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT | |
394 | { | |
395 | return (indeterminate(x) || indeterminate(y)) | |
396 | ? indeterminate | |
397 | : !((x && y) || (!x && !y)) | |
398 | ; | |
399 | } | |
400 | ||
401 | /** | |
402 | * \overload | |
403 | */ | |
404 | BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); } | |
405 | ||
406 | /** | |
407 | * \overload | |
408 | */ | |
409 | BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; } | |
410 | ||
411 | /** | |
412 | * \overload | |
413 | */ | |
414 | BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT | |
415 | { return tribool(indeterminate) != x; } | |
416 | ||
417 | /** | |
418 | * \overload | |
419 | */ | |
420 | BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT | |
421 | { return x != tribool(indeterminate); } | |
422 | ||
423 | } } // end namespace boost::logic | |
424 | ||
425 | // Pull tribool and indeterminate into namespace "boost" | |
426 | namespace boost { | |
427 | using logic::tribool; | |
428 | using logic::indeterminate; | |
429 | } | |
430 | ||
431 | /** | |
432 | * \brief Declare a new name for the third state of a tribool | |
433 | * | |
434 | * Use this macro to declare a new name for the third state of a | |
435 | * tribool. This state can have any number of new names (in addition | |
436 | * to \c indeterminate), all of which will be equivalent. The new name will be | |
437 | * placed in the namespace in which the macro is expanded. | |
438 | * | |
439 | * Example: | |
440 | * BOOST_TRIBOOL_THIRD_STATE(true_or_false) | |
441 | * | |
442 | * tribool x(true_or_false); | |
443 | * // potentially set x | |
444 | * if (true_or_false(x)) { | |
445 | * // don't know what x is | |
446 | * } | |
447 | */ | |
448 | #define BOOST_TRIBOOL_THIRD_STATE(Name) \ | |
449 | inline bool \ | |
450 | Name(boost::logic::tribool x, \ | |
451 | boost::logic::detail::indeterminate_t = \ | |
452 | boost::logic::detail::indeterminate_t()) \ | |
453 | { return x.value == boost::logic::tribool::indeterminate_value; } | |
454 | ||
455 | #endif // BOOST_LOGIC_TRIBOOL_HPP | |
456 |