1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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>
43 : public _GLIBCXX_STD_D::bitset<_Nb>,
44 public __gnu_debug::_Safe_sequence_base
46 typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
47 typedef __gnu_debug::_Safe_sequence_base _Safe_base;
52 : private _Base::reference, public __gnu_debug::_Safe_iterator_base
54 typedef typename _Base::reference _Base_ref;
59 reference(const _Base_ref& __base, bitset* __seq)
60 : _Base_ref(__base), _Safe_iterator_base(__seq, false)
64 reference(const reference& __x)
65 : _Base_ref(__x), _Safe_iterator_base(__x, false)
71 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
72 _M_message(__gnu_debug::__msg_bad_bitset_write)
74 *static_cast<_Base_ref*>(this) = __x;
79 operator=(const reference& __x)
81 _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
82 _M_message(__gnu_debug::__msg_bad_bitset_read)
84 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
85 _M_message(__gnu_debug::__msg_bad_bitset_write)
87 *static_cast<_Base_ref*>(this) = __x;
94 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
95 _M_message(__gnu_debug::__msg_bad_bitset_read)
97 return ~(*static_cast<const _Base_ref*>(this));
100 operator bool() const
102 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
103 _M_message(__gnu_debug::__msg_bad_bitset_read)
104 ._M_iterator(*this));
105 return *static_cast<const _Base_ref*>(this);
111 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
112 _M_message(__gnu_debug::__msg_bad_bitset_flip)
113 ._M_iterator(*this));
119 // 23.3.5.1 constructors:
120 bitset() : _Base() { }
122 bitset(unsigned long __val) : _Base(__val) { }
124 template<typename _CharT, typename _Traits, typename _Alloc>
126 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
127 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
129 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
130 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
131 : _Base(__str, __pos, __n) { }
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // 396. what are characters zero and one.
135 template<class _CharT, class _Traits, class _Alloc>
136 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
137 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
139 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
141 _CharT __zero, _CharT __one = _CharT('1'))
142 : _Base(__str, __pos, __n, __zero, __one) { }
144 bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
146 // 23.3.5.2 bitset operations:
148 operator&=(const bitset<_Nb>& __rhs)
155 operator|=(const bitset<_Nb>& __rhs)
162 operator^=(const bitset<_Nb>& __rhs)
169 operator<<=(size_t __pos)
176 operator>>=(size_t __pos)
189 // _GLIBCXX_RESOLVE_LIB_DEFECTS
190 // 186. bitset::set() second parameter should be bool
192 set(size_t __pos, bool __val = true)
194 _Base::set(__pos, __val);
212 bitset<_Nb> operator~() const { return bitset(~_M_base()); }
229 // _GLIBCXX_RESOLVE_LIB_DEFECTS
230 // 11. Bitset minor problems
232 operator[](size_t __pos)
234 __glibcxx_check_subscript(__pos);
235 return reference(_M_base()[__pos], this);
238 // _GLIBCXX_RESOLVE_LIB_DEFECTS
239 // 11. Bitset minor problems
241 operator[](size_t __pos) const
243 __glibcxx_check_subscript(__pos);
244 return _M_base()[__pos];
247 using _Base::to_ulong;
249 template <typename _CharT, typename _Traits, typename _Alloc>
250 std::basic_string<_CharT, _Traits, _Alloc>
252 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
254 // _GLIBCXX_RESOLVE_LIB_DEFECTS
255 // 396. what are characters zero and one.
256 template<class _CharT, class _Traits, class _Alloc>
257 std::basic_string<_CharT, _Traits, _Alloc>
258 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
260 return _M_base().template
261 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
264 // _GLIBCXX_RESOLVE_LIB_DEFECTS
265 // 434. bitset::to_string() hard to use.
266 template<typename _CharT, typename _Traits>
267 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
269 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
271 // _GLIBCXX_RESOLVE_LIB_DEFECTS
272 // 853. to_string needs updating with zero and one.
273 template<class _CharT, class _Traits>
274 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
275 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
276 { return to_string<_CharT, _Traits,
277 std::allocator<_CharT> >(__zero, __one); }
279 template<typename _CharT>
280 std::basic_string<_CharT, std::char_traits<_CharT>,
281 std::allocator<_CharT> >
284 return to_string<_CharT, std::char_traits<_CharT>,
285 std::allocator<_CharT> >();
288 template<class _CharT>
289 std::basic_string<_CharT, std::char_traits<_CharT>,
290 std::allocator<_CharT> >
291 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
293 return to_string<_CharT, std::char_traits<_CharT>,
294 std::allocator<_CharT> >(__zero, __one);
297 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
300 return to_string<char,std::char_traits<char>,std::allocator<char> >();
303 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
304 to_string(char __zero, char __one = '1') const
306 return to_string<char, std::char_traits<char>,
307 std::allocator<char> >(__zero, __one);
314 operator==(const bitset<_Nb>& __rhs) const
315 { return _M_base() == __rhs; }
318 operator!=(const bitset<_Nb>& __rhs) const
319 { return _M_base() != __rhs; }
327 operator<<(size_t __pos) const
328 { return bitset<_Nb>(_M_base() << __pos); }
331 operator>>(size_t __pos) const
332 { return bitset<_Nb>(_M_base() >> __pos); }
335 _M_base() { return *this; }
338 _M_base() const { return *this; }
343 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
344 { return bitset<_Nb>(__x) &= __y; }
348 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
349 { return bitset<_Nb>(__x) |= __y; }
353 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
354 { return bitset<_Nb>(__x) ^= __y; }
356 template<typename _CharT, typename _Traits, size_t _Nb>
357 std::basic_istream<_CharT, _Traits>&
358 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
359 { return __is >> __x._M_base(); }
361 template<typename _CharT, typename _Traits, size_t _Nb>
362 std::basic_ostream<_CharT, _Traits>&
363 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
364 const bitset<_Nb>& __x)
365 { return __os << __x._M_base(); }
366 } // namespace __debug