1 // TR2 <bool_set> -*- C++ -*-
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr2/bool_set
26 * This is a TR2 C++ Library header.
29 #ifndef _GLIBCXX_TR2_BOOL_SET
30 #define _GLIBCXX_TR2_BOOL_SET 1
32 #pragma GCC system_header
37 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * See N2136, Bool_set: multi-valued logic
47 * by Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion.
49 * The implicit conversion to bool is slippery! I may use the new
50 * explicit conversion. This has been specialized in the language
51 * so that in contexts requiring a bool the conversion happens
52 * implicitly. Thus most objections should be eliminated.
58 /// Default constructor.
59 constexpr bool_set() : _M_b(_S_false) { }
61 /// Constructor from bool.
62 constexpr bool_set(bool __t) : _M_b(_Bool_set_val(__t)) { }
64 // I'm not sure about this.
65 bool contains(bool_set __b) const
66 { return this->is_singleton() && this->equals(__b); }
68 /// Return true if states are equal.
69 bool equals(bool_set __b) const
70 { return __b._M_b == _M_b; }
72 /// Return true if this is empty.
73 bool is_emptyset() const
74 { return _M_b == _S_empty; }
76 /// Return true if this is indeterminate.
77 bool is_indeterminate() const
78 { return _M_b == _S_indet; }
80 /// Return true if this is false or true (normal boolean).
81 bool is_singleton() const
82 { return _M_b == _S_false || _M_b == _S_true_; }
84 /// Conversion to bool.
89 throw std::bad_cast();
94 static bool_set indeterminate()
102 static bool_set emptyset()
110 operator!(bool_set __b)
111 { return __b._M_not(); }
114 operator^(bool_set __s, bool_set __t)
115 { return __s._M_xor(__t); }
118 operator|(bool_set __s, bool_set __t)
119 { return __s._M_or(__t); }
122 operator&(bool_set __s, bool_set __t)
123 { return __s._M_and(__t); }
126 operator==(bool_set __s, bool_set __t)
127 { return __s._M_eq(__t); }
130 // These overloads replace the facet additions in the paper!
132 template<typename CharT, typename Traits>
133 friend std::basic_ostream<CharT, Traits>&
134 operator<<(std::basic_ostream<CharT, Traits>& __out, bool_set __b)
140 template<typename CharT, typename Traits>
141 friend std::basic_istream<CharT, Traits>&
142 operator>>(std::basic_istream<CharT, Traits>& __in, bool_set& __b)
146 if (__c >= _S_false && __c < _S_empty)
147 __b._M_b = static_cast<_Bool_set_val>(__c);
153 enum _Bool_set_val: unsigned char
165 bool_set(_Bool_set_val __c) : _M_b(__c) { }
168 bool_set _M_not() const
169 { return _S_not[this->_M_b]; }
172 bool_set _M_xor(bool_set __b) const
173 { return _S_xor[this->_M_b][__b._M_b]; }
176 bool_set _M_or(bool_set __b) const
177 { return _S_or[this->_M_b][__b._M_b]; }
180 bool_set _M_and(bool_set __b) const
181 { return _S_and[this->_M_b][__b._M_b]; }
184 bool_set _M_eq(bool_set __b) const
185 { return _S_eq[this->_M_b][__b._M_b]; }
188 static _Bool_set_val _S_not[4];
191 static _Bool_set_val _S_xor[4][4];
194 static _Bool_set_val _S_or[4][4];
197 static _Bool_set_val _S_and[4][4];
200 static _Bool_set_val _S_eq[4][4];
203 // 20.2.3.2 bool_set values
206 contains(bool_set __s, bool_set __t)
207 { return __s.contains(__t); }
210 equals(bool_set __s, bool_set __t)
211 { return __s.equals(__t); }
214 is_emptyset(bool_set __b)
215 { return __b.is_emptyset(); }
218 is_indeterminate(bool_set __b)
219 { return __b.is_indeterminate(); }
222 is_singleton(bool_set __b)
223 { return __b.is_singleton(); }
226 certainly(bool_set __b)
227 { return ! __b.contains(false); }
230 possibly(bool_set __b)
231 { return __b.contains(true); }
234 // 20.2.3.3 bool_set set operations
237 set_union(bool __s, bool_set __t)
238 { return bool_set(__s) | __t; }
241 set_union(bool_set __s, bool __t)
242 { return __s | bool_set(__t); }
245 set_union(bool_set __s, bool_set __t)
246 { return __s | __t; }
249 set_intersection(bool __s, bool_set __t)
250 { return bool_set(__s) & __t; }
253 set_intersection(bool_set __s, bool __t)
254 { return __s & bool_set(__t); }
257 set_intersection(bool_set __s, bool_set __t)
258 { return __s & __t; }
261 set_complement(bool_set __b)
265 // 20.2.3.4 bool_set logical operators
268 operator^(bool __s, bool_set __t)
269 { return bool_set(__s) ^ __t; }
272 operator^(bool_set __s, bool __t)
273 { return __s ^ bool_set(__t); }
276 operator|(bool __s, bool_set __t)
277 { return bool_set(__s) | __t; }
280 operator|(bool_set __s, bool __t)
281 { return __s | bool_set(__t); }
284 operator&(bool __s, bool_set __t)
285 { return bool_set(__s) & __t; }
288 operator&(bool_set __s, bool __t)
289 { return __s & bool_set(__t); }
292 // 20.2.3.5 bool_set relational operators
295 operator==(bool __s, bool_set __t)
296 { return bool_set(__s) == __t; }
299 operator==(bool_set __s, bool __t)
300 { return __s == bool_set(__t); }
303 operator!=(bool __s, bool_set __t)
304 { return ! (__s == __t); }
307 operator!=(bool_set __s, bool __t)
308 { return ! (__s == __t); }
311 operator!=(bool_set __s, bool_set __t)
312 { return ! (__s == __t); }
314 _GLIBCXX_END_NAMESPACE_VERSION
318 #include <tr2/bool_set.tcc>
320 #endif // _GLIBCXX_TR2_BOOL_SET