fix doc example typo
[boost.git] / boost / logic / tribool.hpp
blob229feb4843dd0c1ee6f1dba1a99e5f1aa9198252
1 // Three-state boolean logic library
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)
9 // For more information, see http://www.boost.org
10 #ifndef BOOST_LOGIC_TRIBOOL_HPP
11 #define BOOST_LOGIC_TRIBOOL_HPP
13 #include <boost/logic/tribool_fwd.hpp>
14 #include <boost/config.hpp>
15 #include <boost/detail/workaround.hpp>
17 #if BOOST_WORKAROUND(_MSC_VER, >= 1200)
18 # pragma once
19 #endif
21 namespace boost { namespace logic {
23 /// INTERNAL ONLY
24 namespace detail {
25 /**
26 * INTERNAL ONLY
28 * \brief A type used only to uniquely identify the 'indeterminate'
29 * function/keyword.
31 struct indeterminate_t
33 #if BOOST_WORKAROUND(__BORLANDC__, < 0x0600)
34 char dummy_; // BCB would use 8 bytes by default
35 #endif
38 } // end namespace detail
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.
46 typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
48 /**
49 * \brief Keyword and test function for the indeterminate tribool value
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.
59 * \returns <tt>x.value == tribool::indeterminate_value</tt>
60 * \throws nothrow
62 inline bool
63 indeterminate(tribool x,
64 detail::indeterminate_t dummy = detail::indeterminate_t());
66 /**
67 * \brief A 3-state boolean type.
69 * 3-state boolean values are either true, false, or
70 * indeterminate.
72 class tribool
74 private:
75 /// INTERNAL ONLY
76 struct dummy {
77 void nonnull() {};
80 typedef void (dummy::*safe_bool)();
82 public:
83 /**
84 * Construct a new 3-state boolean value with the value 'false'.
86 * \throws nothrow
88 tribool() : value(false_value) {}
90 /**
91 * Construct a new 3-state boolean value with the given boolean
92 * value, which may be \c true or \c false.
94 * \throws nothrow
96 tribool(bool value) : value(value? true_value : false_value) {}
98 /**
99 * Construct a new 3-state boolean value with an indeterminate value.
101 * \throws nothrow
103 tribool(indeterminate_keyword_t) : value(indeterminate_value) {}
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.
109 * \returns true if the 3-state boolean is true, false otherwise
110 * \throws nothrow
112 operator safe_bool() const
114 return value == true_value? &dummy::nonnull : 0;
118 * The actual stored value in this 3-state boolean, which may be false, true,
119 * or indeterminate.
121 enum value_t { false_value, true_value, indeterminate_value } value;
124 // Check if the given tribool has an indeterminate value. Also doubles as a
125 // keyword for the 'indeterminate' value
126 inline bool indeterminate(tribool x, detail::indeterminate_t)
128 return x.value == tribool::indeterminate_value;
131 /** @defgroup logical Logical operations
133 //@{
135 * \brief Computes the logical negation of a tribool
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
159 inline tribool operator!(tribool x)
161 return x.value == tribool::false_value? tribool(true)
162 :x.value == tribool::true_value? tribool(false)
163 :tribool(indeterminate);
167 * \brief Computes the logical conjuction of two tribools
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
199 inline tribool operator&&(tribool x, tribool y)
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;
210 * \overload
212 inline tribool operator&&(tribool x, bool y)
213 { return y? x : tribool(false); }
216 * \overload
218 inline tribool operator&&(bool x, tribool y)
219 { return x? y : tribool(false); }
222 * \overload
224 inline tribool operator&&(indeterminate_keyword_t, tribool x)
225 { return !x? tribool(false) : tribool(indeterminate); }
228 * \overload
230 inline tribool operator&&(tribool x, indeterminate_keyword_t)
231 { return !x? tribool(false) : tribool(indeterminate); }
234 * \brief Computes the logical disjunction of two tribools
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
266 inline tribool operator||(tribool x, tribool y)
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;
277 * \overload
279 inline tribool operator||(tribool x, bool y)
280 { return y? tribool(true) : x; }
283 * \overload
285 inline tribool operator||(bool x, tribool y)
286 { return x? tribool(true) : y; }
289 * \overload
291 inline tribool operator||(indeterminate_keyword_t, tribool x)
292 { return x? tribool(true) : tribool(indeterminate); }
295 * \overload
297 inline tribool operator||(tribool x, indeterminate_keyword_t)
298 { return x? tribool(true) : tribool(indeterminate); }
299 //@}
302 * \brief Compare tribools for equality
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
334 inline tribool operator==(tribool x, tribool y)
336 if (indeterminate(x) || indeterminate(y))
337 return indeterminate;
338 else
339 return (x && y) || (!x && !y);
343 * \overload
345 inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
348 * \overload
350 inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
353 * \overload
355 inline tribool operator==(indeterminate_keyword_t, tribool x)
356 { return tribool(indeterminate) == x; }
359 * \overload
361 inline tribool operator==(tribool x, indeterminate_keyword_t)
362 { return tribool(indeterminate) == x; }
365 * \brief Compare tribools for inequality
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
397 inline tribool operator!=(tribool x, tribool y)
399 if (indeterminate(x) || indeterminate(y))
400 return indeterminate;
401 else
402 return !((x && y) || (!x && !y));
406 * \overload
408 inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
411 * \overload
413 inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
416 * \overload
418 inline tribool operator!=(indeterminate_keyword_t, tribool x)
419 { return tribool(indeterminate) != x; }
422 * \overload
424 inline tribool operator!=(tribool x, indeterminate_keyword_t)
425 { return x != tribool(indeterminate); }
427 } } // end namespace boost::logic
429 // Pull tribool and indeterminate into namespace "boost"
430 namespace boost {
431 using logic::tribool;
432 using logic::indeterminate;
436 * \brief Declare a new name for the third state of a tribool
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.
443 * Example:
444 * BOOST_TRIBOOL_THIRD_STATE(true_or_false)
446 * tribool x(true_or_false);
447 * // potentially set x
448 * if (true_or_false(x)) {
449 * // don't know what x is
452 #define BOOST_TRIBOOL_THIRD_STATE(Name) \
453 inline bool \
454 Name(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; }
459 #endif // BOOST_LOGIC_TRIBOOL_HPP