1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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
9 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
26 /** @file debug/bitset
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_BITSET
31 #define _GLIBCXX_DEBUG_BITSET
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
41 /// Class std::bitset with additional safety/checking/debug instrumentation.
44 : public _GLIBCXX_STD_D::bitset<_Nb>,
45 public __gnu_debug::_Safe_sequence_base
47 typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
48 typedef __gnu_debug::_Safe_sequence_base _Safe_base;
53 : private _Base::reference, public __gnu_debug::_Safe_iterator_base
55 typedef typename _Base::reference _Base_ref;
60 reference(const _Base_ref& __base, bitset* __seq)
61 : _Base_ref(__base), _Safe_iterator_base(__seq, false)
65 reference(const reference& __x)
66 : _Base_ref(__x), _Safe_iterator_base(__x, false)
72 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
73 _M_message(__gnu_debug::__msg_bad_bitset_write)
75 *static_cast<_Base_ref*>(this) = __x;
80 operator=(const reference& __x)
82 _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
83 _M_message(__gnu_debug::__msg_bad_bitset_read)
85 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
86 _M_message(__gnu_debug::__msg_bad_bitset_write)
88 *static_cast<_Base_ref*>(this) = __x;
95 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
96 _M_message(__gnu_debug::__msg_bad_bitset_read)
98 return ~(*static_cast<const _Base_ref*>(this));
101 operator bool() const
103 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
104 _M_message(__gnu_debug::__msg_bad_bitset_read)
105 ._M_iterator(*this));
106 return *static_cast<const _Base_ref*>(this);
112 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
113 _M_message(__gnu_debug::__msg_bad_bitset_flip)
114 ._M_iterator(*this));
120 // 23.3.5.1 constructors:
121 bitset() : _Base() { }
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 bitset(unsigned long long __val)
126 bitset(unsigned long __val)
130 template<typename _CharT, typename _Traits, typename _Alloc>
132 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
133 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
135 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
136 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
137 : _Base(__str, __pos, __n) { }
139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
140 // 396. what are characters zero and one.
141 template<class _CharT, class _Traits, class _Alloc>
142 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
143 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
145 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
147 _CharT __zero, _CharT __one = _CharT('1'))
148 : _Base(__str, __pos, __n, __zero, __one) { }
150 bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
152 #ifdef __GXX_EXPERIMENTAL_CXX0X__
154 bitset(const char* __str) : _Base(__str) { }
157 // 23.3.5.2 bitset operations:
159 operator&=(const bitset<_Nb>& __rhs)
166 operator|=(const bitset<_Nb>& __rhs)
173 operator^=(const bitset<_Nb>& __rhs)
180 operator<<=(size_t __pos)
187 operator>>=(size_t __pos)
200 // _GLIBCXX_RESOLVE_LIB_DEFECTS
201 // 186. bitset::set() second parameter should be bool
203 set(size_t __pos, bool __val = true)
205 _Base::set(__pos, __val);
223 bitset<_Nb> operator~() const { return bitset(~_M_base()); }
240 // _GLIBCXX_RESOLVE_LIB_DEFECTS
241 // 11. Bitset minor problems
243 operator[](size_t __pos)
245 __glibcxx_check_subscript(__pos);
246 return reference(_M_base()[__pos], this);
249 // _GLIBCXX_RESOLVE_LIB_DEFECTS
250 // 11. Bitset minor problems
252 operator[](size_t __pos) const
254 __glibcxx_check_subscript(__pos);
255 return _M_base()[__pos];
258 using _Base::to_ulong;
259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
260 using _Base::to_ullong;
263 template <typename _CharT, typename _Traits, typename _Alloc>
264 std::basic_string<_CharT, _Traits, _Alloc>
266 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // 396. what are characters zero and one.
270 template<class _CharT, class _Traits, class _Alloc>
271 std::basic_string<_CharT, _Traits, _Alloc>
272 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
274 return _M_base().template
275 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
278 // _GLIBCXX_RESOLVE_LIB_DEFECTS
279 // 434. bitset::to_string() hard to use.
280 template<typename _CharT, typename _Traits>
281 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
283 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
285 // _GLIBCXX_RESOLVE_LIB_DEFECTS
286 // 853. to_string needs updating with zero and one.
287 template<class _CharT, class _Traits>
288 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
289 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
290 { return to_string<_CharT, _Traits,
291 std::allocator<_CharT> >(__zero, __one); }
293 template<typename _CharT>
294 std::basic_string<_CharT, std::char_traits<_CharT>,
295 std::allocator<_CharT> >
298 return to_string<_CharT, std::char_traits<_CharT>,
299 std::allocator<_CharT> >();
302 template<class _CharT>
303 std::basic_string<_CharT, std::char_traits<_CharT>,
304 std::allocator<_CharT> >
305 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
307 return to_string<_CharT, std::char_traits<_CharT>,
308 std::allocator<_CharT> >(__zero, __one);
311 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
314 return to_string<char,std::char_traits<char>,std::allocator<char> >();
317 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
318 to_string(char __zero, char __one = '1') const
320 return to_string<char, std::char_traits<char>,
321 std::allocator<char> >(__zero, __one);
328 operator==(const bitset<_Nb>& __rhs) const
329 { return _M_base() == __rhs; }
332 operator!=(const bitset<_Nb>& __rhs) const
333 { return _M_base() != __rhs; }
341 operator<<(size_t __pos) const
342 { return bitset<_Nb>(_M_base() << __pos); }
345 operator>>(size_t __pos) const
346 { return bitset<_Nb>(_M_base() >> __pos); }
349 _M_base() { return *this; }
352 _M_base() const { return *this; }
357 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
358 { return bitset<_Nb>(__x) &= __y; }
362 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
363 { return bitset<_Nb>(__x) |= __y; }
367 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
368 { return bitset<_Nb>(__x) ^= __y; }
370 template<typename _CharT, typename _Traits, size_t _Nb>
371 std::basic_istream<_CharT, _Traits>&
372 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
373 { return __is >> __x._M_base(); }
375 template<typename _CharT, typename _Traits, size_t _Nb>
376 std::basic_ostream<_CharT, _Traits>&
377 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
378 const bitset<_Nb>& __x)
379 { return __os << __x._M_base(); }
381 } // namespace __debug
383 #ifdef __GXX_EXPERIMENTAL_CXX0X__
385 /// std::hash specialization for bitset.
387 struct hash<__debug::bitset<_Nb>>
388 : public std::unary_function<__debug::bitset<_Nb>, size_t>
391 operator()(const __debug::bitset<_Nb>& __b) const
392 { return std::hash<_GLIBCXX_STD_D::bitset<_Nb>>()(__b._M_base()); }