1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/logic/tribool.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,460 @@
1.4 +// Three-state boolean logic library
1.5 +
1.6 +// Copyright Douglas Gregor 2002-2004. Use, modification and
1.7 +// distribution is subject to the Boost Software License, Version
1.8 +// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1.9 +// http://www.boost.org/LICENSE_1_0.txt)
1.10 +
1.11 +
1.12 +// For more information, see http://www.boost.org
1.13 +#ifndef BOOST_LOGIC_TRIBOOL_HPP
1.14 +#define BOOST_LOGIC_TRIBOOL_HPP
1.15 +
1.16 +#include <boost/logic/tribool_fwd.hpp>
1.17 +#include <boost/config.hpp>
1.18 +#include <boost/detail/workaround.hpp>
1.19 +
1.20 +#if BOOST_WORKAROUND(_MSC_VER, >= 1200)
1.21 +# pragma once
1.22 +#endif
1.23 +
1.24 +namespace boost { namespace logic {
1.25 +
1.26 +/// INTERNAL ONLY
1.27 +namespace detail {
1.28 +/**
1.29 + * INTERNAL ONLY
1.30 + *
1.31 + * \brief A type used only to uniquely identify the 'indeterminate'
1.32 + * function/keyword.
1.33 + */
1.34 +struct indeterminate_t
1.35 +{
1.36 +#if BOOST_WORKAROUND(__BORLANDC__, < 0x0600)
1.37 + char dummy_; // BCB would use 8 bytes by default
1.38 +#endif
1.39 +};
1.40 +
1.41 +} // end namespace detail
1.42 +
1.43 +/**
1.44 + * INTERNAL ONLY
1.45 + * The type of the 'indeterminate' keyword. This has the same type as the
1.46 + * function 'indeterminate' so that we can recognize when the keyword is
1.47 + * used.
1.48 + */
1.49 +typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
1.50 +
1.51 +/**
1.52 + * \brief Keyword and test function for the indeterminate tribool value
1.53 + *
1.54 + * The \c indeterminate function has a dual role. It's first role is
1.55 + * as a unary function that tells whether the tribool value is in the
1.56 + * "indeterminate" state. It's second role is as a keyword
1.57 + * representing the indeterminate (just like "true" and "false"
1.58 + * represent the true and false states). If you do not like the name
1.59 + * "indeterminate", and would prefer to use a different name, see the
1.60 + * macro \c BOOST_TRIBOOL_THIRD_STATE.
1.61 + *
1.62 + * \returns <tt>x.value == tribool::indeterminate_value</tt>
1.63 + * \throws nothrow
1.64 + */
1.65 +inline bool
1.66 +indeterminate(tribool x,
1.67 + detail::indeterminate_t dummy = detail::indeterminate_t());
1.68 +
1.69 +/**
1.70 + * \brief A 3-state boolean type.
1.71 + *
1.72 + * 3-state boolean values are either true, false, or
1.73 + * indeterminate.
1.74 + */
1.75 +class tribool
1.76 +{
1.77 +private:
1.78 + /// INTERNAL ONLY
1.79 + struct dummy {
1.80 + void nonnull() {};
1.81 + };
1.82 +
1.83 + typedef void (dummy::*safe_bool)();
1.84 +
1.85 +public:
1.86 + /**
1.87 + * Construct a new 3-state boolean value with the value 'false'.
1.88 + *
1.89 + * \throws nothrow
1.90 + */
1.91 + tribool() : value(false_value) {}
1.92 +
1.93 + /**
1.94 + * Construct a new 3-state boolean value with the given boolean
1.95 + * value, which may be \c true or \c false.
1.96 + *
1.97 + * \throws nothrow
1.98 + */
1.99 + tribool(bool value) : value(value? true_value : false_value) {}
1.100 +
1.101 + /**
1.102 + * Construct a new 3-state boolean value with an indeterminate value.
1.103 + *
1.104 + * \throws nothrow
1.105 + */
1.106 + tribool(indeterminate_keyword_t) : value(indeterminate_value) {}
1.107 +
1.108 + /**
1.109 + * Use a 3-state boolean in a boolean context. Will evaluate true in a
1.110 + * boolean context only when the 3-state boolean is definitely true.
1.111 + *
1.112 + * \returns true if the 3-state boolean is true, false otherwise
1.113 + * \throws nothrow
1.114 + */
1.115 + operator safe_bool() const
1.116 + {
1.117 + return value == true_value? &dummy::nonnull : 0;
1.118 + }
1.119 +
1.120 + /**
1.121 + * The actual stored value in this 3-state boolean, which may be false, true,
1.122 + * or indeterminate.
1.123 + */
1.124 + enum value_t { false_value, true_value, indeterminate_value } value;
1.125 +};
1.126 +
1.127 +// Check if the given tribool has an indeterminate value. Also doubles as a
1.128 +// keyword for the 'indeterminate' value
1.129 +inline bool indeterminate(tribool x, detail::indeterminate_t)
1.130 +{
1.131 + return x.value == tribool::indeterminate_value;
1.132 +}
1.133 +
1.134 +/** @defgroup logical Logical operations
1.135 + */
1.136 +//@{
1.137 +/**
1.138 + * \brief Computes the logical negation of a tribool
1.139 + *
1.140 + * \returns the logical negation of the tribool, according to the
1.141 + * table:
1.142 + * <table border=1>
1.143 + * <tr>
1.144 + * <th><center><code>!</code></center></th>
1.145 + * <th/>
1.146 + * </tr>
1.147 + * <tr>
1.148 + * <th><center>false</center></th>
1.149 + * <td><center>true</center></td>
1.150 + * </tr>
1.151 + * <tr>
1.152 + * <th><center>true</center></th>
1.153 + * <td><center>false</center></td>
1.154 + * </tr>
1.155 + * <tr>
1.156 + * <th><center>indeterminate</center></th>
1.157 + * <td><center>indeterminate</center></td>
1.158 + * </tr>
1.159 + * </table>
1.160 + * \throws nothrow
1.161 + */
1.162 +inline tribool operator!(tribool x)
1.163 +{
1.164 + return x.value == tribool::false_value? tribool(true)
1.165 + :x.value == tribool::true_value? tribool(false)
1.166 + :tribool(indeterminate);
1.167 +}
1.168 +
1.169 +/**
1.170 + * \brief Computes the logical conjuction of two tribools
1.171 + *
1.172 + * \returns the result of logically ANDing the two tribool values,
1.173 + * according to the following table:
1.174 + * <table border=1>
1.175 + * <tr>
1.176 + * <th><center><code>&&</code></center></th>
1.177 + * <th><center>false</center></th>
1.178 + * <th><center>true</center></th>
1.179 + * <th><center>indeterminate</center></th>
1.180 + * </tr>
1.181 + * <tr>
1.182 + * <th><center>false</center></th>
1.183 + * <td><center>false</center></td>
1.184 + * <td><center>false</center></td>
1.185 + * <td><center>false</center></td>
1.186 + * </tr>
1.187 + * <tr>
1.188 + * <th><center>true</center></th>
1.189 + * <td><center>false</center></td>
1.190 + * <td><center>true</center></td>
1.191 + * <td><center>indeterminate</center></td>
1.192 + * </tr>
1.193 + * <tr>
1.194 + * <th><center>indeterminate</center></th>
1.195 + * <td><center>false</center></td>
1.196 + * <td><center>indeterminate</center></td>
1.197 + * <td><center>indeterminate</center></td>
1.198 + * </tr>
1.199 + * </table>
1.200 + * \throws nothrow
1.201 + */
1.202 +inline tribool operator&&(tribool x, tribool y)
1.203 +{
1.204 + if (static_cast<bool>(!x) || static_cast<bool>(!y))
1.205 + return false;
1.206 + else if (static_cast<bool>(x) && static_cast<bool>(y))
1.207 + return true;
1.208 + else
1.209 + return indeterminate;
1.210 +}
1.211 +
1.212 +/**
1.213 + * \overload
1.214 + */
1.215 +inline tribool operator&&(tribool x, bool y)
1.216 +{ return y? x : tribool(false); }
1.217 +
1.218 +/**
1.219 + * \overload
1.220 + */
1.221 +inline tribool operator&&(bool x, tribool y)
1.222 +{ return x? y : tribool(false); }
1.223 +
1.224 +/**
1.225 + * \overload
1.226 + */
1.227 +inline tribool operator&&(indeterminate_keyword_t, tribool x)
1.228 +{ return !x? tribool(false) : tribool(indeterminate); }
1.229 +
1.230 +/**
1.231 + * \overload
1.232 + */
1.233 +inline tribool operator&&(tribool x, indeterminate_keyword_t)
1.234 +{ return !x? tribool(false) : tribool(indeterminate); }
1.235 +
1.236 +/**
1.237 + * \brief Computes the logical disjunction of two tribools
1.238 + *
1.239 + * \returns the result of logically ORing the two tribool values,
1.240 + * according to the following table:
1.241 + * <table border=1>
1.242 + * <tr>
1.243 + * <th><center><code>||</code></center></th>
1.244 + * <th><center>false</center></th>
1.245 + * <th><center>true</center></th>
1.246 + * <th><center>indeterminate</center></th>
1.247 + * </tr>
1.248 + * <tr>
1.249 + * <th><center>false</center></th>
1.250 + * <td><center>false</center></td>
1.251 + * <td><center>true</center></td>
1.252 + * <td><center>indeterminate</center></td>
1.253 + * </tr>
1.254 + * <tr>
1.255 + * <th><center>true</center></th>
1.256 + * <td><center>true</center></td>
1.257 + * <td><center>true</center></td>
1.258 + * <td><center>true</center></td>
1.259 + * </tr>
1.260 + * <tr>
1.261 + * <th><center>indeterminate</center></th>
1.262 + * <td><center>indeterminate</center></td>
1.263 + * <td><center>true</center></td>
1.264 + * <td><center>indeterminate</center></td>
1.265 + * </tr>
1.266 + * </table>
1.267 + * \throws nothrow
1.268 + */
1.269 +inline tribool operator||(tribool x, tribool y)
1.270 +{
1.271 + if (static_cast<bool>(!x) && static_cast<bool>(!y))
1.272 + return false;
1.273 + else if (static_cast<bool>(x) || static_cast<bool>(y))
1.274 + return true;
1.275 + else
1.276 + return indeterminate;
1.277 +}
1.278 +
1.279 +/**
1.280 + * \overload
1.281 + */
1.282 +inline tribool operator||(tribool x, bool y)
1.283 +{ return y? tribool(true) : x; }
1.284 +
1.285 +/**
1.286 + * \overload
1.287 + */
1.288 +inline tribool operator||(bool x, tribool y)
1.289 +{ return x? tribool(true) : y; }
1.290 +
1.291 +/**
1.292 + * \overload
1.293 + */
1.294 +inline tribool operator||(indeterminate_keyword_t, tribool x)
1.295 +{ return x? tribool(true) : tribool(indeterminate); }
1.296 +
1.297 +/**
1.298 + * \overload
1.299 + */
1.300 +inline tribool operator||(tribool x, indeterminate_keyword_t)
1.301 +{ return x? tribool(true) : tribool(indeterminate); }
1.302 +//@}
1.303 +
1.304 +/**
1.305 + * \brief Compare tribools for equality
1.306 + *
1.307 + * \returns the result of comparing two tribool values, according to
1.308 + * the following table:
1.309 + * <table border=1>
1.310 + * <tr>
1.311 + * <th><center><code>==</code></center></th>
1.312 + * <th><center>false</center></th>
1.313 + * <th><center>true</center></th>
1.314 + * <th><center>indeterminate</center></th>
1.315 + * </tr>
1.316 + * <tr>
1.317 + * <th><center>false</center></th>
1.318 + * <td><center>true</center></td>
1.319 + * <td><center>false</center></td>
1.320 + * <td><center>indeterminate</center></td>
1.321 + * </tr>
1.322 + * <tr>
1.323 + * <th><center>true</center></th>
1.324 + * <td><center>false</center></td>
1.325 + * <td><center>true</center></td>
1.326 + * <td><center>indeterminate</center></td>
1.327 + * </tr>
1.328 + * <tr>
1.329 + * <th><center>indeterminate</center></th>
1.330 + * <td><center>indeterminate</center></td>
1.331 + * <td><center>indeterminate</center></td>
1.332 + * <td><center>indeterminate</center></td>
1.333 + * </tr>
1.334 + * </table>
1.335 + * \throws nothrow
1.336 + */
1.337 +inline tribool operator==(tribool x, tribool y)
1.338 +{
1.339 + if (indeterminate(x) || indeterminate(y))
1.340 + return indeterminate;
1.341 + else
1.342 + return x && y || !x && !y;
1.343 +}
1.344 +
1.345 +/**
1.346 + * \overload
1.347 + */
1.348 +inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
1.349 +
1.350 +/**
1.351 + * \overload
1.352 + */
1.353 +inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
1.354 +
1.355 +/**
1.356 + * \overload
1.357 + */
1.358 +inline tribool operator==(indeterminate_keyword_t, tribool x)
1.359 +{ return tribool(indeterminate) == x; }
1.360 +
1.361 +/**
1.362 + * \overload
1.363 + */
1.364 +inline tribool operator==(tribool x, indeterminate_keyword_t)
1.365 +{ return tribool(indeterminate) == x; }
1.366 +
1.367 +/**
1.368 + * \brief Compare tribools for inequality
1.369 + *
1.370 + * \returns the result of comparing two tribool values for inequality,
1.371 + * according to the following table:
1.372 + * <table border=1>
1.373 + * <tr>
1.374 + * <th><center><code>!=</code></center></th>
1.375 + * <th><center>false</center></th>
1.376 + * <th><center>true</center></th>
1.377 + * <th><center>indeterminate</center></th>
1.378 + * </tr>
1.379 + * <tr>
1.380 + * <th><center>false</center></th>
1.381 + * <td><center>false</center></td>
1.382 + * <td><center>true</center></td>
1.383 + * <td><center>indeterminate</center></td>
1.384 + * </tr>
1.385 + * <tr>
1.386 + * <th><center>true</center></th>
1.387 + * <td><center>true</center></td>
1.388 + * <td><center>false</center></td>
1.389 + * <td><center>indeterminate</center></td>
1.390 + * </tr>
1.391 + * <tr>
1.392 + * <th><center>indeterminate</center></th>
1.393 + * <td><center>indeterminate</center></td>
1.394 + * <td><center>indeterminate</center></td>
1.395 + * <td><center>indeterminate</center></td>
1.396 + * </tr>
1.397 + * </table>
1.398 + * \throws nothrow
1.399 + */
1.400 +inline tribool operator!=(tribool x, tribool y)
1.401 +{
1.402 + if (indeterminate(x) || indeterminate(y))
1.403 + return indeterminate;
1.404 + else
1.405 + return !(x && y || !x && !y);
1.406 +}
1.407 +
1.408 +/**
1.409 + * \overload
1.410 + */
1.411 +inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
1.412 +
1.413 +/**
1.414 + * \overload
1.415 + */
1.416 +inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
1.417 +
1.418 +/**
1.419 + * \overload
1.420 + */
1.421 +inline tribool operator!=(indeterminate_keyword_t, tribool x)
1.422 +{ return tribool(indeterminate) != x; }
1.423 +
1.424 +/**
1.425 + * \overload
1.426 + */
1.427 +inline tribool operator!=(tribool x, indeterminate_keyword_t)
1.428 +{ return x != tribool(indeterminate); }
1.429 +
1.430 +} } // end namespace boost::logic
1.431 +
1.432 +// Pull tribool and indeterminate into namespace "boost"
1.433 +namespace boost {
1.434 + using logic::tribool;
1.435 + using logic::indeterminate;
1.436 +}
1.437 +
1.438 +/**
1.439 + * \brief Declare a new name for the third state of a tribool
1.440 + *
1.441 + * Use this macro to declare a new name for the third state of a
1.442 + * tribool. This state can have any number of new names (in addition
1.443 + * to \c indeterminate), all of which will be equivalent. The new name will be
1.444 + * placed in the namespace in which the macro is expanded.
1.445 + *
1.446 + * Example:
1.447 + * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
1.448 + *
1.449 + * tribool x(true_or_false);
1.450 + * // potentially set x
1.451 + * if (true_or_false(x)) {
1.452 + * // don't know what x is
1.453 + * }
1.454 + */
1.455 +#define BOOST_TRIBOOL_THIRD_STATE(Name) \
1.456 +inline bool \
1.457 +Name(boost::logic::tribool x, \
1.458 + boost::logic::detail::indeterminate_t dummy = \
1.459 + boost::logic::detail::indeterminate_t()) \
1.460 +{ return x.value == boost::logic::tribool::indeterminate_value; }
1.461 +
1.462 +#endif // BOOST_LOGIC_TRIBOOL_HPP
1.463 +