annotate DEPENDENCIES/generic/include/boost/logic/tribool.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
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@16 17 #if BOOST_WORKAROUND(_MSC_VER, >= 1200)
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@16 62 inline bool
Chris@16 63 indeterminate(tribool x,
Chris@16 64 detail::indeterminate_t dummy = detail::indeterminate_t());
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@16 88 tribool() : 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@16 96 tribool(bool initial_value) : 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@16 103 tribool(indeterminate_keyword_t) : 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@16 112 operator safe_bool() const
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@16 126 inline bool indeterminate(tribool x, detail::indeterminate_t)
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@16 159 inline tribool operator!(tribool x)
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@16 199 inline tribool operator&&(tribool x, tribool y)
Chris@16 200 {
Chris@16 201 if (static_cast<bool>(!x) || static_cast<bool>(!y))
Chris@16 202 return false;
Chris@16 203 else if (static_cast<bool>(x) && static_cast<bool>(y))
Chris@16 204 return true;
Chris@16 205 else
Chris@16 206 return indeterminate;
Chris@16 207 }
Chris@16 208
Chris@16 209 /**
Chris@16 210 * \overload
Chris@16 211 */
Chris@16 212 inline tribool operator&&(tribool x, bool y)
Chris@16 213 { return y? x : tribool(false); }
Chris@16 214
Chris@16 215 /**
Chris@16 216 * \overload
Chris@16 217 */
Chris@16 218 inline tribool operator&&(bool x, tribool y)
Chris@16 219 { return x? y : tribool(false); }
Chris@16 220
Chris@16 221 /**
Chris@16 222 * \overload
Chris@16 223 */
Chris@16 224 inline tribool operator&&(indeterminate_keyword_t, tribool x)
Chris@16 225 { return !x? tribool(false) : tribool(indeterminate); }
Chris@16 226
Chris@16 227 /**
Chris@16 228 * \overload
Chris@16 229 */
Chris@16 230 inline tribool operator&&(tribool x, indeterminate_keyword_t)
Chris@16 231 { return !x? tribool(false) : tribool(indeterminate); }
Chris@16 232
Chris@16 233 /**
Chris@16 234 * \brief Computes the logical disjunction of two tribools
Chris@16 235 *
Chris@16 236 * \returns the result of logically ORing the two tribool values,
Chris@16 237 * according to the following table:
Chris@16 238 * <table border=1>
Chris@16 239 * <tr>
Chris@16 240 * <th><center><code>||</code></center></th>
Chris@16 241 * <th><center>false</center></th>
Chris@16 242 * <th><center>true</center></th>
Chris@16 243 * <th><center>indeterminate</center></th>
Chris@16 244 * </tr>
Chris@16 245 * <tr>
Chris@16 246 * <th><center>false</center></th>
Chris@16 247 * <td><center>false</center></td>
Chris@16 248 * <td><center>true</center></td>
Chris@16 249 * <td><center>indeterminate</center></td>
Chris@16 250 * </tr>
Chris@16 251 * <tr>
Chris@16 252 * <th><center>true</center></th>
Chris@16 253 * <td><center>true</center></td>
Chris@16 254 * <td><center>true</center></td>
Chris@16 255 * <td><center>true</center></td>
Chris@16 256 * </tr>
Chris@16 257 * <tr>
Chris@16 258 * <th><center>indeterminate</center></th>
Chris@16 259 * <td><center>indeterminate</center></td>
Chris@16 260 * <td><center>true</center></td>
Chris@16 261 * <td><center>indeterminate</center></td>
Chris@16 262 * </tr>
Chris@16 263 * </table>
Chris@16 264 * \throws nothrow
Chris@16 265 */
Chris@16 266 inline tribool operator||(tribool x, tribool y)
Chris@16 267 {
Chris@16 268 if (static_cast<bool>(!x) && static_cast<bool>(!y))
Chris@16 269 return false;
Chris@16 270 else if (static_cast<bool>(x) || static_cast<bool>(y))
Chris@16 271 return true;
Chris@16 272 else
Chris@16 273 return indeterminate;
Chris@16 274 }
Chris@16 275
Chris@16 276 /**
Chris@16 277 * \overload
Chris@16 278 */
Chris@16 279 inline tribool operator||(tribool x, bool y)
Chris@16 280 { return y? tribool(true) : x; }
Chris@16 281
Chris@16 282 /**
Chris@16 283 * \overload
Chris@16 284 */
Chris@16 285 inline tribool operator||(bool x, tribool y)
Chris@16 286 { return x? tribool(true) : y; }
Chris@16 287
Chris@16 288 /**
Chris@16 289 * \overload
Chris@16 290 */
Chris@16 291 inline tribool operator||(indeterminate_keyword_t, tribool x)
Chris@16 292 { return x? tribool(true) : tribool(indeterminate); }
Chris@16 293
Chris@16 294 /**
Chris@16 295 * \overload
Chris@16 296 */
Chris@16 297 inline tribool operator||(tribool x, indeterminate_keyword_t)
Chris@16 298 { return x? tribool(true) : tribool(indeterminate); }
Chris@16 299 //@}
Chris@16 300
Chris@16 301 /**
Chris@16 302 * \brief Compare tribools for equality
Chris@16 303 *
Chris@16 304 * \returns the result of comparing two tribool values, according to
Chris@16 305 * the following table:
Chris@16 306 * <table border=1>
Chris@16 307 * <tr>
Chris@16 308 * <th><center><code>==</code></center></th>
Chris@16 309 * <th><center>false</center></th>
Chris@16 310 * <th><center>true</center></th>
Chris@16 311 * <th><center>indeterminate</center></th>
Chris@16 312 * </tr>
Chris@16 313 * <tr>
Chris@16 314 * <th><center>false</center></th>
Chris@16 315 * <td><center>true</center></td>
Chris@16 316 * <td><center>false</center></td>
Chris@16 317 * <td><center>indeterminate</center></td>
Chris@16 318 * </tr>
Chris@16 319 * <tr>
Chris@16 320 * <th><center>true</center></th>
Chris@16 321 * <td><center>false</center></td>
Chris@16 322 * <td><center>true</center></td>
Chris@16 323 * <td><center>indeterminate</center></td>
Chris@16 324 * </tr>
Chris@16 325 * <tr>
Chris@16 326 * <th><center>indeterminate</center></th>
Chris@16 327 * <td><center>indeterminate</center></td>
Chris@16 328 * <td><center>indeterminate</center></td>
Chris@16 329 * <td><center>indeterminate</center></td>
Chris@16 330 * </tr>
Chris@16 331 * </table>
Chris@16 332 * \throws nothrow
Chris@16 333 */
Chris@16 334 inline tribool operator==(tribool x, tribool y)
Chris@16 335 {
Chris@16 336 if (indeterminate(x) || indeterminate(y))
Chris@16 337 return indeterminate;
Chris@16 338 else
Chris@16 339 return (x && y) || (!x && !y);
Chris@16 340 }
Chris@16 341
Chris@16 342 /**
Chris@16 343 * \overload
Chris@16 344 */
Chris@16 345 inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
Chris@16 346
Chris@16 347 /**
Chris@16 348 * \overload
Chris@16 349 */
Chris@16 350 inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
Chris@16 351
Chris@16 352 /**
Chris@16 353 * \overload
Chris@16 354 */
Chris@16 355 inline tribool operator==(indeterminate_keyword_t, tribool x)
Chris@16 356 { return tribool(indeterminate) == x; }
Chris@16 357
Chris@16 358 /**
Chris@16 359 * \overload
Chris@16 360 */
Chris@16 361 inline tribool operator==(tribool x, indeterminate_keyword_t)
Chris@16 362 { return tribool(indeterminate) == x; }
Chris@16 363
Chris@16 364 /**
Chris@16 365 * \brief Compare tribools for inequality
Chris@16 366 *
Chris@16 367 * \returns the result of comparing two tribool values for inequality,
Chris@16 368 * according to the following table:
Chris@16 369 * <table border=1>
Chris@16 370 * <tr>
Chris@16 371 * <th><center><code>!=</code></center></th>
Chris@16 372 * <th><center>false</center></th>
Chris@16 373 * <th><center>true</center></th>
Chris@16 374 * <th><center>indeterminate</center></th>
Chris@16 375 * </tr>
Chris@16 376 * <tr>
Chris@16 377 * <th><center>false</center></th>
Chris@16 378 * <td><center>false</center></td>
Chris@16 379 * <td><center>true</center></td>
Chris@16 380 * <td><center>indeterminate</center></td>
Chris@16 381 * </tr>
Chris@16 382 * <tr>
Chris@16 383 * <th><center>true</center></th>
Chris@16 384 * <td><center>true</center></td>
Chris@16 385 * <td><center>false</center></td>
Chris@16 386 * <td><center>indeterminate</center></td>
Chris@16 387 * </tr>
Chris@16 388 * <tr>
Chris@16 389 * <th><center>indeterminate</center></th>
Chris@16 390 * <td><center>indeterminate</center></td>
Chris@16 391 * <td><center>indeterminate</center></td>
Chris@16 392 * <td><center>indeterminate</center></td>
Chris@16 393 * </tr>
Chris@16 394 * </table>
Chris@16 395 * \throws nothrow
Chris@16 396 */
Chris@16 397 inline tribool operator!=(tribool x, tribool y)
Chris@16 398 {
Chris@16 399 if (indeterminate(x) || indeterminate(y))
Chris@16 400 return indeterminate;
Chris@16 401 else
Chris@16 402 return !((x && y) || (!x && !y));
Chris@16 403 }
Chris@16 404
Chris@16 405 /**
Chris@16 406 * \overload
Chris@16 407 */
Chris@16 408 inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
Chris@16 409
Chris@16 410 /**
Chris@16 411 * \overload
Chris@16 412 */
Chris@16 413 inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
Chris@16 414
Chris@16 415 /**
Chris@16 416 * \overload
Chris@16 417 */
Chris@16 418 inline tribool operator!=(indeterminate_keyword_t, tribool x)
Chris@16 419 { return tribool(indeterminate) != x; }
Chris@16 420
Chris@16 421 /**
Chris@16 422 * \overload
Chris@16 423 */
Chris@16 424 inline tribool operator!=(tribool x, indeterminate_keyword_t)
Chris@16 425 { return x != tribool(indeterminate); }
Chris@16 426
Chris@16 427 } } // end namespace boost::logic
Chris@16 428
Chris@16 429 // Pull tribool and indeterminate into namespace "boost"
Chris@16 430 namespace boost {
Chris@16 431 using logic::tribool;
Chris@16 432 using logic::indeterminate;
Chris@16 433 }
Chris@16 434
Chris@16 435 /**
Chris@16 436 * \brief Declare a new name for the third state of a tribool
Chris@16 437 *
Chris@16 438 * Use this macro to declare a new name for the third state of a
Chris@16 439 * tribool. This state can have any number of new names (in addition
Chris@16 440 * to \c indeterminate), all of which will be equivalent. The new name will be
Chris@16 441 * placed in the namespace in which the macro is expanded.
Chris@16 442 *
Chris@16 443 * Example:
Chris@16 444 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
Chris@16 445 *
Chris@16 446 * tribool x(true_or_false);
Chris@16 447 * // potentially set x
Chris@16 448 * if (true_or_false(x)) {
Chris@16 449 * // don't know what x is
Chris@16 450 * }
Chris@16 451 */
Chris@16 452 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
Chris@16 453 inline bool \
Chris@16 454 Name(boost::logic::tribool x, \
Chris@16 455 boost::logic::detail::indeterminate_t = \
Chris@16 456 boost::logic::detail::indeterminate_t()) \
Chris@16 457 { return x.value == boost::logic::tribool::indeterminate_value; }
Chris@16 458
Chris@16 459 #endif // BOOST_LOGIC_TRIBOOL_HPP
Chris@16 460