Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoKart_HS18/src/libraries/util/tribool.h @ 12209

Last change on this file since 12209 was 11096, checked in by muemart, 9 years ago

Constexpr for some util things

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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// Modifications by Orxonox to shape the third state into 'dontcare' instead
9// of 'indeterminate'. The difference is that 'dontcare' is actually a value
10// so that (dontcare == dontcare).
11// Also removed all logic operators except for == and !=
12// Added C++11 constexpr
13
14
15// For more information, see http://www.boost.org
16#ifndef ORXONOX_TRIBOOL_H
17#define ORXONOX_TRIBOOL_H
18
19namespace orxonox {
20
21/**
22 * INTERNAL ONLY
23 * The type of the 'dontcare' keyword.
24 */
25struct dontcare_keyword_t {};
26
27/**
28 * \brief Keyword for the dontcare tribool value
29 */
30constexpr dontcare_keyword_t dontcare = dontcare_keyword_t();
31
32/**
33 * \brief A 3-state boolean type.
34 *
35 * 3-state boolean values are either true, false, or
36 * dontcare.
37 */
38class tribool
39{
40public:
41  /**
42   * Construct a new 3-state boolean value with the value 'false'.
43   *
44   * \throws nothrow
45   */
46  constexpr tribool() : value(false_value) {}
47
48  /**
49   * Construct a new 3-state boolean value with the given boolean
50   * value, which may be \c true or \c false.
51   *
52   * \throws nothrow
53   */
54  constexpr tribool(bool value) : value(value? true_value : false_value) {}
55
56  /**
57   * Construct a new 3-state boolean value with an dontcare value.
58   *
59   * \throws nothrow
60   */
61  constexpr tribool(dontcare_keyword_t) : value(dontcare_value) {}
62
63  /**
64   * \brief Compare tribools for equality
65   *
66   * \returns the result of comparing two tribool values.
67   * \throws nothrow
68   */
69  constexpr bool operator==(tribool y) const
70  {
71    return (this->value == y.value);
72  }
73
74  /**
75   * \overload
76   */
77  constexpr bool operator==(bool y) const { return (*this) == tribool(y); }
78
79  /**
80   * \overload
81   */
82  constexpr bool operator==(dontcare_keyword_t) const
83  { return tribool(dontcare) == (*this); }
84
85  /**
86   * \brief Compare tribools for inequality
87   *
88   * \returns the result of comparing two tribool values for inequality.
89   * \throws nothrow
90   */
91  constexpr bool operator!=(tribool y) const
92  {
93    return !((*this) == y);
94  }
95
96  /**
97   * \overload
98   */
99  constexpr bool operator!=(bool y) const { return (*this) != tribool(y); }
100
101  /**
102   * \overload
103   */
104  constexpr bool operator!=(dontcare_keyword_t) const
105  { return (*this) != tribool(dontcare); }
106
107  /**
108   * The actual stored value in this 3-state boolean, which may be false, true,
109   * or dontcare.
110   */
111  enum value_t { false_value, true_value, dontcare_value } value;
112};
113
114/**
115 * \overload
116 */
117constexpr bool operator==(bool x, tribool y) { return tribool(x) == y; }
118
119/**
120 * \overload
121 */
122constexpr bool operator==(dontcare_keyword_t, tribool x)
123{ return tribool(dontcare) == x; }
124
125/**
126 * \overload
127 */
128constexpr bool operator!=(bool x, tribool y) { return tribool(x) != y; }
129
130/**
131 * \overload
132 */
133constexpr bool operator!=(dontcare_keyword_t, tribool x)
134{ return tribool(dontcare) != x; }
135
136} // end namespace orxonox
137
138#endif // ORXONOX_TRIBOOL_H
139
Note: See TracBrowser for help on using the repository browser.