1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003-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 debug/bitset
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_BITSET
30 #define _GLIBCXX_DEBUG_BITSET
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
40 /// Class std::bitset with additional safety/checking/debug instrumentation.
43 : public _GLIBCXX_STD_C::bitset<_Nb>
44 #if __cplusplus < 201103L
45 , public __gnu_debug::_Safe_sequence_base
48 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
51 // In C++0x we rely on normal reference type to preserve the property
52 // of bitset to be use as a literal.
53 // TODO: Find another solution.
54 #if __cplusplus >= 201103L
55 typedef typename _Base::reference reference;
59 : private _Base::reference
60 , public __gnu_debug::_Safe_iterator_base
62 typedef typename _Base::reference _Base_ref;
67 reference(const _Base_ref& __base,
68 bitset* __seq __attribute__((__unused__))) _GLIBCXX_NOEXCEPT
70 , _Safe_iterator_base(__seq, false)
74 reference(const reference& __x) _GLIBCXX_NOEXCEPT
76 , _Safe_iterator_base(__x, false)
80 operator=(bool __x) _GLIBCXX_NOEXCEPT
82 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
83 _M_message(__gnu_debug::__msg_bad_bitset_write)
85 *static_cast<_Base_ref*>(this) = __x;
90 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
92 _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
93 _M_message(__gnu_debug::__msg_bad_bitset_read)
95 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
96 _M_message(__gnu_debug::__msg_bad_bitset_write)
98 *static_cast<_Base_ref*>(this) = __x;
103 operator~() const _GLIBCXX_NOEXCEPT
105 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
106 _M_message(__gnu_debug::__msg_bad_bitset_read)
107 ._M_iterator(*this));
108 return ~(*static_cast<const _Base_ref*>(this));
111 operator bool() const _GLIBCXX_NOEXCEPT
113 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
114 _M_message(__gnu_debug::__msg_bad_bitset_read)
115 ._M_iterator(*this));
116 return *static_cast<const _Base_ref*>(this);
120 flip() _GLIBCXX_NOEXCEPT
122 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
123 _M_message(__gnu_debug::__msg_bad_bitset_flip)
124 ._M_iterator(*this));
131 // 23.3.5.1 constructors:
132 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
135 #if __cplusplus >= 201103L
136 constexpr bitset(unsigned long long __val) noexcept
138 bitset(unsigned long __val)
142 template<typename _CharT, typename _Traits, typename _Alloc>
144 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
145 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
147 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
148 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
149 : _Base(__str, __pos, __n) { }
151 // _GLIBCXX_RESOLVE_LIB_DEFECTS
152 // 396. what are characters zero and one.
153 template<class _CharT, class _Traits, class _Alloc>
154 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
155 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
157 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
159 _CharT __zero, _CharT __one = _CharT('1'))
160 : _Base(__str, __pos, __n, __zero, __one) { }
162 bitset(const _Base& __x) : _Base(__x) { }
164 #if __cplusplus >= 201103L
165 template<typename _CharT>
167 bitset(const _CharT* __str,
168 typename std::basic_string<_CharT>::size_type __n
169 = std::basic_string<_CharT>::npos,
170 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
171 : _Base(__str, __n, __zero, __one) { }
174 // 23.3.5.2 bitset operations:
176 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
183 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
190 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
197 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
204 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
211 set() _GLIBCXX_NOEXCEPT
217 // _GLIBCXX_RESOLVE_LIB_DEFECTS
218 // 186. bitset::set() second parameter should be bool
220 set(size_t __pos, bool __val = true)
222 _Base::set(__pos, __val);
227 reset() _GLIBCXX_NOEXCEPT
241 operator~() const _GLIBCXX_NOEXCEPT
242 { return bitset(~_M_base()); }
245 flip() _GLIBCXX_NOEXCEPT
259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
260 // 11. Bitset minor problems
262 operator[](size_t __pos)
264 __glibcxx_check_subscript(__pos);
265 #if __cplusplus >= 201103L
266 return _M_base()[__pos];
268 return reference(_M_base()[__pos], this);
272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
273 // 11. Bitset minor problems
274 _GLIBCXX_CONSTEXPR bool
275 operator[](size_t __pos) const
277 #if __cplusplus < 201103L
278 // TODO: Check in debug-mode too.
279 __glibcxx_check_subscript(__pos);
281 return _Base::operator[](__pos);
284 using _Base::to_ulong;
285 #if __cplusplus >= 201103L
286 using _Base::to_ullong;
289 template <typename _CharT, typename _Traits, typename _Alloc>
290 std::basic_string<_CharT, _Traits, _Alloc>
292 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
294 // _GLIBCXX_RESOLVE_LIB_DEFECTS
295 // 396. what are characters zero and one.
296 template<class _CharT, class _Traits, class _Alloc>
297 std::basic_string<_CharT, _Traits, _Alloc>
298 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
300 return _M_base().template
301 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
305 // 434. bitset::to_string() hard to use.
306 template<typename _CharT, typename _Traits>
307 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
309 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
311 // _GLIBCXX_RESOLVE_LIB_DEFECTS
312 // 853. to_string needs updating with zero and one.
313 template<class _CharT, class _Traits>
314 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
315 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
316 { return to_string<_CharT, _Traits,
317 std::allocator<_CharT> >(__zero, __one); }
319 template<typename _CharT>
320 std::basic_string<_CharT, std::char_traits<_CharT>,
321 std::allocator<_CharT> >
324 return to_string<_CharT, std::char_traits<_CharT>,
325 std::allocator<_CharT> >();
328 template<class _CharT>
329 std::basic_string<_CharT, std::char_traits<_CharT>,
330 std::allocator<_CharT> >
331 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
333 return to_string<_CharT, std::char_traits<_CharT>,
334 std::allocator<_CharT> >(__zero, __one);
337 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
340 return to_string<char,std::char_traits<char>,std::allocator<char> >();
343 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
344 to_string(char __zero, char __one = '1') const
346 return to_string<char, std::char_traits<char>,
347 std::allocator<char> >(__zero, __one);
354 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
355 { return _M_base() == __rhs; }
358 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
359 { return _M_base() != __rhs; }
367 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
368 { return bitset<_Nb>(_M_base() << __pos); }
371 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
372 { return bitset<_Nb>(_M_base() >> __pos); }
375 _M_base() _GLIBCXX_NOEXCEPT
379 _M_base() const _GLIBCXX_NOEXCEPT
385 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
386 { return bitset<_Nb>(__x) &= __y; }
390 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
391 { return bitset<_Nb>(__x) |= __y; }
395 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
396 { return bitset<_Nb>(__x) ^= __y; }
398 template<typename _CharT, typename _Traits, size_t _Nb>
399 std::basic_istream<_CharT, _Traits>&
400 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
401 { return __is >> __x._M_base(); }
403 template<typename _CharT, typename _Traits, size_t _Nb>
404 std::basic_ostream<_CharT, _Traits>&
405 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
406 const bitset<_Nb>& __x)
407 { return __os << __x._M_base(); }
409 } // namespace __debug
411 #if __cplusplus >= 201103L
413 /// std::hash specialization for bitset.
415 struct hash<__debug::bitset<_Nb>>
416 : public __hash_base<size_t, __debug::bitset<_Nb>>
419 operator()(const __debug::bitset<_Nb>& __b) const noexcept
420 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }