annotate DEPENDENCIES/generic/include/boost/logic/tribool.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Three-state boolean logic library
Chris@16 2
Chris@16 3 // Copyright Douglas Gregor 2002-2004. Use, modification and
Chris@16 4 // distribution is subject to the Boost Software License, Version
Chris@16 5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8
Chris@16 9 // For more information, see http://www.boost.org
Chris@16 10 #ifndef BOOST_LOGIC_TRIBOOL_HPP
Chris@16 11 #define BOOST_LOGIC_TRIBOOL_HPP
Chris@16 12
Chris@16 13 #include <boost/logic/tribool_fwd.hpp>
Chris@16 14 #include <boost/config.hpp>
Chris@16 15 #include <boost/detail/workaround.hpp>
Chris@16 16
Chris@101 17 #ifdef BOOST_HAS_PRAGMA_ONCE
Chris@16 18 # pragma once
Chris@16 19 #endif
Chris@16 20
Chris@16 21 namespace boost { namespace logic {
Chris@16 22
Chris@16 23 /// INTERNAL ONLY
Chris@16 24 namespace detail {
Chris@16 25 /**
Chris@16 26 * INTERNAL ONLY
Chris@16 27 *
Chris@16 28 * \brief A type used only to uniquely identify the 'indeterminate'
Chris@16 29 * function/keyword.
Chris@16 30 */
Chris@16 31 struct indeterminate_t
Chris@16 32 {
Chris@16 33 #if BOOST_WORKAROUND(__BORLANDC__, < 0x0600)
Chris@16 34 char dummy_; // BCB would use 8 bytes by default
Chris@16 35 #endif
Chris@16 36 };
Chris@16 37
Chris@16 38 } // end namespace detail
Chris@16 39
Chris@16 40 /**
Chris@16 41 * INTERNAL ONLY
Chris@16 42 * The type of the 'indeterminate' keyword. This has the same type as the
Chris@16 43 * function 'indeterminate' so that we can recognize when the keyword is
Chris@16 44 * used.
Chris@16 45 */
Chris@16 46 typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
Chris@16 47
Chris@16 48 /**
Chris@16 49 * \brief Keyword and test function for the indeterminate tribool value
Chris@16 50 *
Chris@16 51 * The \c indeterminate function has a dual role. It's first role is
Chris@16 52 * as a unary function that tells whether the tribool value is in the
Chris@16 53 * "indeterminate" state. It's second role is as a keyword
Chris@16 54 * representing the indeterminate (just like "true" and "false"
Chris@16 55 * represent the true and false states). If you do not like the name
Chris@16 56 * "indeterminate", and would prefer to use a different name, see the
Chris@16 57 * macro \c BOOST_TRIBOOL_THIRD_STATE.
Chris@16 58 *
Chris@16 59 * \returns <tt>x.value == tribool::indeterminate_value</tt>
Chris@16 60 * \throws nothrow
Chris@16 61 */
Chris@101 62 BOOST_CONSTEXPR inline bool
Chris@16 63 indeterminate(tribool x,
Chris@101 64 detail::indeterminate_t dummy = detail::indeterminate_t()) BOOST_NOEXCEPT;
Chris@16 65
Chris@16 66 /**
Chris@16 67 * \brief A 3-state boolean type.
Chris@16 68 *
Chris@16 69 * 3-state boolean values are either true, false, or
Chris@16 70 * indeterminate.
Chris@16 71 */
Chris@16 72 class tribool
Chris@16 73 {
Chris@16 74 private:
Chris@16 75 /// INTERNAL ONLY
Chris@16 76 struct dummy {
Chris@16 77 void nonnull() {};
Chris@16 78 };
Chris@16 79
Chris@16 80 typedef void (dummy::*safe_bool)();
Chris@16 81
Chris@16 82 public:
Chris@16 83 /**
Chris@16 84 * Construct a new 3-state boolean value with the value 'false'.
Chris@16 85 *
Chris@16 86 * \throws nothrow
Chris@16 87 */
Chris@101 88 BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : value(false_value) {}
Chris@16 89
Chris@16 90 /**
Chris@16 91 * Construct a new 3-state boolean value with the given boolean
Chris@16 92 * value, which may be \c true or \c false.
Chris@16 93 *
Chris@16 94 * \throws nothrow
Chris@16 95 */
Chris@101 96 BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {}
Chris@16 97
Chris@16 98 /**
Chris@16 99 * Construct a new 3-state boolean value with an indeterminate value.
Chris@16 100 *
Chris@16 101 * \throws nothrow
Chris@16 102 */
Chris@101 103 BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : value(indeterminate_value) {}
Chris@16 104
Chris@16 105 /**
Chris@16 106 * Use a 3-state boolean in a boolean context. Will evaluate true in a
Chris@16 107 * boolean context only when the 3-state boolean is definitely true.
Chris@16 108 *
Chris@16 109 * \returns true if the 3-state boolean is true, false otherwise
Chris@16 110 * \throws nothrow
Chris@16 111 */
Chris@101 112 BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT
Chris@16 113 {
Chris@16 114 return value == true_value? &dummy::nonnull : 0;
Chris@16 115 }
Chris@16 116
Chris@16 117 /**
Chris@16 118 * The actual stored value in this 3-state boolean, which may be false, true,
Chris@16 119 * or indeterminate.
Chris@16 120 */
Chris@16 121 enum value_t { false_value, true_value, indeterminate_value } value;
Chris@16 122 };
Chris@16 123
Chris@16 124 // Check if the given tribool has an indeterminate value. Also doubles as a
Chris@16 125 // keyword for the 'indeterminate' value
Chris@101 126 BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT
Chris@16 127 {
Chris@16 128 return x.value == tribool::indeterminate_value;
Chris@16 129 }
Chris@16 130
Chris@16 131 /** @defgroup logical Logical operations
Chris@16 132 */
Chris@16 133 //@{
Chris@16 134 /**
Chris@16 135 * \brief Computes the logical negation of a tribool
Chris@16 136 *
Chris@16 137 * \returns the logical negation of the tribool, according to the
Chris@16 138 * table:
Chris@16 139 * <table border=1>
Chris@16 140 * <tr>
Chris@16 141 * <th><center><code>!</code></center></th>
Chris@16 142 * <th/>
Chris@16 143 * </tr>
Chris@16 144 * <tr>
Chris@16 145 * <th><center>false</center></th>
Chris@16 146 * <td><center>true</center></td>
Chris@16 147 * </tr>
Chris@16 148 * <tr>
Chris@16 149 * <th><center>true</center></th>
Chris@16 150 * <td><center>false</center></td>
Chris@16 151 * </tr>
Chris@16 152 * <tr>
Chris@16 153 * <th><center>indeterminate</center></th>
Chris@16 154 * <td><center>indeterminate</center></td>
Chris@16 155 * </tr>
Chris@16 156 * </table>
Chris@16 157 * \throws nothrow
Chris@16 158 */
Chris@101 159 BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT
Chris@16 160 {
Chris@16 161 return x.value == tribool::false_value? tribool(true)
Chris@16 162 :x.value == tribool::true_value? tribool(false)
Chris@16 163 :tribool(indeterminate);
Chris@16 164 }
Chris@16 165
Chris@16 166 /**
Chris@16 167 * \brief Computes the logical conjuction of two tribools
Chris@16 168 *
Chris@16 169 * \returns the result of logically ANDing the two tribool values,
Chris@16 170 * according to the following table:
Chris@16 171 * <table border=1>
Chris@16 172 * <tr>
Chris@16 173 * <th><center><code>&amp;&amp;</code></center></th>
Chris@16 174 * <th><center>false</center></th>
Chris@16 175 * <th><center>true</center></th>
Chris@16 176 * <th><center>indeterminate</center></th>
Chris@16 177 * </tr>
Chris@16 178 * <tr>
Chris@16 179 * <th><center>false</center></th>
Chris@16 180 * <td><center>false</center></td>
Chris@16 181 * <td><center>false</center></td>
Chris@16 182 * <td><center>false</center></td>
Chris@16 183 * </tr>
Chris@16 184 * <tr>
Chris@16 185 * <th><center>true</center></th>
Chris@16 186 * <td><center>false</center></td>
Chris@16 187 * <td><center>true</center></td>
Chris@16 188 * <td><center>indeterminate</center></td>
Chris@16 189 * </tr>
Chris@16 190 * <tr>
Chris@16 191 * <th><center>indeterminate</center></th>
Chris@16 192 * <td><center>false</center></td>
Chris@16 193 * <td><center>indeterminate</center></td>
Chris@16 194 * <td><center>indeterminate</center></td>
Chris@16 195 * </tr>
Chris@16 196 * </table>
Chris@16 197 * \throws nothrow
Chris@16 198 */
Chris@101 199 BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT
Chris@16 200 {
Chris@101 201 return (static_cast<bool>(!x) || static_cast<bool>(!y))
Chris@101 202 ? tribool(false)
Chris@101 203 : ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate)
Chris@101 204 ;
Chris@16 205 }
Chris@16 206
Chris@16 207 /**
Chris@16 208 * \overload
Chris@16 209 */
Chris@101 210 BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT
Chris@16 211 { return y? x : tribool(false); }
Chris@16 212
Chris@16 213 /**
Chris@16 214 * \overload
Chris@16 215 */
Chris@101 216 BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT
Chris@16 217 { return x? y : tribool(false); }
Chris@16 218
Chris@16 219 /**
Chris@16 220 * \overload
Chris@16 221 */
Chris@101 222 BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
Chris@16 223 { return !x? tribool(false) : tribool(indeterminate); }
Chris@16 224
Chris@16 225 /**
Chris@16 226 * \overload
Chris@16 227 */
Chris@101 228 BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
Chris@16 229 { return !x? tribool(false) : tribool(indeterminate); }
Chris@16 230
Chris@16 231 /**
Chris@16 232 * \brief Computes the logical disjunction of two tribools
Chris@16 233 *
Chris@16 234 * \returns the result of logically ORing the two tribool values,
Chris@16 235 * according to the following table:
Chris@16 236 * <table border=1>
Chris@16 237 * <tr>
Chris@16 238 * <th><center><code>||</code></center></th>
Chris@16 239 * <th><center>false</center></th>
Chris@16 240 * <th><center>true</center></th>
Chris@16 241 * <th><center>indeterminate</center></th>
Chris@16 242 * </tr>
Chris@16 243 * <tr>
Chris@16 244 * <th><center>false</center></th>
Chris@16 245 * <td><center>false</center></td>
Chris@16 246 * <td><center>true</center></td>
Chris@16 247 * <td><center>indeterminate</center></td>
Chris@16 248 * </tr>
Chris@16 249 * <tr>
Chris@16 250 * <th><center>true</center></th>
Chris@16 251 * <td><center>true</center></td>
Chris@16 252 * <td><center>true</center></td>
Chris@16 253 * <td><center>true</center></td>
Chris@16 254 * </tr>
Chris@16 255 * <tr>
Chris@16 256 * <th><center>indeterminate</center></th>
Chris@16 257 * <td><center>indeterminate</center></td>
Chris@16 258 * <td><center>true</center></td>
Chris@16 259 * <td><center>indeterminate</center></td>
Chris@16 260 * </tr>
Chris@16 261 * </table>
Chris@16 262 * \throws nothrow
Chris@16 263 */
Chris@101 264 BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT
Chris@16 265 {
Chris@101 266 return (static_cast<bool>(!x) && static_cast<bool>(!y))
Chris@101 267 ? tribool(false)
Chris@101 268 : ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate))
Chris@101 269 ;
Chris@16 270 }
Chris@16 271
Chris@16 272 /**
Chris@16 273 * \overload
Chris@16 274 */
Chris@101 275 BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT
Chris@16 276 { return y? tribool(true) : x; }
Chris@16 277
Chris@16 278 /**
Chris@16 279 * \overload
Chris@16 280 */
Chris@101 281 BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT
Chris@16 282 { return x? tribool(true) : y; }
Chris@16 283
Chris@16 284 /**
Chris@16 285 * \overload
Chris@16 286 */
Chris@101 287 BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
Chris@16 288 { return x? tribool(true) : tribool(indeterminate); }
Chris@16 289
Chris@16 290 /**
Chris@16 291 * \overload
Chris@16 292 */
Chris@101 293 BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
Chris@16 294 { return x? tribool(true) : tribool(indeterminate); }
Chris@16 295 //@}
Chris@16 296
Chris@16 297 /**
Chris@16 298 * \brief Compare tribools for equality
Chris@16 299 *
Chris@16 300 * \returns the result of comparing two tribool values, according to
Chris@16 301 * the following table:
Chris@16 302 * <table border=1>
Chris@16 303 * <tr>
Chris@16 304 * <th><center><code>==</code></center></th>
Chris@16 305 * <th><center>false</center></th>
Chris@16 306 * <th><center>true</center></th>
Chris@16 307 * <th><center>indeterminate</center></th>
Chris@16 308 * </tr>
Chris@16 309 * <tr>
Chris@16 310 * <th><center>false</center></th>
Chris@16 311 * <td><center>true</center></td>
Chris@16 312 * <td><center>false</center></td>
Chris@16 313 * <td><center>indeterminate</center></td>
Chris@16 314 * </tr>
Chris@16 315 * <tr>
Chris@16 316 * <th><center>true</center></th>
Chris@16 317 * <td><center>false</center></td>
Chris@16 318 * <td><center>true</center></td>
Chris@16 319 * <td><center>indeterminate</center></td>
Chris@16 320 * </tr>
Chris@16 321 * <tr>
Chris@16 322 * <th><center>indeterminate</center></th>
Chris@16 323 * <td><center>indeterminate</center></td>
Chris@16 324 * <td><center>indeterminate</center></td>
Chris@16 325 * <td><center>indeterminate</center></td>
Chris@16 326 * </tr>
Chris@16 327 * </table>
Chris@16 328 * \throws nothrow
Chris@16 329 */
Chris@101 330 BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT
Chris@16 331 {
Chris@101 332 return (indeterminate(x) || indeterminate(y))
Chris@101 333 ? indeterminate
Chris@101 334 : ((x && y) || (!x && !y))
Chris@101 335 ;
Chris@16 336 }
Chris@16 337
Chris@16 338 /**
Chris@16 339 * \overload
Chris@16 340 */
Chris@101 341 BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); }
Chris@16 342
Chris@16 343 /**
Chris@16 344 * \overload
Chris@16 345 */
Chris@101 346 BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; }
Chris@16 347
Chris@16 348 /**
Chris@16 349 * \overload
Chris@16 350 */
Chris@101 351 BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
Chris@16 352 { return tribool(indeterminate) == x; }
Chris@16 353
Chris@16 354 /**
Chris@16 355 * \overload
Chris@16 356 */
Chris@101 357 BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
Chris@16 358 { return tribool(indeterminate) == x; }
Chris@16 359
Chris@16 360 /**
Chris@16 361 * \brief Compare tribools for inequality
Chris@16 362 *
Chris@16 363 * \returns the result of comparing two tribool values for inequality,
Chris@16 364 * according to the following table:
Chris@16 365 * <table border=1>
Chris@16 366 * <tr>
Chris@16 367 * <th><center><code>!=</code></center></th>
Chris@16 368 * <th><center>false</center></th>
Chris@16 369 * <th><center>true</center></th>
Chris@16 370 * <th><center>indeterminate</center></th>
Chris@16 371 * </tr>
Chris@16 372 * <tr>
Chris@16 373 * <th><center>false</center></th>
Chris@16 374 * <td><center>false</center></td>
Chris@16 375 * <td><center>true</center></td>
Chris@16 376 * <td><center>indeterminate</center></td>
Chris@16 377 * </tr>
Chris@16 378 * <tr>
Chris@16 379 * <th><center>true</center></th>
Chris@16 380 * <td><center>true</center></td>
Chris@16 381 * <td><center>false</center></td>
Chris@16 382 * <td><center>indeterminate</center></td>
Chris@16 383 * </tr>
Chris@16 384 * <tr>
Chris@16 385 * <th><center>indeterminate</center></th>
Chris@16 386 * <td><center>indeterminate</center></td>
Chris@16 387 * <td><center>indeterminate</center></td>
Chris@16 388 * <td><center>indeterminate</center></td>
Chris@16 389 * </tr>
Chris@16 390 * </table>
Chris@16 391 * \throws nothrow
Chris@16 392 */
Chris@101 393 BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT
Chris@16 394 {
Chris@101 395 return (indeterminate(x) || indeterminate(y))
Chris@101 396 ? indeterminate
Chris@101 397 : !((x && y) || (!x && !y))
Chris@101 398 ;
Chris@16 399 }
Chris@16 400
Chris@16 401 /**
Chris@16 402 * \overload
Chris@16 403 */
Chris@101 404 BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); }
Chris@16 405
Chris@16 406 /**
Chris@16 407 * \overload
Chris@16 408 */
Chris@101 409 BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; }
Chris@16 410
Chris@16 411 /**
Chris@16 412 * \overload
Chris@16 413 */
Chris@101 414 BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
Chris@16 415 { return tribool(indeterminate) != x; }
Chris@16 416
Chris@16 417 /**
Chris@16 418 * \overload
Chris@16 419 */
Chris@101 420 BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
Chris@16 421 { return x != tribool(indeterminate); }
Chris@16 422
Chris@16 423 } } // end namespace boost::logic
Chris@16 424
Chris@16 425 // Pull tribool and indeterminate into namespace "boost"
Chris@16 426 namespace boost {
Chris@16 427 using logic::tribool;
Chris@16 428 using logic::indeterminate;
Chris@16 429 }
Chris@16 430
Chris@16 431 /**
Chris@16 432 * \brief Declare a new name for the third state of a tribool
Chris@16 433 *
Chris@16 434 * Use this macro to declare a new name for the third state of a
Chris@16 435 * tribool. This state can have any number of new names (in addition
Chris@16 436 * to \c indeterminate), all of which will be equivalent. The new name will be
Chris@16 437 * placed in the namespace in which the macro is expanded.
Chris@16 438 *
Chris@16 439 * Example:
Chris@16 440 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
Chris@16 441 *
Chris@16 442 * tribool x(true_or_false);
Chris@16 443 * // potentially set x
Chris@16 444 * if (true_or_false(x)) {
Chris@16 445 * // don't know what x is
Chris@16 446 * }
Chris@16 447 */
Chris@16 448 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
Chris@16 449 inline bool \
Chris@16 450 Name(boost::logic::tribool x, \
Chris@16 451 boost::logic::detail::indeterminate_t = \
Chris@16 452 boost::logic::detail::indeterminate_t()) \
Chris@16 453 { return x.value == boost::logic::tribool::indeterminate_value; }
Chris@16 454
Chris@16 455 #endif // BOOST_LOGIC_TRIBOOL_HPP
Chris@16 456