]>
Commit | Line | Data |
---|---|---|
285b36d6 BK |
1 | // Debugging bitset implementation -*- C++ -*- |
2 | ||
5d861bf2 | 3 | // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
47cd1557 | 4 | // Free Software Foundation, Inc. |
285b36d6 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
285b36d6 BK |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | // You should have received a copy of the GNU General Public License and | |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
285b36d6 | 25 | |
78a53887 BK |
26 | /** @file debug/bitset |
27 | * This file is a GNU debug extension to the Standard C++ Library. | |
28 | */ | |
29 | ||
285b36d6 BK |
30 | #ifndef _GLIBCXX_DEBUG_BITSET |
31 | #define _GLIBCXX_DEBUG_BITSET | |
32 | ||
33 | #include <bitset> | |
34 | #include <debug/safe_sequence.h> | |
35 | #include <debug/safe_iterator.h> | |
36 | ||
12ffa228 | 37 | namespace std _GLIBCXX_VISIBILITY(default) |
3cbc7af0 | 38 | { |
45f388bb | 39 | namespace __debug |
526da49c | 40 | { |
1ceb9e06 | 41 | /// Class std::bitset with additional safety/checking/debug instrumentation. |
526da49c | 42 | template<size_t _Nb> |
285b36d6 | 43 | class bitset |
12ffa228 | 44 | : public _GLIBCXX_STD_C::bitset<_Nb> |
734f5023 | 45 | #if __cplusplus < 201103L |
17e3f4aa PC |
46 | , public __gnu_debug::_Safe_sequence_base |
47 | #endif | |
285b36d6 | 48 | { |
12ffa228 | 49 | typedef _GLIBCXX_STD_C::bitset<_Nb> _Base; |
285b36d6 BK |
50 | |
51 | public: | |
c5589aa7 FD |
52 | // In C++0x we rely on normal reference type to preserve the property |
53 | // of bitset to be use as a literal. | |
fc09e5b6 | 54 | // TODO: Find another solution. |
734f5023 | 55 | #if __cplusplus >= 201103L |
c5589aa7 FD |
56 | typedef typename _Base::reference reference; |
57 | #else | |
285b36d6 | 58 | // bit reference: |
526da49c | 59 | class reference |
17e3f4aa | 60 | : private _Base::reference |
17e3f4aa | 61 | , public __gnu_debug::_Safe_iterator_base |
285b36d6 BK |
62 | { |
63 | typedef typename _Base::reference _Base_ref; | |
64 | ||
65 | friend class bitset; | |
66 | reference(); | |
526da49c | 67 | |
17e3f4aa | 68 | reference(const _Base_ref& __base, |
5d861bf2 | 69 | bitset* __seq __attribute__((__unused__))) _GLIBCXX_NOEXCEPT |
17e3f4aa | 70 | : _Base_ref(__base) |
17e3f4aa | 71 | , _Safe_iterator_base(__seq, false) |
285b36d6 BK |
72 | { } |
73 | ||
74 | public: | |
5d861bf2 | 75 | reference(const reference& __x) _GLIBCXX_NOEXCEPT |
17e3f4aa | 76 | : _Base_ref(__x) |
17e3f4aa | 77 | , _Safe_iterator_base(__x, false) |
285b36d6 BK |
78 | { } |
79 | ||
526da49c | 80 | reference& |
5d861bf2 | 81 | operator=(bool __x) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
82 | { |
83 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), | |
3cbc7af0 | 84 | _M_message(__gnu_debug::__msg_bad_bitset_write) |
285b36d6 BK |
85 | ._M_iterator(*this)); |
86 | *static_cast<_Base_ref*>(this) = __x; | |
87 | return *this; | |
88 | } | |
89 | ||
526da49c | 90 | reference& |
5d861bf2 | 91 | operator=(const reference& __x) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
92 | { |
93 | _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(), | |
3cbc7af0 | 94 | _M_message(__gnu_debug::__msg_bad_bitset_read) |
285b36d6 BK |
95 | ._M_iterator(__x)); |
96 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), | |
3cbc7af0 | 97 | _M_message(__gnu_debug::__msg_bad_bitset_write) |
285b36d6 BK |
98 | ._M_iterator(*this)); |
99 | *static_cast<_Base_ref*>(this) = __x; | |
100 | return *this; | |
101 | } | |
526da49c BI |
102 | |
103 | bool | |
5d861bf2 | 104 | operator~() const _GLIBCXX_NOEXCEPT |
285b36d6 BK |
105 | { |
106 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), | |
3cbc7af0 | 107 | _M_message(__gnu_debug::__msg_bad_bitset_read) |
285b36d6 BK |
108 | ._M_iterator(*this)); |
109 | return ~(*static_cast<const _Base_ref*>(this)); | |
110 | } | |
526da49c | 111 | |
5d861bf2 | 112 | operator bool() const _GLIBCXX_NOEXCEPT |
285b36d6 BK |
113 | { |
114 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), | |
3cbc7af0 | 115 | _M_message(__gnu_debug::__msg_bad_bitset_read) |
285b36d6 BK |
116 | ._M_iterator(*this)); |
117 | return *static_cast<const _Base_ref*>(this); | |
118 | } | |
526da49c BI |
119 | |
120 | reference& | |
5d861bf2 | 121 | flip() _GLIBCXX_NOEXCEPT |
285b36d6 BK |
122 | { |
123 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), | |
3cbc7af0 | 124 | _M_message(__gnu_debug::__msg_bad_bitset_flip) |
285b36d6 BK |
125 | ._M_iterator(*this)); |
126 | _Base_ref::flip(); | |
127 | return *this; | |
128 | } | |
129 | }; | |
c5589aa7 | 130 | #endif |
285b36d6 BK |
131 | |
132 | // 23.3.5.1 constructors: | |
5d861bf2 PC |
133 | _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT |
134 | : _Base() { } | |
526da49c | 135 | |
734f5023 | 136 | #if __cplusplus >= 201103L |
5d861bf2 | 137 | constexpr bitset(unsigned long long __val) noexcept |
eb07a8f5 PC |
138 | #else |
139 | bitset(unsigned long __val) | |
140 | #endif | |
141 | : _Base(__val) { } | |
526da49c | 142 | |
47cd1557 | 143 | template<typename _CharT, typename _Traits, typename _Alloc> |
526da49c | 144 | explicit |
47cd1557 PC |
145 | bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str, |
146 | typename std::basic_string<_CharT, _Traits, _Alloc>::size_type | |
285b36d6 | 147 | __pos = 0, |
47cd1557 PC |
148 | typename std::basic_string<_CharT, _Traits, _Alloc>::size_type |
149 | __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos)) | |
285b36d6 BK |
150 | : _Base(__str, __pos, __n) { } |
151 | ||
47cd1557 PC |
152 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
153 | // 396. what are characters zero and one. | |
154 | template<class _CharT, class _Traits, class _Alloc> | |
155 | bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str, | |
156 | typename std::basic_string<_CharT, _Traits, _Alloc>::size_type | |
157 | __pos, | |
158 | typename std::basic_string<_CharT, _Traits, _Alloc>::size_type | |
159 | __n, | |
160 | _CharT __zero, _CharT __one = _CharT('1')) | |
161 | : _Base(__str, __pos, __n, __zero, __one) { } | |
162 | ||
17e3f4aa | 163 | bitset(const _Base& __x) : _Base(__x) { } |
285b36d6 | 164 | |
734f5023 | 165 | #if __cplusplus >= 201103L |
a0a2a399 PC |
166 | template<typename _CharT> |
167 | explicit | |
168 | bitset(const _CharT* __str, | |
169 | typename std::basic_string<_CharT>::size_type __n | |
170 | = std::basic_string<_CharT>::npos, | |
171 | _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) | |
172 | : _Base(__str, __n, __zero, __one) { } | |
2838468c PC |
173 | #endif |
174 | ||
285b36d6 | 175 | // 23.3.5.2 bitset operations: |
526da49c | 176 | bitset<_Nb>& |
5d861bf2 | 177 | operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
178 | { |
179 | _M_base() &= __rhs; | |
180 | return *this; | |
181 | } | |
526da49c BI |
182 | |
183 | bitset<_Nb>& | |
5d861bf2 | 184 | operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT |
285b36d6 | 185 | { |
72afd981 | 186 | _M_base() |= __rhs; |
285b36d6 BK |
187 | return *this; |
188 | } | |
526da49c BI |
189 | |
190 | bitset<_Nb>& | |
5d861bf2 | 191 | operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
192 | { |
193 | _M_base() ^= __rhs; | |
194 | return *this; | |
195 | } | |
526da49c BI |
196 | |
197 | bitset<_Nb>& | |
5d861bf2 | 198 | operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
199 | { |
200 | _M_base() <<= __pos; | |
201 | return *this; | |
202 | } | |
526da49c BI |
203 | |
204 | bitset<_Nb>& | |
5d861bf2 | 205 | operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
206 | { |
207 | _M_base() >>= __pos; | |
208 | return *this; | |
209 | } | |
526da49c BI |
210 | |
211 | bitset<_Nb>& | |
5d861bf2 | 212 | set() _GLIBCXX_NOEXCEPT |
285b36d6 BK |
213 | { |
214 | _Base::set(); | |
215 | return *this; | |
216 | } | |
526da49c | 217 | |
285b36d6 | 218 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
526da49c BI |
219 | // 186. bitset::set() second parameter should be bool |
220 | bitset<_Nb>& | |
285b36d6 BK |
221 | set(size_t __pos, bool __val = true) |
222 | { | |
223 | _Base::set(__pos, __val); | |
224 | return *this; | |
225 | } | |
526da49c BI |
226 | |
227 | bitset<_Nb>& | |
5d861bf2 | 228 | reset() _GLIBCXX_NOEXCEPT |
285b36d6 BK |
229 | { |
230 | _Base::reset(); | |
231 | return *this; | |
232 | } | |
526da49c BI |
233 | |
234 | bitset<_Nb>& | |
285b36d6 BK |
235 | reset(size_t __pos) |
236 | { | |
237 | _Base::reset(__pos); | |
238 | return *this; | |
239 | } | |
526da49c | 240 | |
5d861bf2 PC |
241 | bitset<_Nb> |
242 | operator~() const _GLIBCXX_NOEXCEPT | |
243 | { return bitset(~_M_base()); } | |
526da49c BI |
244 | |
245 | bitset<_Nb>& | |
5d861bf2 | 246 | flip() _GLIBCXX_NOEXCEPT |
285b36d6 BK |
247 | { |
248 | _Base::flip(); | |
249 | return *this; | |
250 | } | |
526da49c BI |
251 | |
252 | bitset<_Nb>& | |
285b36d6 BK |
253 | flip(size_t __pos) |
254 | { | |
255 | _Base::flip(__pos); | |
256 | return *this; | |
257 | } | |
526da49c | 258 | |
285b36d6 BK |
259 | // element access: |
260 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
526da49c BI |
261 | // 11. Bitset minor problems |
262 | reference | |
285b36d6 | 263 | operator[](size_t __pos) |
526da49c | 264 | { |
285b36d6 | 265 | __glibcxx_check_subscript(__pos); |
734f5023 | 266 | #if __cplusplus >= 201103L |
c5589aa7 FD |
267 | return _M_base()[__pos]; |
268 | #else | |
526da49c | 269 | return reference(_M_base()[__pos], this); |
c5589aa7 | 270 | #endif |
285b36d6 | 271 | } |
526da49c | 272 | |
285b36d6 | 273 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
526da49c | 274 | // 11. Bitset minor problems |
fc09e5b6 | 275 | _GLIBCXX_CONSTEXPR bool |
526da49c BI |
276 | operator[](size_t __pos) const |
277 | { | |
734f5023 | 278 | #if __cplusplus < 201103L |
fc09e5b6 | 279 | // TODO: Check in debug-mode too. |
285b36d6 | 280 | __glibcxx_check_subscript(__pos); |
fc09e5b6 PC |
281 | #endif |
282 | return _Base::operator[](__pos); | |
285b36d6 | 283 | } |
526da49c | 284 | |
285b36d6 | 285 | using _Base::to_ulong; |
734f5023 | 286 | #if __cplusplus >= 201103L |
700d2899 PC |
287 | using _Base::to_ullong; |
288 | #endif | |
526da49c | 289 | |
47cd1557 PC |
290 | template <typename _CharT, typename _Traits, typename _Alloc> |
291 | std::basic_string<_CharT, _Traits, _Alloc> | |
285b36d6 | 292 | to_string() const |
47cd1557 PC |
293 | { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); } |
294 | ||
295 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
296 | // 396. what are characters zero and one. | |
297 | template<class _CharT, class _Traits, class _Alloc> | |
298 | std::basic_string<_CharT, _Traits, _Alloc> | |
299 | to_string(_CharT __zero, _CharT __one = _CharT('1')) const | |
300 | { | |
301 | return _M_base().template | |
302 | to_string<_CharT, _Traits, _Alloc>(__zero, __one); | |
303 | } | |
526da49c | 304 | |
48cf14eb JW |
305 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
306 | // 434. bitset::to_string() hard to use. | |
307 | template<typename _CharT, typename _Traits> | |
308 | std::basic_string<_CharT, _Traits, std::allocator<_CharT> > | |
309 | to_string() const | |
310 | { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } | |
311 | ||
47cd1557 | 312 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
19a6a2ea | 313 | // 853. to_string needs updating with zero and one. |
47cd1557 PC |
314 | template<class _CharT, class _Traits> |
315 | std::basic_string<_CharT, _Traits, std::allocator<_CharT> > | |
316 | to_string(_CharT __zero, _CharT __one = _CharT('1')) const | |
317 | { return to_string<_CharT, _Traits, | |
318 | std::allocator<_CharT> >(__zero, __one); } | |
319 | ||
48cf14eb JW |
320 | template<typename _CharT> |
321 | std::basic_string<_CharT, std::char_traits<_CharT>, | |
322 | std::allocator<_CharT> > | |
323 | to_string() const | |
324 | { | |
325 | return to_string<_CharT, std::char_traits<_CharT>, | |
326 | std::allocator<_CharT> >(); | |
327 | } | |
328 | ||
47cd1557 PC |
329 | template<class _CharT> |
330 | std::basic_string<_CharT, std::char_traits<_CharT>, | |
331 | std::allocator<_CharT> > | |
332 | to_string(_CharT __zero, _CharT __one = _CharT('1')) const | |
333 | { | |
334 | return to_string<_CharT, std::char_traits<_CharT>, | |
335 | std::allocator<_CharT> >(__zero, __one); | |
336 | } | |
337 | ||
48cf14eb | 338 | std::basic_string<char, std::char_traits<char>, std::allocator<char> > |
47cd1557 PC |
339 | to_string() const |
340 | { | |
341 | return to_string<char,std::char_traits<char>,std::allocator<char> >(); | |
342 | } | |
343 | ||
344 | std::basic_string<char, std::char_traits<char>, std::allocator<char> > | |
345 | to_string(char __zero, char __one = '1') const | |
346 | { | |
347 | return to_string<char, std::char_traits<char>, | |
348 | std::allocator<char> >(__zero, __one); | |
349 | } | |
48cf14eb | 350 | |
285b36d6 BK |
351 | using _Base::count; |
352 | using _Base::size; | |
526da49c BI |
353 | |
354 | bool | |
5d861bf2 | 355 | operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT |
285b36d6 BK |
356 | { return _M_base() == __rhs; } |
357 | ||
526da49c | 358 | bool |
5d861bf2 | 359 | operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT |
285b36d6 | 360 | { return _M_base() != __rhs; } |
526da49c | 361 | |
285b36d6 | 362 | using _Base::test; |
b96817da | 363 | using _Base::all; |
285b36d6 BK |
364 | using _Base::any; |
365 | using _Base::none; | |
526da49c BI |
366 | |
367 | bitset<_Nb> | |
5d861bf2 | 368 | operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT |
285b36d6 | 369 | { return bitset<_Nb>(_M_base() << __pos); } |
526da49c BI |
370 | |
371 | bitset<_Nb> | |
5d861bf2 | 372 | operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT |
285b36d6 | 373 | { return bitset<_Nb>(_M_base() >> __pos); } |
526da49c | 374 | |
5d861bf2 PC |
375 | _Base& |
376 | _M_base() _GLIBCXX_NOEXCEPT | |
377 | { return *this; } | |
285b36d6 | 378 | |
526da49c | 379 | const _Base& |
5d861bf2 PC |
380 | _M_base() const _GLIBCXX_NOEXCEPT |
381 | { return *this; } | |
285b36d6 | 382 | }; |
526da49c | 383 | |
285b36d6 | 384 | template<size_t _Nb> |
526da49c | 385 | bitset<_Nb> |
5d861bf2 | 386 | operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT |
285b36d6 | 387 | { return bitset<_Nb>(__x) &= __y; } |
526da49c | 388 | |
285b36d6 | 389 | template<size_t _Nb> |
526da49c | 390 | bitset<_Nb> |
5d861bf2 | 391 | operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
392 | { return bitset<_Nb>(__x) |= __y; } |
393 | ||
394 | template<size_t _Nb> | |
526da49c | 395 | bitset<_Nb> |
5d861bf2 | 396 | operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT |
285b36d6 BK |
397 | { return bitset<_Nb>(__x) ^= __y; } |
398 | ||
399 | template<typename _CharT, typename _Traits, size_t _Nb> | |
400 | std::basic_istream<_CharT, _Traits>& | |
401 | operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) | |
402 | { return __is >> __x._M_base(); } | |
403 | ||
404 | template<typename _CharT, typename _Traits, size_t _Nb> | |
405 | std::basic_ostream<_CharT, _Traits>& | |
526da49c | 406 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
285b36d6 BK |
407 | const bitset<_Nb>& __x) |
408 | { return __os << __x._M_base(); } | |
1ceb9e06 | 409 | |
45f388bb | 410 | } // namespace __debug |
ec7058d6 | 411 | |
734f5023 | 412 | #if __cplusplus >= 201103L |
ec7058d6 PC |
413 | // DR 1182. |
414 | /// std::hash specialization for bitset. | |
415 | template<size_t _Nb> | |
95addb1b | 416 | struct hash<__debug::bitset<_Nb>> |
5d64ee19 | 417 | : public __hash_base<size_t, __debug::bitset<_Nb>> |
ec7058d6 PC |
418 | { |
419 | size_t | |
72f1c34b | 420 | operator()(const __debug::bitset<_Nb>& __b) const noexcept |
12ffa228 | 421 | { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); } |
ec7058d6 PC |
422 | }; |
423 | #endif | |
424 | ||
3cbc7af0 | 425 | } // namespace std |
285b36d6 BK |
426 | |
427 | #endif |