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 #ifdef BOOST_HAS_PRAGMA_ONCE
21 namespace boost { namespace logic {
28 * \brief A type used only to uniquely identify the 'indeterminate'
31 struct indeterminate_t
33 #if BOOST_WORKAROUND(BOOST_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>
62 BOOST_CONSTEXPR inline bool
63 indeterminate(tribool x,
64 detail::indeterminate_t dummy = detail::indeterminate_t()) BOOST_NOEXCEPT;
67 * \brief A 3-state boolean type.
69 * 3-state boolean values are either true, false, or
74 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
81 typedef void (dummy::*safe_bool)();
86 * Construct a new 3-state boolean value with the value 'false'.
90 BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : value(false_value) {}
93 * Construct a new 3-state boolean value with the given boolean
94 * value, which may be \c true or \c false.
98 BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {}
101 * Construct a new 3-state boolean value with an indeterminate value.
105 BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : value(indeterminate_value) {}
108 * Use a 3-state boolean in a boolean context. Will evaluate true in a
109 * boolean context only when the 3-state boolean is definitely true.
111 * \returns true if the 3-state boolean is true, false otherwise
114 #if !defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
116 BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT
118 return value == true_value;
123 BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT
125 return value == true_value? &dummy::nonnull : 0;
131 * The actual stored value in this 3-state boolean, which may be false, true,
134 enum value_t { false_value, true_value, indeterminate_value } value;
137 // Check if the given tribool has an indeterminate value. Also doubles as a
138 // keyword for the 'indeterminate' value
139 BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT
141 return x.value == tribool::indeterminate_value;
144 /** @defgroup logical Logical operations
148 * \brief Computes the logical negation of a tribool
150 * \returns the logical negation of the tribool, according to the
154 * <th><center><code>!</code></center></th>
158 * <th><center>false</center></th>
159 * <td><center>true</center></td>
162 * <th><center>true</center></th>
163 * <td><center>false</center></td>
166 * <th><center>indeterminate</center></th>
167 * <td><center>indeterminate</center></td>
172 BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT
174 return x.value == tribool::false_value? tribool(true)
175 :x.value == tribool::true_value? tribool(false)
176 :tribool(indeterminate);
180 * \brief Computes the logical conjunction of two tribools
182 * \returns the result of logically ANDing the two tribool values,
183 * according to the following table:
186 * <th><center><code>&&</code></center></th>
187 * <th><center>false</center></th>
188 * <th><center>true</center></th>
189 * <th><center>indeterminate</center></th>
192 * <th><center>false</center></th>
193 * <td><center>false</center></td>
194 * <td><center>false</center></td>
195 * <td><center>false</center></td>
198 * <th><center>true</center></th>
199 * <td><center>false</center></td>
200 * <td><center>true</center></td>
201 * <td><center>indeterminate</center></td>
204 * <th><center>indeterminate</center></th>
205 * <td><center>false</center></td>
206 * <td><center>indeterminate</center></td>
207 * <td><center>indeterminate</center></td>
212 BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT
214 return (static_cast<bool>(!x) || static_cast<bool>(!y))
216 : ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate)
223 BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT
224 { return y? x : tribool(false); }
229 BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT
230 { return x? y : tribool(false); }
235 BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
236 { return !x? tribool(false) : tribool(indeterminate); }
241 BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
242 { return !x? tribool(false) : tribool(indeterminate); }
245 * \brief Computes the logical disjunction of two tribools
247 * \returns the result of logically ORing the two tribool values,
248 * according to the following table:
251 * <th><center><code>||</code></center></th>
252 * <th><center>false</center></th>
253 * <th><center>true</center></th>
254 * <th><center>indeterminate</center></th>
257 * <th><center>false</center></th>
258 * <td><center>false</center></td>
259 * <td><center>true</center></td>
260 * <td><center>indeterminate</center></td>
263 * <th><center>true</center></th>
264 * <td><center>true</center></td>
265 * <td><center>true</center></td>
266 * <td><center>true</center></td>
269 * <th><center>indeterminate</center></th>
270 * <td><center>indeterminate</center></td>
271 * <td><center>true</center></td>
272 * <td><center>indeterminate</center></td>
277 BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT
279 return (static_cast<bool>(!x) && static_cast<bool>(!y))
281 : ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate))
288 BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT
289 { return y? tribool(true) : x; }
294 BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT
295 { return x? tribool(true) : y; }
300 BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
301 { return x? tribool(true) : tribool(indeterminate); }
306 BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
307 { return x? tribool(true) : tribool(indeterminate); }
311 * \brief Compare tribools for equality
313 * \returns the result of comparing two tribool values, according to
314 * the following table:
317 * <th><center><code>==</code></center></th>
318 * <th><center>false</center></th>
319 * <th><center>true</center></th>
320 * <th><center>indeterminate</center></th>
323 * <th><center>false</center></th>
324 * <td><center>true</center></td>
325 * <td><center>false</center></td>
326 * <td><center>indeterminate</center></td>
329 * <th><center>true</center></th>
330 * <td><center>false</center></td>
331 * <td><center>true</center></td>
332 * <td><center>indeterminate</center></td>
335 * <th><center>indeterminate</center></th>
336 * <td><center>indeterminate</center></td>
337 * <td><center>indeterminate</center></td>
338 * <td><center>indeterminate</center></td>
343 BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT
345 return (indeterminate(x) || indeterminate(y))
347 : ((x && y) || (!x && !y))
354 BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); }
359 BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; }
364 BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
365 { return tribool(indeterminate) == x; }
370 BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
371 { return tribool(indeterminate) == x; }
374 * \brief Compare tribools for inequality
376 * \returns the result of comparing two tribool values for inequality,
377 * according to the following table:
380 * <th><center><code>!=</code></center></th>
381 * <th><center>false</center></th>
382 * <th><center>true</center></th>
383 * <th><center>indeterminate</center></th>
386 * <th><center>false</center></th>
387 * <td><center>false</center></td>
388 * <td><center>true</center></td>
389 * <td><center>indeterminate</center></td>
392 * <th><center>true</center></th>
393 * <td><center>true</center></td>
394 * <td><center>false</center></td>
395 * <td><center>indeterminate</center></td>
398 * <th><center>indeterminate</center></th>
399 * <td><center>indeterminate</center></td>
400 * <td><center>indeterminate</center></td>
401 * <td><center>indeterminate</center></td>
406 BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT
408 return (indeterminate(x) || indeterminate(y))
410 : !((x && y) || (!x && !y))
417 BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); }
422 BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; }
427 BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
428 { return tribool(indeterminate) != x; }
433 BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
434 { return x != tribool(indeterminate); }
436 } } // end namespace boost::logic
438 // Pull tribool and indeterminate into namespace "boost"
440 using logic::tribool;
441 using logic::indeterminate;
445 * \brief Declare a new name for the third state of a tribool
447 * Use this macro to declare a new name for the third state of a
448 * tribool. This state can have any number of new names (in addition
449 * to \c indeterminate), all of which will be equivalent. The new name will be
450 * placed in the namespace in which the macro is expanded.
453 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
455 * tribool x(true_or_false);
456 * // potentially set x
457 * if (true_or_false(x)) {
458 * // don't know what x is
461 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
463 Name(boost::logic::tribool x, \
464 boost::logic::detail::indeterminate_t = \
465 boost::logic::detail::indeterminate_t()) \
466 { return x.value == boost::logic::tribool::indeterminate_value; }
468 #endif // BOOST_LOGIC_TRIBOOL_HPP