1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file debug/bitset
31 * This file is a GNU debug extension to the Standard C++ Library.
34 #ifndef _GLIBCXX_DEBUG_BITSET
35 #define _GLIBCXX_DEBUG_BITSET
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
47 : public _GLIBCXX_STD_D::bitset<_Nb>,
48 public __gnu_debug::_Safe_sequence_base
50 typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
51 typedef __gnu_debug::_Safe_sequence_base _Safe_base;
56 : private _Base::reference, public __gnu_debug::_Safe_iterator_base
58 typedef typename _Base::reference _Base_ref;
63 reference(const _Base_ref& __base, bitset* __seq)
64 : _Base_ref(__base), _Safe_iterator_base(__seq, false)
68 reference(const reference& __x)
69 : _Base_ref(__x), _Safe_iterator_base(__x, false)
75 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
76 _M_message(__gnu_debug::__msg_bad_bitset_write)
78 *static_cast<_Base_ref*>(this) = __x;
83 operator=(const reference& __x)
85 _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
86 _M_message(__gnu_debug::__msg_bad_bitset_read)
88 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
89 _M_message(__gnu_debug::__msg_bad_bitset_write)
91 *static_cast<_Base_ref*>(this) = __x;
98 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
99 _M_message(__gnu_debug::__msg_bad_bitset_read)
100 ._M_iterator(*this));
101 return ~(*static_cast<const _Base_ref*>(this));
104 operator bool() const
106 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
107 _M_message(__gnu_debug::__msg_bad_bitset_read)
108 ._M_iterator(*this));
109 return *static_cast<const _Base_ref*>(this);
115 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
116 _M_message(__gnu_debug::__msg_bad_bitset_flip)
117 ._M_iterator(*this));
123 // 23.3.5.1 constructors:
124 bitset() : _Base() { }
126 bitset(unsigned long __val) : _Base(__val) { }
128 template<typename _CharT, typename _Traits, typename _Allocator>
130 bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
131 typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
133 typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
134 __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
135 : _Base(__str, __pos, __n) { }
137 bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
139 // 23.3.5.2 bitset operations:
141 operator&=(const bitset<_Nb>& __rhs)
148 operator|=(const bitset<_Nb>& __rhs)
155 operator^=(const bitset<_Nb>& __rhs)
162 operator<<=(size_t __pos)
169 operator>>=(size_t __pos)
182 // _GLIBCXX_RESOLVE_LIB_DEFECTS
183 // 186. bitset::set() second parameter should be bool
185 set(size_t __pos, bool __val = true)
187 _Base::set(__pos, __val);
205 bitset<_Nb> operator~() const { return bitset(~_M_base()); }
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 11. Bitset minor problems
225 operator[](size_t __pos)
227 __glibcxx_check_subscript(__pos);
228 return reference(_M_base()[__pos], this);
231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
232 // 11. Bitset minor problems
234 operator[](size_t __pos) const
236 __glibcxx_check_subscript(__pos);
237 return _M_base()[__pos];
240 using _Base::to_ulong;
242 template <typename _CharT, typename _Traits, typename _Allocator>
243 std::basic_string<_CharT, _Traits, _Allocator>
245 { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
247 // _GLIBCXX_RESOLVE_LIB_DEFECTS
248 // 434. bitset::to_string() hard to use.
249 template<typename _CharT, typename _Traits>
250 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
252 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
254 template<typename _CharT>
255 std::basic_string<_CharT, std::char_traits<_CharT>,
256 std::allocator<_CharT> >
259 return to_string<_CharT, std::char_traits<_CharT>,
260 std::allocator<_CharT> >();
263 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
266 return to_string<char,std::char_traits<char>,std::allocator<char> >();
273 operator==(const bitset<_Nb>& __rhs) const
274 { return _M_base() == __rhs; }
277 operator!=(const bitset<_Nb>& __rhs) const
278 { return _M_base() != __rhs; }
286 operator<<(size_t __pos) const
287 { return bitset<_Nb>(_M_base() << __pos); }
290 operator>>(size_t __pos) const
291 { return bitset<_Nb>(_M_base() >> __pos); }
294 _M_base() { return *this; }
297 _M_base() const { return *this; }
302 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
303 { return bitset<_Nb>(__x) &= __y; }
307 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
308 { return bitset<_Nb>(__x) |= __y; }
312 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
313 { return bitset<_Nb>(__x) ^= __y; }
315 template<typename _CharT, typename _Traits, size_t _Nb>
316 std::basic_istream<_CharT, _Traits>&
317 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
318 { return __is >> __x._M_base(); }
320 template<typename _CharT, typename _Traits, size_t _Nb>
321 std::basic_ostream<_CharT, _Traits>&
322 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
323 const bitset<_Nb>& __x)
324 { return __os << __x._M_base(); }
325 } // namespace __debug