source: NonGTP/Boost/boost/logic/tribool.hpp @ 857

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