First public contribution.
1 // Three-state boolean logic library
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)
9 // For more information, see http://www.boost.org
10 #ifndef BOOST_LOGIC_TRIBOOL_HPP
11 #define BOOST_LOGIC_TRIBOOL_HPP
13 #include <boost/logic/tribool_fwd.hpp>
14 #include <boost/config.hpp>
15 #include <boost/detail/workaround.hpp>
17 #if BOOST_WORKAROUND(_MSC_VER, >= 1200)
21 namespace boost { namespace logic {
28 * \brief A type used only to uniquely identify the 'indeterminate'
31 struct indeterminate_t
33 #if BOOST_WORKAROUND(__BORLANDC__, < 0x0600)
34 char dummy_; // BCB would use 8 bytes by default
38 } // end namespace detail
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
46 typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
49 * \brief Keyword and test function for the indeterminate tribool value
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.
59 * \returns <tt>x.value == tribool::indeterminate_value</tt>
63 indeterminate(tribool x,
64 detail::indeterminate_t dummy = detail::indeterminate_t());
67 * \brief A 3-state boolean type.
69 * 3-state boolean values are either true, false, or
80 typedef void (dummy::*safe_bool)();
84 * Construct a new 3-state boolean value with the value 'false'.
88 tribool() : value(false_value) {}
91 * Construct a new 3-state boolean value with the given boolean
92 * value, which may be \c true or \c false.
96 tribool(bool value) : value(value? true_value : false_value) {}
99 * Construct a new 3-state boolean value with an indeterminate value.
103 tribool(indeterminate_keyword_t) : value(indeterminate_value) {}
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.
109 * \returns true if the 3-state boolean is true, false otherwise
112 operator safe_bool() const
114 return value == true_value? &dummy::nonnull : 0;
118 * The actual stored value in this 3-state boolean, which may be false, true,
121 enum value_t { false_value, true_value, indeterminate_value } value;
124 // Check if the given tribool has an indeterminate value. Also doubles as a
125 // keyword for the 'indeterminate' value
126 inline bool indeterminate(tribool x, detail::indeterminate_t)
128 return x.value == tribool::indeterminate_value;
131 /** @defgroup logical Logical operations
135 * \brief Computes the logical negation of a tribool
137 * \returns the logical negation of the tribool, according to the
141 * <th><center><code>!</code></center></th>
145 * <th><center>false</center></th>
146 * <td><center>true</center></td>
149 * <th><center>true</center></th>
150 * <td><center>false</center></td>
153 * <th><center>indeterminate</center></th>
154 * <td><center>indeterminate</center></td>
159 inline tribool operator!(tribool x)
161 return x.value == tribool::false_value? tribool(true)
162 :x.value == tribool::true_value? tribool(false)
163 :tribool(indeterminate);
167 * \brief Computes the logical conjuction of two tribools
169 * \returns the result of logically ANDing the two tribool values,
170 * according to the following table:
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>
179 * <th><center>false</center></th>
180 * <td><center>false</center></td>
181 * <td><center>false</center></td>
182 * <td><center>false</center></td>
185 * <th><center>true</center></th>
186 * <td><center>false</center></td>
187 * <td><center>true</center></td>
188 * <td><center>indeterminate</center></td>
191 * <th><center>indeterminate</center></th>
192 * <td><center>false</center></td>
193 * <td><center>indeterminate</center></td>
194 * <td><center>indeterminate</center></td>
199 inline tribool operator&&(tribool x, tribool y)
201 if (static_cast<bool>(!x) || static_cast<bool>(!y))
203 else if (static_cast<bool>(x) && static_cast<bool>(y))
206 return indeterminate;
212 inline tribool operator&&(tribool x, bool y)
213 { return y? x : tribool(false); }
218 inline tribool operator&&(bool x, tribool y)
219 { return x? y : tribool(false); }
224 inline tribool operator&&(indeterminate_keyword_t, tribool x)
225 { return !x? tribool(false) : tribool(indeterminate); }
230 inline tribool operator&&(tribool x, indeterminate_keyword_t)
231 { return !x? tribool(false) : tribool(indeterminate); }
234 * \brief Computes the logical disjunction of two tribools
236 * \returns the result of logically ORing the two tribool values,
237 * according to the following table:
240 * <th><center><code>||</code></center></th>
241 * <th><center>false</center></th>
242 * <th><center>true</center></th>
243 * <th><center>indeterminate</center></th>
246 * <th><center>false</center></th>
247 * <td><center>false</center></td>
248 * <td><center>true</center></td>
249 * <td><center>indeterminate</center></td>
252 * <th><center>true</center></th>
253 * <td><center>true</center></td>
254 * <td><center>true</center></td>
255 * <td><center>true</center></td>
258 * <th><center>indeterminate</center></th>
259 * <td><center>indeterminate</center></td>
260 * <td><center>true</center></td>
261 * <td><center>indeterminate</center></td>
266 inline tribool operator||(tribool x, tribool y)
268 if (static_cast<bool>(!x) && static_cast<bool>(!y))
270 else if (static_cast<bool>(x) || static_cast<bool>(y))
273 return indeterminate;
279 inline tribool operator||(tribool x, bool y)
280 { return y? tribool(true) : x; }
285 inline tribool operator||(bool x, tribool y)
286 { return x? tribool(true) : y; }
291 inline tribool operator||(indeterminate_keyword_t, tribool x)
292 { return x? tribool(true) : tribool(indeterminate); }
297 inline tribool operator||(tribool x, indeterminate_keyword_t)
298 { return x? tribool(true) : tribool(indeterminate); }
302 * \brief Compare tribools for equality
304 * \returns the result of comparing two tribool values, according to
305 * the following table:
308 * <th><center><code>==</code></center></th>
309 * <th><center>false</center></th>
310 * <th><center>true</center></th>
311 * <th><center>indeterminate</center></th>
314 * <th><center>false</center></th>
315 * <td><center>true</center></td>
316 * <td><center>false</center></td>
317 * <td><center>indeterminate</center></td>
320 * <th><center>true</center></th>
321 * <td><center>false</center></td>
322 * <td><center>true</center></td>
323 * <td><center>indeterminate</center></td>
326 * <th><center>indeterminate</center></th>
327 * <td><center>indeterminate</center></td>
328 * <td><center>indeterminate</center></td>
329 * <td><center>indeterminate</center></td>
334 inline tribool operator==(tribool x, tribool y)
336 if (indeterminate(x) || indeterminate(y))
337 return indeterminate;
339 return x && y || !x && !y;
345 inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
350 inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
355 inline tribool operator==(indeterminate_keyword_t, tribool x)
356 { return tribool(indeterminate) == x; }
361 inline tribool operator==(tribool x, indeterminate_keyword_t)
362 { return tribool(indeterminate) == x; }
365 * \brief Compare tribools for inequality
367 * \returns the result of comparing two tribool values for inequality,
368 * according to the following table:
371 * <th><center><code>!=</code></center></th>
372 * <th><center>false</center></th>
373 * <th><center>true</center></th>
374 * <th><center>indeterminate</center></th>
377 * <th><center>false</center></th>
378 * <td><center>false</center></td>
379 * <td><center>true</center></td>
380 * <td><center>indeterminate</center></td>
383 * <th><center>true</center></th>
384 * <td><center>true</center></td>
385 * <td><center>false</center></td>
386 * <td><center>indeterminate</center></td>
389 * <th><center>indeterminate</center></th>
390 * <td><center>indeterminate</center></td>
391 * <td><center>indeterminate</center></td>
392 * <td><center>indeterminate</center></td>
397 inline tribool operator!=(tribool x, tribool y)
399 if (indeterminate(x) || indeterminate(y))
400 return indeterminate;
402 return !(x && y || !x && !y);
408 inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
413 inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
418 inline tribool operator!=(indeterminate_keyword_t, tribool x)
419 { return tribool(indeterminate) != x; }
424 inline tribool operator!=(tribool x, indeterminate_keyword_t)
425 { return x != tribool(indeterminate); }
427 } } // end namespace boost::logic
429 // Pull tribool and indeterminate into namespace "boost"
431 using logic::tribool;
432 using logic::indeterminate;
436 * \brief Declare a new name for the third state of a tribool
438 * Use this macro to declare a new name for the third state of a
439 * tribool. This state can have any number of new names (in addition
440 * to \c indeterminate), all of which will be equivalent. The new name will be
441 * placed in the namespace in which the macro is expanded.
444 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
446 * tribool x(true_or_false);
447 * // potentially set x
448 * if (true_or_false(x)) {
449 * // don't know what x is
452 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
454 Name(boost::logic::tribool x, \
455 boost::logic::detail::indeterminate_t dummy = \
456 boost::logic::detail::indeterminate_t()) \
457 { return x.value == boost::logic::tribool::indeterminate_value; }
459 #endif // BOOST_LOGIC_TRIBOOL_HPP