1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file debug/bitset
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_BITSET
36 #define _GLIBCXX_DEBUG_BITSET
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
48 : public _GLIBCXX_STD::bitset<_Nb>,
49 public __gnu_debug::_Safe_sequence_base
51 typedef _GLIBCXX_STD::bitset<_Nb> _Base;
52 typedef __gnu_debug::_Safe_sequence_base _Safe_base;
57 : private _Base::reference, public __gnu_debug::_Safe_iterator_base
59 typedef typename _Base::reference _Base_ref;
64 reference(const _Base_ref& __base, bitset* __seq)
65 : _Base_ref(__base), _Safe_iterator_base(__seq, false)
69 reference(const reference& __x)
70 : _Base_ref(__x), _Safe_iterator_base(__x, false)
76 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
77 _M_message(__gnu_debug::__msg_bad_bitset_write)
79 *static_cast<_Base_ref*>(this) = __x;
84 operator=(const reference& __x)
86 _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
87 _M_message(__gnu_debug::__msg_bad_bitset_read)
89 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
90 _M_message(__gnu_debug::__msg_bad_bitset_write)
92 *static_cast<_Base_ref*>(this) = __x;
99 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
100 _M_message(__gnu_debug::__msg_bad_bitset_read)
101 ._M_iterator(*this));
102 return ~(*static_cast<const _Base_ref*>(this));
105 operator bool() const
107 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
108 _M_message(__gnu_debug::__msg_bad_bitset_read)
109 ._M_iterator(*this));
110 return *static_cast<const _Base_ref*>(this);
116 _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
117 _M_message(__gnu_debug::__msg_bad_bitset_flip)
118 ._M_iterator(*this));
124 // 23.3.5.1 constructors:
125 bitset() : _Base() { }
127 bitset(unsigned long __val) : _Base(__val) { }
129 template<typename _CharT, typename _Traits, typename _Allocator>
131 bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
132 typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
134 typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
135 __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
136 : _Base(__str, __pos, __n) { }
138 bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
140 // 23.3.5.2 bitset operations:
142 operator&=(const bitset<_Nb>& __rhs)
149 operator|=(const bitset<_Nb>& __rhs)
156 operator^=(const bitset<_Nb>& __rhs)
163 operator<<=(size_t __pos)
170 operator>>=(size_t __pos)
183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 // 186. bitset::set() second parameter should be bool
186 set(size_t __pos, bool __val = true)
188 _Base::set(__pos, __val);
206 bitset<_Nb> operator~() const { return bitset(~_M_base()); }
223 // _GLIBCXX_RESOLVE_LIB_DEFECTS
224 // 11. Bitset minor problems
226 operator[](size_t __pos)
228 __glibcxx_check_subscript(__pos);
229 return reference(_M_base()[__pos], this);
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // 11. Bitset minor problems
235 operator[](size_t __pos) const
237 __glibcxx_check_subscript(__pos);
238 return _M_base()[__pos];
241 using _Base::to_ulong;
243 template <typename _CharT, typename _Traits, typename _Allocator>
244 std::basic_string<_CharT, _Traits, _Allocator>
246 { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
248 // _GLIBCXX_RESOLVE_LIB_DEFECTS
249 // 434. bitset::to_string() hard to use.
250 template<typename _CharT, typename _Traits>
251 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
253 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
255 template<typename _CharT>
256 std::basic_string<_CharT, std::char_traits<_CharT>,
257 std::allocator<_CharT> >
260 return to_string<_CharT, std::char_traits<_CharT>,
261 std::allocator<_CharT> >();
264 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
267 return to_string<char,std::char_traits<char>,std::allocator<char> >();
274 operator==(const bitset<_Nb>& __rhs) const
275 { return _M_base() == __rhs; }
278 operator!=(const bitset<_Nb>& __rhs) const
279 { 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