|
||||
File indexing completed on 2025-01-18 09:39:30
0001 // Three-state boolean logic library 0002 0003 // Copyright Douglas Gregor 2002-2004. Use, modification and 0004 // distribution is subject to the Boost Software License, Version 0005 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at 0006 // http://www.boost.org/LICENSE_1_0.txt) 0007 0008 0009 // For more information, see http://www.boost.org 0010 #ifndef BOOST_LOGIC_TRIBOOL_HPP 0011 #define BOOST_LOGIC_TRIBOOL_HPP 0012 0013 #include <boost/logic/tribool_fwd.hpp> 0014 #include <boost/config.hpp> 0015 #include <boost/detail/workaround.hpp> 0016 0017 #ifdef BOOST_HAS_PRAGMA_ONCE 0018 # pragma once 0019 #endif 0020 0021 namespace boost { namespace logic { 0022 0023 /// INTERNAL ONLY 0024 namespace detail { 0025 /** 0026 * INTERNAL ONLY 0027 * 0028 * \brief A type used only to uniquely identify the 'indeterminate' 0029 * function/keyword. 0030 */ 0031 struct indeterminate_t 0032 { 0033 #if BOOST_WORKAROUND(BOOST_BORLANDC, < 0x0600) 0034 char dummy_; // BCB would use 8 bytes by default 0035 #endif 0036 }; 0037 0038 } // end namespace detail 0039 0040 /** 0041 * INTERNAL ONLY 0042 * The type of the 'indeterminate' keyword. This has the same type as the 0043 * function 'indeterminate' so that we can recognize when the keyword is 0044 * used. 0045 */ 0046 typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t); 0047 0048 /** 0049 * \brief Keyword and test function for the indeterminate tribool value 0050 * 0051 * The \c indeterminate function has a dual role. It's first role is 0052 * as a unary function that tells whether the tribool value is in the 0053 * "indeterminate" state. It's second role is as a keyword 0054 * representing the indeterminate (just like "true" and "false" 0055 * represent the true and false states). If you do not like the name 0056 * "indeterminate", and would prefer to use a different name, see the 0057 * macro \c BOOST_TRIBOOL_THIRD_STATE. 0058 * 0059 * \returns <tt>x.value == tribool::indeterminate_value</tt> 0060 * \throws nothrow 0061 */ 0062 BOOST_CONSTEXPR inline bool 0063 indeterminate(tribool x, 0064 detail::indeterminate_t dummy = detail::indeterminate_t()) BOOST_NOEXCEPT; 0065 0066 /** 0067 * \brief A 3-state boolean type. 0068 * 0069 * 3-state boolean values are either true, false, or 0070 * indeterminate. 0071 */ 0072 class tribool 0073 { 0074 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) 0075 private: 0076 /// INTERNAL ONLY 0077 struct dummy { 0078 void nonnull() {}; 0079 }; 0080 0081 typedef void (dummy::*safe_bool)(); 0082 #endif 0083 0084 public: 0085 /** 0086 * Construct a new 3-state boolean value with the value 'false'. 0087 * 0088 * \throws nothrow 0089 */ 0090 BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : value(false_value) {} 0091 0092 /** 0093 * Construct a new 3-state boolean value with the given boolean 0094 * value, which may be \c true or \c false. 0095 * 0096 * \throws nothrow 0097 */ 0098 BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {} 0099 0100 /** 0101 * Construct a new 3-state boolean value with an indeterminate value. 0102 * 0103 * \throws nothrow 0104 */ 0105 BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : value(indeterminate_value) {} 0106 0107 /** 0108 * Use a 3-state boolean in a boolean context. Will evaluate true in a 0109 * boolean context only when the 3-state boolean is definitely true. 0110 * 0111 * \returns true if the 3-state boolean is true, false otherwise 0112 * \throws nothrow 0113 */ 0114 #if !defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) 0115 0116 BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT 0117 { 0118 return value == true_value; 0119 } 0120 0121 #else 0122 0123 BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT 0124 { 0125 return value == true_value? &dummy::nonnull : 0; 0126 } 0127 0128 #endif 0129 0130 /** 0131 * The actual stored value in this 3-state boolean, which may be false, true, 0132 * or indeterminate. 0133 */ 0134 enum value_t { false_value, true_value, indeterminate_value } value; 0135 }; 0136 0137 // Check if the given tribool has an indeterminate value. Also doubles as a 0138 // keyword for the 'indeterminate' value 0139 BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT 0140 { 0141 return x.value == tribool::indeterminate_value; 0142 } 0143 0144 /** @defgroup logical Logical operations 0145 */ 0146 //@{ 0147 /** 0148 * \brief Computes the logical negation of a tribool 0149 * 0150 * \returns the logical negation of the tribool, according to the 0151 * table: 0152 * <table border=1> 0153 * <tr> 0154 * <th><center><code>!</code></center></th> 0155 * <th/> 0156 * </tr> 0157 * <tr> 0158 * <th><center>false</center></th> 0159 * <td><center>true</center></td> 0160 * </tr> 0161 * <tr> 0162 * <th><center>true</center></th> 0163 * <td><center>false</center></td> 0164 * </tr> 0165 * <tr> 0166 * <th><center>indeterminate</center></th> 0167 * <td><center>indeterminate</center></td> 0168 * </tr> 0169 * </table> 0170 * \throws nothrow 0171 */ 0172 BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT 0173 { 0174 return x.value == tribool::false_value? tribool(true) 0175 :x.value == tribool::true_value? tribool(false) 0176 :tribool(indeterminate); 0177 } 0178 0179 /** 0180 * \brief Computes the logical conjunction of two tribools 0181 * 0182 * \returns the result of logically ANDing the two tribool values, 0183 * according to the following table: 0184 * <table border=1> 0185 * <tr> 0186 * <th><center><code>&&</code></center></th> 0187 * <th><center>false</center></th> 0188 * <th><center>true</center></th> 0189 * <th><center>indeterminate</center></th> 0190 * </tr> 0191 * <tr> 0192 * <th><center>false</center></th> 0193 * <td><center>false</center></td> 0194 * <td><center>false</center></td> 0195 * <td><center>false</center></td> 0196 * </tr> 0197 * <tr> 0198 * <th><center>true</center></th> 0199 * <td><center>false</center></td> 0200 * <td><center>true</center></td> 0201 * <td><center>indeterminate</center></td> 0202 * </tr> 0203 * <tr> 0204 * <th><center>indeterminate</center></th> 0205 * <td><center>false</center></td> 0206 * <td><center>indeterminate</center></td> 0207 * <td><center>indeterminate</center></td> 0208 * </tr> 0209 * </table> 0210 * \throws nothrow 0211 */ 0212 BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT 0213 { 0214 return (static_cast<bool>(!x) || static_cast<bool>(!y)) 0215 ? tribool(false) 0216 : ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate) 0217 ; 0218 } 0219 0220 /** 0221 * \overload 0222 */ 0223 BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT 0224 { return y? x : tribool(false); } 0225 0226 /** 0227 * \overload 0228 */ 0229 BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT 0230 { return x? y : tribool(false); } 0231 0232 /** 0233 * \overload 0234 */ 0235 BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT 0236 { return !x? tribool(false) : tribool(indeterminate); } 0237 0238 /** 0239 * \overload 0240 */ 0241 BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT 0242 { return !x? tribool(false) : tribool(indeterminate); } 0243 0244 /** 0245 * \brief Computes the logical disjunction of two tribools 0246 * 0247 * \returns the result of logically ORing the two tribool values, 0248 * according to the following table: 0249 * <table border=1> 0250 * <tr> 0251 * <th><center><code>||</code></center></th> 0252 * <th><center>false</center></th> 0253 * <th><center>true</center></th> 0254 * <th><center>indeterminate</center></th> 0255 * </tr> 0256 * <tr> 0257 * <th><center>false</center></th> 0258 * <td><center>false</center></td> 0259 * <td><center>true</center></td> 0260 * <td><center>indeterminate</center></td> 0261 * </tr> 0262 * <tr> 0263 * <th><center>true</center></th> 0264 * <td><center>true</center></td> 0265 * <td><center>true</center></td> 0266 * <td><center>true</center></td> 0267 * </tr> 0268 * <tr> 0269 * <th><center>indeterminate</center></th> 0270 * <td><center>indeterminate</center></td> 0271 * <td><center>true</center></td> 0272 * <td><center>indeterminate</center></td> 0273 * </tr> 0274 * </table> 0275 * \throws nothrow 0276 */ 0277 BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT 0278 { 0279 return (static_cast<bool>(!x) && static_cast<bool>(!y)) 0280 ? tribool(false) 0281 : ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate)) 0282 ; 0283 } 0284 0285 /** 0286 * \overload 0287 */ 0288 BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT 0289 { return y? tribool(true) : x; } 0290 0291 /** 0292 * \overload 0293 */ 0294 BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT 0295 { return x? tribool(true) : y; } 0296 0297 /** 0298 * \overload 0299 */ 0300 BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT 0301 { return x? tribool(true) : tribool(indeterminate); } 0302 0303 /** 0304 * \overload 0305 */ 0306 BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT 0307 { return x? tribool(true) : tribool(indeterminate); } 0308 //@} 0309 0310 /** 0311 * \brief Compare tribools for equality 0312 * 0313 * \returns the result of comparing two tribool values, according to 0314 * the following table: 0315 * <table border=1> 0316 * <tr> 0317 * <th><center><code>==</code></center></th> 0318 * <th><center>false</center></th> 0319 * <th><center>true</center></th> 0320 * <th><center>indeterminate</center></th> 0321 * </tr> 0322 * <tr> 0323 * <th><center>false</center></th> 0324 * <td><center>true</center></td> 0325 * <td><center>false</center></td> 0326 * <td><center>indeterminate</center></td> 0327 * </tr> 0328 * <tr> 0329 * <th><center>true</center></th> 0330 * <td><center>false</center></td> 0331 * <td><center>true</center></td> 0332 * <td><center>indeterminate</center></td> 0333 * </tr> 0334 * <tr> 0335 * <th><center>indeterminate</center></th> 0336 * <td><center>indeterminate</center></td> 0337 * <td><center>indeterminate</center></td> 0338 * <td><center>indeterminate</center></td> 0339 * </tr> 0340 * </table> 0341 * \throws nothrow 0342 */ 0343 BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT 0344 { 0345 return (indeterminate(x) || indeterminate(y)) 0346 ? indeterminate 0347 : ((x && y) || (!x && !y)) 0348 ; 0349 } 0350 0351 /** 0352 * \overload 0353 */ 0354 BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); } 0355 0356 /** 0357 * \overload 0358 */ 0359 BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; } 0360 0361 /** 0362 * \overload 0363 */ 0364 BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT 0365 { return tribool(indeterminate) == x; } 0366 0367 /** 0368 * \overload 0369 */ 0370 BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT 0371 { return tribool(indeterminate) == x; } 0372 0373 /** 0374 * \brief Compare tribools for inequality 0375 * 0376 * \returns the result of comparing two tribool values for inequality, 0377 * according to the following table: 0378 * <table border=1> 0379 * <tr> 0380 * <th><center><code>!=</code></center></th> 0381 * <th><center>false</center></th> 0382 * <th><center>true</center></th> 0383 * <th><center>indeterminate</center></th> 0384 * </tr> 0385 * <tr> 0386 * <th><center>false</center></th> 0387 * <td><center>false</center></td> 0388 * <td><center>true</center></td> 0389 * <td><center>indeterminate</center></td> 0390 * </tr> 0391 * <tr> 0392 * <th><center>true</center></th> 0393 * <td><center>true</center></td> 0394 * <td><center>false</center></td> 0395 * <td><center>indeterminate</center></td> 0396 * </tr> 0397 * <tr> 0398 * <th><center>indeterminate</center></th> 0399 * <td><center>indeterminate</center></td> 0400 * <td><center>indeterminate</center></td> 0401 * <td><center>indeterminate</center></td> 0402 * </tr> 0403 * </table> 0404 * \throws nothrow 0405 */ 0406 BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT 0407 { 0408 return (indeterminate(x) || indeterminate(y)) 0409 ? indeterminate 0410 : !((x && y) || (!x && !y)) 0411 ; 0412 } 0413 0414 /** 0415 * \overload 0416 */ 0417 BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); } 0418 0419 /** 0420 * \overload 0421 */ 0422 BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; } 0423 0424 /** 0425 * \overload 0426 */ 0427 BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT 0428 { return tribool(indeterminate) != x; } 0429 0430 /** 0431 * \overload 0432 */ 0433 BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT 0434 { return x != tribool(indeterminate); } 0435 0436 } } // end namespace boost::logic 0437 0438 // Pull tribool and indeterminate into namespace "boost" 0439 namespace boost { 0440 using logic::tribool; 0441 using logic::indeterminate; 0442 } 0443 0444 /** 0445 * \brief Declare a new name for the third state of a tribool 0446 * 0447 * Use this macro to declare a new name for the third state of a 0448 * tribool. This state can have any number of new names (in addition 0449 * to \c indeterminate), all of which will be equivalent. The new name will be 0450 * placed in the namespace in which the macro is expanded. 0451 * 0452 * Example: 0453 * BOOST_TRIBOOL_THIRD_STATE(true_or_false) 0454 * 0455 * tribool x(true_or_false); 0456 * // potentially set x 0457 * if (true_or_false(x)) { 0458 * // don't know what x is 0459 * } 0460 */ 0461 #define BOOST_TRIBOOL_THIRD_STATE(Name) \ 0462 inline bool \ 0463 Name(boost::logic::tribool x, \ 0464 boost::logic::detail::indeterminate_t = \ 0465 boost::logic::detail::indeterminate_t()) \ 0466 { return x.value == boost::logic::tribool::indeterminate_value; } 0467 0468 #endif // BOOST_LOGIC_TRIBOOL_HPP 0469
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |