sl@0: // Three-state boolean logic library sl@0: sl@0: // Copyright Douglas Gregor 2002-2004. Use, modification and sl@0: // distribution is subject to the Boost Software License, Version sl@0: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: sl@0: // For more information, see http://www.boost.org sl@0: #ifndef BOOST_LOGIC_TRIBOOL_HPP sl@0: #define BOOST_LOGIC_TRIBOOL_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if BOOST_WORKAROUND(_MSC_VER, >= 1200) sl@0: # pragma once sl@0: #endif sl@0: sl@0: namespace boost { namespace logic { sl@0: sl@0: /// INTERNAL ONLY sl@0: namespace detail { sl@0: /** sl@0: * INTERNAL ONLY sl@0: * sl@0: * \brief A type used only to uniquely identify the 'indeterminate' sl@0: * function/keyword. sl@0: */ sl@0: struct indeterminate_t sl@0: { sl@0: #if BOOST_WORKAROUND(__BORLANDC__, < 0x0600) sl@0: char dummy_; // BCB would use 8 bytes by default sl@0: #endif sl@0: }; sl@0: sl@0: } // end namespace detail sl@0: sl@0: /** sl@0: * INTERNAL ONLY sl@0: * The type of the 'indeterminate' keyword. This has the same type as the sl@0: * function 'indeterminate' so that we can recognize when the keyword is sl@0: * used. sl@0: */ sl@0: typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t); sl@0: sl@0: /** sl@0: * \brief Keyword and test function for the indeterminate tribool value sl@0: * sl@0: * The \c indeterminate function has a dual role. It's first role is sl@0: * as a unary function that tells whether the tribool value is in the sl@0: * "indeterminate" state. It's second role is as a keyword sl@0: * representing the indeterminate (just like "true" and "false" sl@0: * represent the true and false states). If you do not like the name sl@0: * "indeterminate", and would prefer to use a different name, see the sl@0: * macro \c BOOST_TRIBOOL_THIRD_STATE. sl@0: * sl@0: * \returns x.value == tribool::indeterminate_value sl@0: * \throws nothrow sl@0: */ sl@0: inline bool sl@0: indeterminate(tribool x, sl@0: detail::indeterminate_t dummy = detail::indeterminate_t()); sl@0: sl@0: /** sl@0: * \brief A 3-state boolean type. sl@0: * sl@0: * 3-state boolean values are either true, false, or sl@0: * indeterminate. sl@0: */ sl@0: class tribool sl@0: { sl@0: private: sl@0: /// INTERNAL ONLY sl@0: struct dummy { sl@0: void nonnull() {}; sl@0: }; sl@0: sl@0: typedef void (dummy::*safe_bool)(); sl@0: sl@0: public: sl@0: /** sl@0: * Construct a new 3-state boolean value with the value 'false'. sl@0: * sl@0: * \throws nothrow sl@0: */ sl@0: tribool() : value(false_value) {} sl@0: sl@0: /** sl@0: * Construct a new 3-state boolean value with the given boolean sl@0: * value, which may be \c true or \c false. sl@0: * sl@0: * \throws nothrow sl@0: */ sl@0: tribool(bool value) : value(value? true_value : false_value) {} sl@0: sl@0: /** sl@0: * Construct a new 3-state boolean value with an indeterminate value. sl@0: * sl@0: * \throws nothrow sl@0: */ sl@0: tribool(indeterminate_keyword_t) : value(indeterminate_value) {} sl@0: sl@0: /** sl@0: * Use a 3-state boolean in a boolean context. Will evaluate true in a sl@0: * boolean context only when the 3-state boolean is definitely true. sl@0: * sl@0: * \returns true if the 3-state boolean is true, false otherwise sl@0: * \throws nothrow sl@0: */ sl@0: operator safe_bool() const sl@0: { sl@0: return value == true_value? &dummy::nonnull : 0; sl@0: } sl@0: sl@0: /** sl@0: * The actual stored value in this 3-state boolean, which may be false, true, sl@0: * or indeterminate. sl@0: */ sl@0: enum value_t { false_value, true_value, indeterminate_value } value; sl@0: }; sl@0: sl@0: // Check if the given tribool has an indeterminate value. Also doubles as a sl@0: // keyword for the 'indeterminate' value sl@0: inline bool indeterminate(tribool x, detail::indeterminate_t) sl@0: { sl@0: return x.value == tribool::indeterminate_value; sl@0: } sl@0: sl@0: /** @defgroup logical Logical operations sl@0: */ sl@0: //@{ sl@0: /** sl@0: * \brief Computes the logical negation of a tribool sl@0: * sl@0: * \returns the logical negation of the tribool, according to the sl@0: * table: sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: *
!
sl@0: *
false
true
true
false
indeterminate
indeterminate
sl@0: * \throws nothrow sl@0: */ sl@0: inline tribool operator!(tribool x) sl@0: { sl@0: return x.value == tribool::false_value? tribool(true) sl@0: :x.value == tribool::true_value? tribool(false) sl@0: :tribool(indeterminate); sl@0: } sl@0: sl@0: /** sl@0: * \brief Computes the logical conjuction of two tribools sl@0: * sl@0: * \returns the result of logically ANDing the two tribool values, sl@0: * according to the following table: sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: *
&&
false
true
indeterminate
false
false
false
false
true
false
true
indeterminate
indeterminate
false
indeterminate
indeterminate
sl@0: * \throws nothrow sl@0: */ sl@0: inline tribool operator&&(tribool x, tribool y) sl@0: { sl@0: if (static_cast(!x) || static_cast(!y)) sl@0: return false; sl@0: else if (static_cast(x) && static_cast(y)) sl@0: return true; sl@0: else sl@0: return indeterminate; sl@0: } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator&&(tribool x, bool y) sl@0: { return y? x : tribool(false); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator&&(bool x, tribool y) sl@0: { return x? y : tribool(false); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator&&(indeterminate_keyword_t, tribool x) sl@0: { return !x? tribool(false) : tribool(indeterminate); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator&&(tribool x, indeterminate_keyword_t) sl@0: { return !x? tribool(false) : tribool(indeterminate); } sl@0: sl@0: /** sl@0: * \brief Computes the logical disjunction of two tribools sl@0: * sl@0: * \returns the result of logically ORing the two tribool values, sl@0: * according to the following table: sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: *
||
false
true
indeterminate
false
false
true
indeterminate
true
true
true
true
indeterminate
indeterminate
true
indeterminate
sl@0: * \throws nothrow sl@0: */ sl@0: inline tribool operator||(tribool x, tribool y) sl@0: { sl@0: if (static_cast(!x) && static_cast(!y)) sl@0: return false; sl@0: else if (static_cast(x) || static_cast(y)) sl@0: return true; sl@0: else sl@0: return indeterminate; sl@0: } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator||(tribool x, bool y) sl@0: { return y? tribool(true) : x; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator||(bool x, tribool y) sl@0: { return x? tribool(true) : y; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator||(indeterminate_keyword_t, tribool x) sl@0: { return x? tribool(true) : tribool(indeterminate); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator||(tribool x, indeterminate_keyword_t) sl@0: { return x? tribool(true) : tribool(indeterminate); } sl@0: //@} sl@0: sl@0: /** sl@0: * \brief Compare tribools for equality sl@0: * sl@0: * \returns the result of comparing two tribool values, according to sl@0: * the following table: sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: *
==
false
true
indeterminate
false
true
false
indeterminate
true
false
true
indeterminate
indeterminate
indeterminate
indeterminate
indeterminate
sl@0: * \throws nothrow sl@0: */ sl@0: inline tribool operator==(tribool x, tribool y) sl@0: { sl@0: if (indeterminate(x) || indeterminate(y)) sl@0: return indeterminate; sl@0: else sl@0: return x && y || !x && !y; sl@0: } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator==(tribool x, bool y) { return x == tribool(y); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator==(bool x, tribool y) { return tribool(x) == y; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator==(indeterminate_keyword_t, tribool x) sl@0: { return tribool(indeterminate) == x; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator==(tribool x, indeterminate_keyword_t) sl@0: { return tribool(indeterminate) == x; } sl@0: sl@0: /** sl@0: * \brief Compare tribools for inequality sl@0: * sl@0: * \returns the result of comparing two tribool values for inequality, sl@0: * according to the following table: sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * sl@0: *
!=
false
true
indeterminate
false
false
true
indeterminate
true
true
false
indeterminate
indeterminate
indeterminate
indeterminate
indeterminate
sl@0: * \throws nothrow sl@0: */ sl@0: inline tribool operator!=(tribool x, tribool y) sl@0: { sl@0: if (indeterminate(x) || indeterminate(y)) sl@0: return indeterminate; sl@0: else sl@0: return !(x && y || !x && !y); sl@0: } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator!=(tribool x, bool y) { return x != tribool(y); } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator!=(indeterminate_keyword_t, tribool x) sl@0: { return tribool(indeterminate) != x; } sl@0: sl@0: /** sl@0: * \overload sl@0: */ sl@0: inline tribool operator!=(tribool x, indeterminate_keyword_t) sl@0: { return x != tribool(indeterminate); } sl@0: sl@0: } } // end namespace boost::logic sl@0: sl@0: // Pull tribool and indeterminate into namespace "boost" sl@0: namespace boost { sl@0: using logic::tribool; sl@0: using logic::indeterminate; sl@0: } sl@0: sl@0: /** sl@0: * \brief Declare a new name for the third state of a tribool sl@0: * sl@0: * Use this macro to declare a new name for the third state of a sl@0: * tribool. This state can have any number of new names (in addition sl@0: * to \c indeterminate), all of which will be equivalent. The new name will be sl@0: * placed in the namespace in which the macro is expanded. sl@0: * sl@0: * Example: sl@0: * BOOST_TRIBOOL_THIRD_STATE(true_or_false) sl@0: * sl@0: * tribool x(true_or_false); sl@0: * // potentially set x sl@0: * if (true_or_false(x)) { sl@0: * // don't know what x is sl@0: * } sl@0: */ sl@0: #define BOOST_TRIBOOL_THIRD_STATE(Name) \ sl@0: inline bool \ sl@0: Name(boost::logic::tribool x, \ sl@0: boost::logic::detail::indeterminate_t dummy = \ sl@0: boost::logic::detail::indeterminate_t()) \ sl@0: { return x.value == boost::logic::tribool::indeterminate_value; } sl@0: sl@0: #endif // BOOST_LOGIC_TRIBOOL_HPP sl@0: