bitset revision 256281
197403Sobrien// Debugging bitset implementation -*- C++ -*-
297403Sobrien
397403Sobrien// Copyright (C) 2003, 2004, 2005
497403Sobrien// Free Software Foundation, Inc.
597403Sobrien//
697403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
797403Sobrien// software; you can redistribute it and/or modify it under the
897403Sobrien// terms of the GNU General Public License as published by the
997403Sobrien// Free Software Foundation; either version 2, or (at your option)
1097403Sobrien// any later version.
1197403Sobrien
1297403Sobrien// This library is distributed in the hope that it will be useful,
1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1597403Sobrien// GNU General Public License for more details.
1697403Sobrien
1797403Sobrien// You should have received a copy of the GNU General Public License along
1897403Sobrien// with this library; see the file COPYING.  If not, write to the Free
1997403Sobrien// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2097403Sobrien// USA.
2197403Sobrien
2297403Sobrien// As a special exception, you may use this file as part of a free software
2397403Sobrien// library without restriction.  Specifically, if other files instantiate
2497403Sobrien// templates or use macros or inline functions from this file, or you compile
2597403Sobrien// this file and link it with other files to produce an executable, this
2697403Sobrien// file does not by itself cause the resulting executable to be covered by
2797403Sobrien// the GNU General Public License.  This exception does not however
2897403Sobrien// invalidate any other reasons why the executable file might be covered by
2997403Sobrien// the GNU General Public License.
3097403Sobrien
3197403Sobrien/** @file debug/bitset
3297403Sobrien *  This file is a GNU debug extension to the Standard C++ Library.
3397403Sobrien */
3497403Sobrien
3597403Sobrien#ifndef _GLIBCXX_DEBUG_BITSET
3697403Sobrien#define _GLIBCXX_DEBUG_BITSET
3797403Sobrien
3897403Sobrien#include <bitset>
3997403Sobrien#include <debug/safe_sequence.h>
4097403Sobrien#include <debug/safe_iterator.h>
4197403Sobrien
4297403Sobriennamespace std
43102782Skan{
4497403Sobriennamespace __debug
4597403Sobrien{
4697403Sobrien  template<size_t _Nb>
4797403Sobrien    class bitset
4897403Sobrien    : public _GLIBCXX_STD::bitset<_Nb>, 
4997403Sobrien      public __gnu_debug::_Safe_sequence_base
5097403Sobrien    {
5197403Sobrien      typedef _GLIBCXX_STD::bitset<_Nb> _Base;
5297403Sobrien      typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
5397403Sobrien
5497403Sobrien    public:
5597403Sobrien      // bit reference:
5697403Sobrien      class reference
5797403Sobrien      : private _Base::reference, public __gnu_debug::_Safe_iterator_base
5897403Sobrien      {
5997403Sobrien	typedef typename _Base::reference _Base_ref;
6097403Sobrien
6197403Sobrien	friend class bitset;
6297403Sobrien	reference();
6397403Sobrien
6497403Sobrien	reference(const _Base_ref& __base, bitset* __seq)
6597403Sobrien	: _Base_ref(__base), _Safe_iterator_base(__seq, false)
6697403Sobrien	{ }
6797403Sobrien
6897403Sobrien      public:
6997403Sobrien	reference(const reference& __x)
7097403Sobrien	: _Base_ref(__x), _Safe_iterator_base(__x, false)
7197403Sobrien	{ }
7297403Sobrien
7397403Sobrien	reference&
7497403Sobrien	operator=(bool __x)
7597403Sobrien	{
7697403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
7797403Sobrien			      _M_message(__gnu_debug::__msg_bad_bitset_write)
7897403Sobrien				._M_iterator(*this));
7997403Sobrien	  *static_cast<_Base_ref*>(this) = __x;
8097403Sobrien	  return *this;
8197403Sobrien	}
8297403Sobrien
8397403Sobrien	reference&
8497403Sobrien	operator=(const reference& __x)
8597403Sobrien	{
8697403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
8797403Sobrien			       _M_message(__gnu_debug::__msg_bad_bitset_read)
8897403Sobrien				._M_iterator(__x));
8997403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
9097403Sobrien			      _M_message(__gnu_debug::__msg_bad_bitset_write)
9197403Sobrien				._M_iterator(*this));
9297403Sobrien	  *static_cast<_Base_ref*>(this) = __x;
9397403Sobrien	  return *this;
9497403Sobrien	}
9597403Sobrien
9697403Sobrien	bool
9797403Sobrien	operator~() const
9897403Sobrien	{
9997403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
10097403Sobrien			       _M_message(__gnu_debug::__msg_bad_bitset_read)
10197403Sobrien				._M_iterator(*this));
10297403Sobrien	  return ~(*static_cast<const _Base_ref*>(this));
10397403Sobrien	}
10497403Sobrien
10597403Sobrien	operator bool() const
10697403Sobrien	{
10797403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
10897403Sobrien			      _M_message(__gnu_debug::__msg_bad_bitset_read)
10997403Sobrien				._M_iterator(*this));
11097403Sobrien	  return *static_cast<const _Base_ref*>(this);
11197403Sobrien	}
11297403Sobrien
11397403Sobrien	reference&
11497403Sobrien	flip()
11597403Sobrien	{
11697403Sobrien	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
11797403Sobrien			      _M_message(__gnu_debug::__msg_bad_bitset_flip)
11897403Sobrien				._M_iterator(*this));
11997403Sobrien	  _Base_ref::flip();
12097403Sobrien	  return *this;
12197403Sobrien	}
12297403Sobrien      };
12397403Sobrien
12497403Sobrien      // 23.3.5.1 constructors:
12597403Sobrien      bitset() : _Base() { }
12697403Sobrien
12797403Sobrien      bitset(unsigned long __val) : _Base(__val) { }
12897403Sobrien
12997403Sobrien      template<typename _CharT, typename _Traits, typename _Allocator>
13097403Sobrien        explicit
13197403Sobrien        bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
13297403Sobrien	       typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
13397403Sobrien	       __pos = 0,
13497403Sobrien	       typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
13597403Sobrien	       __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
13697403Sobrien	: _Base(__str, __pos, __n) { }
13797403Sobrien
13897403Sobrien      bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
13997403Sobrien
14097403Sobrien      // 23.3.5.2 bitset operations:
141102782Skan      bitset<_Nb>&
14297403Sobrien      operator&=(const bitset<_Nb>& __rhs)
14397403Sobrien      {
14497403Sobrien	_M_base() &= __rhs;
14597403Sobrien	return *this;
14697403Sobrien      }
14797403Sobrien
14897403Sobrien      bitset<_Nb>&
14997403Sobrien      operator|=(const bitset<_Nb>& __rhs)
15097403Sobrien      {
15197403Sobrien	_M_base() |= __rhs;
15297403Sobrien	return *this;
15397403Sobrien      }
15497403Sobrien
15597403Sobrien      bitset<_Nb>&
15697403Sobrien      operator^=(const bitset<_Nb>& __rhs)
15797403Sobrien      {
15897403Sobrien	_M_base() ^= __rhs;
15997403Sobrien	return *this;
16097403Sobrien      }
16197403Sobrien
16297403Sobrien      bitset<_Nb>&
16397403Sobrien      operator<<=(size_t __pos)
16497403Sobrien      {
16597403Sobrien	_M_base() <<= __pos;
16697403Sobrien	return *this;
16797403Sobrien      }
16897403Sobrien
16997403Sobrien      bitset<_Nb>&
17097403Sobrien      operator>>=(size_t __pos)
17197403Sobrien      {
17297403Sobrien	_M_base() >>= __pos;
17397403Sobrien	return *this;
17497403Sobrien      }
17597403Sobrien
17697403Sobrien      bitset<_Nb>&
17797403Sobrien      set()
17897403Sobrien      {
17997403Sobrien	_Base::set();
180102782Skan	return *this;
18197403Sobrien      }
18297403Sobrien
18397403Sobrien      // _GLIBCXX_RESOLVE_LIB_DEFECTS
18497403Sobrien      // 186. bitset::set() second parameter should be bool
18597403Sobrien      bitset<_Nb>&
18697403Sobrien      set(size_t __pos, bool __val = true)
18797403Sobrien      {
18897403Sobrien	_Base::set(__pos, __val);
18997403Sobrien	return *this;
19097403Sobrien      }
19197403Sobrien
19297403Sobrien      bitset<_Nb>&
19397403Sobrien      reset()
19497403Sobrien      {
19597403Sobrien	_Base::reset();
19697403Sobrien	return *this;
19797403Sobrien      }
19897403Sobrien
19997403Sobrien      bitset<_Nb>&
20097403Sobrien      reset(size_t __pos)
20197403Sobrien      {
20297403Sobrien	_Base::reset(__pos);
20397403Sobrien	return *this;
20497403Sobrien      }
20597403Sobrien
20697403Sobrien      bitset<_Nb> operator~() const { return bitset(~_M_base()); }
20797403Sobrien
20897403Sobrien      bitset<_Nb>&
20997403Sobrien      flip()
21097403Sobrien      {
21197403Sobrien	_Base::flip();
21297403Sobrien	return *this;
21397403Sobrien      }
21497403Sobrien
21597403Sobrien      bitset<_Nb>&
21697403Sobrien      flip(size_t __pos)
217102782Skan      {
21897403Sobrien	_Base::flip(__pos);
21997403Sobrien	return *this;
22097403Sobrien      }
22197403Sobrien
22297403Sobrien      // element access:
22397403Sobrien      // _GLIBCXX_RESOLVE_LIB_DEFECTS
22497403Sobrien      // 11. Bitset minor problems
22597403Sobrien      reference
22697403Sobrien      operator[](size_t __pos)
22797403Sobrien      {
22897403Sobrien	__glibcxx_check_subscript(__pos);
22997403Sobrien	return reference(_M_base()[__pos], this);
23097403Sobrien      }
23197403Sobrien
23297403Sobrien      // _GLIBCXX_RESOLVE_LIB_DEFECTS
23397403Sobrien      // 11. Bitset minor problems
23497403Sobrien      bool
23597403Sobrien      operator[](size_t __pos) const
23697403Sobrien      {
23797403Sobrien	__glibcxx_check_subscript(__pos);
23897403Sobrien	return _M_base()[__pos];
23997403Sobrien      }
24097403Sobrien
24197403Sobrien      using _Base::to_ulong;
24297403Sobrien
24397403Sobrien      template <typename _CharT, typename _Traits, typename _Allocator>
24497403Sobrien        std::basic_string<_CharT, _Traits, _Allocator>
24597403Sobrien        to_string() const
24697403Sobrien        { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
24797403Sobrien
24897403Sobrien      // _GLIBCXX_RESOLVE_LIB_DEFECTS
24997403Sobrien      // 434. bitset::to_string() hard to use.
25097403Sobrien      template<typename _CharT, typename _Traits>
251        std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
252        to_string() const
253        { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
254
255      template<typename _CharT>
256        std::basic_string<_CharT, std::char_traits<_CharT>,
257                          std::allocator<_CharT> >
258        to_string() const
259        {
260          return to_string<_CharT, std::char_traits<_CharT>,
261                           std::allocator<_CharT> >();
262        }
263
264      std::basic_string<char, std::char_traits<char>, std::allocator<char> >
265        to_string() const
266        {
267          return to_string<char,std::char_traits<char>,std::allocator<char> >();
268        }
269
270      using _Base::count;
271      using _Base::size;
272
273      bool
274      operator==(const bitset<_Nb>& __rhs) const
275      { return _M_base() == __rhs; }
276
277      bool
278      operator!=(const bitset<_Nb>& __rhs) const
279      { return _M_base() != __rhs; }
280
281      using _Base::test;
282      using _Base::any;
283      using _Base::none;
284
285      bitset<_Nb>
286      operator<<(size_t __pos) const
287      { return bitset<_Nb>(_M_base() << __pos); }
288
289      bitset<_Nb>
290      operator>>(size_t __pos) const
291      { return bitset<_Nb>(_M_base() >> __pos); }
292
293      _Base&
294      _M_base() { return *this; }
295
296      const _Base&
297      _M_base() const { return *this; }
298    };
299
300  template<size_t _Nb>
301    bitset<_Nb>
302    operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
303    { return bitset<_Nb>(__x) &= __y; }
304
305  template<size_t _Nb>
306    bitset<_Nb>
307    operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
308    { return bitset<_Nb>(__x) |= __y; }
309
310  template<size_t _Nb>
311    bitset<_Nb>
312    operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
313    { return bitset<_Nb>(__x) ^= __y; }
314
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(); }
319
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
326} // namespace std
327
328#endif
329