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(__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
80 typedef void (dummy::*safe_bool)();
84 * Construct a new 3-state boolean value with the value 'false'.
88 BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : 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 BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {}
99 * Construct a new 3-state boolean value with an indeterminate value.
103 BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : 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 BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT
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 BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT
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 BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT
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 BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT
201 return (static_cast<bool>(!x) || static_cast<bool>(!y))
203 : ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate)
210 BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT
211 { return y? x : tribool(false); }
216 BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT
217 { return x? y : tribool(false); }
222 BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
223 { return !x? tribool(false) : tribool(indeterminate); }
228 BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
229 { return !x? tribool(false) : tribool(indeterminate); }
232 * \brief Computes the logical disjunction of two tribools
234 * \returns the result of logically ORing the two tribool values,
235 * according to the following table:
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>
244 * <th><center>false</center></th>
245 * <td><center>false</center></td>
246 * <td><center>true</center></td>
247 * <td><center>indeterminate</center></td>
250 * <th><center>true</center></th>
251 * <td><center>true</center></td>
252 * <td><center>true</center></td>
253 * <td><center>true</center></td>
256 * <th><center>indeterminate</center></th>
257 * <td><center>indeterminate</center></td>
258 * <td><center>true</center></td>
259 * <td><center>indeterminate</center></td>
264 BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT
266 return (static_cast<bool>(!x) && static_cast<bool>(!y))
268 : ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate))
275 BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT
276 { return y? tribool(true) : x; }
281 BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT
282 { return x? tribool(true) : y; }
287 BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
288 { return x? tribool(true) : tribool(indeterminate); }
293 BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
294 { return x? tribool(true) : tribool(indeterminate); }
298 * \brief Compare tribools for equality
300 * \returns the result of comparing two tribool values, according to
301 * the following table:
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>
310 * <th><center>false</center></th>
311 * <td><center>true</center></td>
312 * <td><center>false</center></td>
313 * <td><center>indeterminate</center></td>
316 * <th><center>true</center></th>
317 * <td><center>false</center></td>
318 * <td><center>true</center></td>
319 * <td><center>indeterminate</center></td>
322 * <th><center>indeterminate</center></th>
323 * <td><center>indeterminate</center></td>
324 * <td><center>indeterminate</center></td>
325 * <td><center>indeterminate</center></td>
330 BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT
332 return (indeterminate(x) || indeterminate(y))
334 : ((x && y) || (!x && !y))
341 BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); }
346 BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; }
351 BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
352 { return tribool(indeterminate) == x; }
357 BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
358 { return tribool(indeterminate) == x; }
361 * \brief Compare tribools for inequality
363 * \returns the result of comparing two tribool values for inequality,
364 * according to the following table:
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>
373 * <th><center>false</center></th>
374 * <td><center>false</center></td>
375 * <td><center>true</center></td>
376 * <td><center>indeterminate</center></td>
379 * <th><center>true</center></th>
380 * <td><center>true</center></td>
381 * <td><center>false</center></td>
382 * <td><center>indeterminate</center></td>
385 * <th><center>indeterminate</center></th>
386 * <td><center>indeterminate</center></td>
387 * <td><center>indeterminate</center></td>
388 * <td><center>indeterminate</center></td>
393 BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT
395 return (indeterminate(x) || indeterminate(y))
397 : !((x && y) || (!x && !y))
404 BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); }
409 BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; }
414 BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
415 { return tribool(indeterminate) != x; }
420 BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
421 { return x != tribool(indeterminate); }
423 } } // end namespace boost::logic
425 // Pull tribool and indeterminate into namespace "boost"
427 using logic::tribool;
428 using logic::indeterminate;
432 * \brief Declare a new name for the third state of a tribool
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.
440 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
442 * tribool x(true_or_false);
443 * // potentially set x
444 * if (true_or_false(x)) {
445 * // don't know what x is
448 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
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; }
455 #endif // BOOST_LOGIC_TRIBOOL_HPP