staticlib_support  1.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
tribool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Copyright Douglas Gregor 2002-2004. Use, modification and
18 // distribution is subject to the Boost Software License, Version
19 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
20 // http://www.pion.org/LICENSE_1_0.txt)
21 
22 #ifndef STATICLIB_SUPPORT_TRIBOOL_HPP
23 #define STATICLIB_SUPPORT_TRIBOOL_HPP
24 
25 namespace staticlib { namespace support { namespace logic {
26 
27 class tribool;
28 
30 namespace detail {
31 
39 };
40 
41 } // end namespace detail
42 
50 
65 inline bool
68 
75 class tribool
76 {
77 private:
79  struct dummy {
80  void nonnull() {};
81  };
82 
83  typedef void (dummy::*safe_bool)();
84 
85 public:
92 
99  tribool(bool initial_value) : value(initial_value? true_value : false_value) {}
100 
107 
115  operator safe_bool() const
116  {
117  return value == true_value? &dummy::nonnull : 0;
118  }
119 
125 };
126 
127 // Check if the given tribool has an indeterminate value. Also doubles as a
128 // keyword for the 'indeterminate' value
130 {
132 }
133 
163 {
164  return x.value == tribool::false_value? tribool(true)
165  :x.value == tribool::true_value? tribool(false)
166  :tribool(indeterminate);
167 }
168 
203 {
204  if (static_cast<bool>(!x) || static_cast<bool>(!y))
205  return false;
206  else if (static_cast<bool>(x) && static_cast<bool>(y))
207  return true;
208  else
209  return indeterminate;
210 }
211 
215 inline tribool operator&&(tribool x, bool y)
216 { return y? x : tribool(false); }
217 
221 inline tribool operator&&(bool x, tribool y)
222 { return x? y : tribool(false); }
223 
228 { return !x? tribool(false) : tribool(indeterminate); }
229 
234 { return !x? tribool(false) : tribool(indeterminate); }
235 
270 {
271  if (static_cast<bool>(!x) && static_cast<bool>(!y))
272  return false;
273  else if (static_cast<bool>(x) || static_cast<bool>(y))
274  return true;
275  else
276  return indeterminate;
277 }
278 
282 inline tribool operator||(tribool x, bool y)
283 { return y? tribool(true) : x; }
284 
288 inline tribool operator||(bool x, tribool y)
289 { return x? tribool(true) : y; }
290 
295 { return x? tribool(true) : tribool(indeterminate); }
296 
301 { return x? tribool(true) : tribool(indeterminate); }
303 
338 {
339  if (indeterminate(x) || indeterminate(y))
340  return indeterminate;
341  else
342  return (x && y) || (!x && !y);
343 }
344 
348 inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
349 
353 inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
354 
359 { return tribool(indeterminate) == x; }
360 
365 { return tribool(indeterminate) == x; }
366 
401 {
402  if (indeterminate(x) || indeterminate(y))
403  return indeterminate;
404  else
405  return !((x && y) || (!x && !y));
406 }
407 
411 inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
412 
416 inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
417 
422 { return tribool(indeterminate) != x; }
423 
428 { return x != tribool(indeterminate); }
429 
430 } } } // end namespace staticlib::support::logic
431 
432 // Pull tribool and indeterminate into namespace "sl::support"
433 namespace staticlib { namespace support {
434  using logic::tribool;
435  using logic::indeterminate;
436 } }
437 
455 #define STATICLIB_HTTPSERVER_TRIBOOL_THIRD_STATE(Name) \
456 inline bool \
457 Name(staticlib::support::logic::tribool x, \
458  staticlib::support::logic::detail::indeterminate_t = \
459  staticlib::support::logic::detail::indeterminate_t()) \
460 { return x.value == staticlib::support::logic::tribool::indeterminate_value; }
461 
462 #endif // STATICLIB_SUPPORT_TRIBOOL_HPP
463