xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/bitset (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert// Debugging bitset implementation -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert// Copyright (C) 2003, 2004, 2005
4*404b540aSrobert// Free Software Foundation, Inc.
5*404b540aSrobert//
6*404b540aSrobert// This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert// software; you can redistribute it and/or modify it under the
8*404b540aSrobert// terms of the GNU General Public License as published by the
9*404b540aSrobert// Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert// any later version.
11*404b540aSrobert
12*404b540aSrobert// This library is distributed in the hope that it will be useful,
13*404b540aSrobert// but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert// GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert// You should have received a copy of the GNU General Public License along
18*404b540aSrobert// with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert// USA.
21*404b540aSrobert
22*404b540aSrobert// As a special exception, you may use this file as part of a free software
23*404b540aSrobert// library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert// templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert// this file and link it with other files to produce an executable, this
26*404b540aSrobert// file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert// the GNU General Public License.  This exception does not however
28*404b540aSrobert// invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert// the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert/** @file debug/bitset
32*404b540aSrobert *  This file is a GNU debug extension to the Standard C++ Library.
33*404b540aSrobert */
34*404b540aSrobert
35*404b540aSrobert#ifndef _GLIBCXX_DEBUG_BITSET
36*404b540aSrobert#define _GLIBCXX_DEBUG_BITSET
37*404b540aSrobert
38*404b540aSrobert#include <bitset>
39*404b540aSrobert#include <debug/safe_sequence.h>
40*404b540aSrobert#include <debug/safe_iterator.h>
41*404b540aSrobert
42*404b540aSrobertnamespace std
43*404b540aSrobert{
44*404b540aSrobertnamespace __debug
45*404b540aSrobert{
46*404b540aSrobert  template<size_t _Nb>
47*404b540aSrobert    class bitset
48*404b540aSrobert    : public _GLIBCXX_STD::bitset<_Nb>,
49*404b540aSrobert      public __gnu_debug::_Safe_sequence_base
50*404b540aSrobert    {
51*404b540aSrobert      typedef _GLIBCXX_STD::bitset<_Nb> _Base;
52*404b540aSrobert      typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
53*404b540aSrobert
54*404b540aSrobert    public:
55*404b540aSrobert      // bit reference:
56*404b540aSrobert      class reference
57*404b540aSrobert      : private _Base::reference, public __gnu_debug::_Safe_iterator_base
58*404b540aSrobert      {
59*404b540aSrobert	typedef typename _Base::reference _Base_ref;
60*404b540aSrobert
61*404b540aSrobert	friend class bitset;
62*404b540aSrobert	reference();
63*404b540aSrobert
64*404b540aSrobert	reference(const _Base_ref& __base, bitset* __seq)
65*404b540aSrobert	: _Base_ref(__base), _Safe_iterator_base(__seq, false)
66*404b540aSrobert	{ }
67*404b540aSrobert
68*404b540aSrobert      public:
69*404b540aSrobert	reference(const reference& __x)
70*404b540aSrobert	: _Base_ref(__x), _Safe_iterator_base(__x, false)
71*404b540aSrobert	{ }
72*404b540aSrobert
73*404b540aSrobert	reference&
74*404b540aSrobert	operator=(bool __x)
75*404b540aSrobert	{
76*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
77*404b540aSrobert			      _M_message(__gnu_debug::__msg_bad_bitset_write)
78*404b540aSrobert				._M_iterator(*this));
79*404b540aSrobert	  *static_cast<_Base_ref*>(this) = __x;
80*404b540aSrobert	  return *this;
81*404b540aSrobert	}
82*404b540aSrobert
83*404b540aSrobert	reference&
84*404b540aSrobert	operator=(const reference& __x)
85*404b540aSrobert	{
86*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
87*404b540aSrobert			       _M_message(__gnu_debug::__msg_bad_bitset_read)
88*404b540aSrobert				._M_iterator(__x));
89*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
90*404b540aSrobert			      _M_message(__gnu_debug::__msg_bad_bitset_write)
91*404b540aSrobert				._M_iterator(*this));
92*404b540aSrobert	  *static_cast<_Base_ref*>(this) = __x;
93*404b540aSrobert	  return *this;
94*404b540aSrobert	}
95*404b540aSrobert
96*404b540aSrobert	bool
97*404b540aSrobert	operator~() const
98*404b540aSrobert	{
99*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
100*404b540aSrobert			       _M_message(__gnu_debug::__msg_bad_bitset_read)
101*404b540aSrobert				._M_iterator(*this));
102*404b540aSrobert	  return ~(*static_cast<const _Base_ref*>(this));
103*404b540aSrobert	}
104*404b540aSrobert
105*404b540aSrobert	operator bool() const
106*404b540aSrobert	{
107*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
108*404b540aSrobert			      _M_message(__gnu_debug::__msg_bad_bitset_read)
109*404b540aSrobert				._M_iterator(*this));
110*404b540aSrobert	  return *static_cast<const _Base_ref*>(this);
111*404b540aSrobert	}
112*404b540aSrobert
113*404b540aSrobert	reference&
114*404b540aSrobert	flip()
115*404b540aSrobert	{
116*404b540aSrobert	  _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
117*404b540aSrobert			      _M_message(__gnu_debug::__msg_bad_bitset_flip)
118*404b540aSrobert				._M_iterator(*this));
119*404b540aSrobert	  _Base_ref::flip();
120*404b540aSrobert	  return *this;
121*404b540aSrobert	}
122*404b540aSrobert      };
123*404b540aSrobert
124*404b540aSrobert      // 23.3.5.1 constructors:
125*404b540aSrobert      bitset() : _Base() { }
126*404b540aSrobert
127*404b540aSrobert      bitset(unsigned long __val) : _Base(__val) { }
128*404b540aSrobert
129*404b540aSrobert      template<typename _CharT, typename _Traits, typename _Allocator>
130*404b540aSrobert        explicit
131*404b540aSrobert        bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
132*404b540aSrobert	       typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
133*404b540aSrobert	       __pos = 0,
134*404b540aSrobert	       typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
135*404b540aSrobert	       __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
136*404b540aSrobert	: _Base(__str, __pos, __n) { }
137*404b540aSrobert
138*404b540aSrobert      bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
139*404b540aSrobert
140*404b540aSrobert      // 23.3.5.2 bitset operations:
141*404b540aSrobert      bitset<_Nb>&
142*404b540aSrobert      operator&=(const bitset<_Nb>& __rhs)
143*404b540aSrobert      {
144*404b540aSrobert	_M_base() &= __rhs;
145*404b540aSrobert	return *this;
146*404b540aSrobert      }
147*404b540aSrobert
148*404b540aSrobert      bitset<_Nb>&
149*404b540aSrobert      operator|=(const bitset<_Nb>& __rhs)
150*404b540aSrobert      {
151*404b540aSrobert	_M_base() |= __rhs;
152*404b540aSrobert	return *this;
153*404b540aSrobert      }
154*404b540aSrobert
155*404b540aSrobert      bitset<_Nb>&
156*404b540aSrobert      operator^=(const bitset<_Nb>& __rhs)
157*404b540aSrobert      {
158*404b540aSrobert	_M_base() ^= __rhs;
159*404b540aSrobert	return *this;
160*404b540aSrobert      }
161*404b540aSrobert
162*404b540aSrobert      bitset<_Nb>&
163*404b540aSrobert      operator<<=(size_t __pos)
164*404b540aSrobert      {
165*404b540aSrobert	_M_base() <<= __pos;
166*404b540aSrobert	return *this;
167*404b540aSrobert      }
168*404b540aSrobert
169*404b540aSrobert      bitset<_Nb>&
170*404b540aSrobert      operator>>=(size_t __pos)
171*404b540aSrobert      {
172*404b540aSrobert	_M_base() >>= __pos;
173*404b540aSrobert	return *this;
174*404b540aSrobert      }
175*404b540aSrobert
176*404b540aSrobert      bitset<_Nb>&
177*404b540aSrobert      set()
178*404b540aSrobert      {
179*404b540aSrobert	_Base::set();
180*404b540aSrobert	return *this;
181*404b540aSrobert      }
182*404b540aSrobert
183*404b540aSrobert      // _GLIBCXX_RESOLVE_LIB_DEFECTS
184*404b540aSrobert      // 186. bitset::set() second parameter should be bool
185*404b540aSrobert      bitset<_Nb>&
186*404b540aSrobert      set(size_t __pos, bool __val = true)
187*404b540aSrobert      {
188*404b540aSrobert	_Base::set(__pos, __val);
189*404b540aSrobert	return *this;
190*404b540aSrobert      }
191*404b540aSrobert
192*404b540aSrobert      bitset<_Nb>&
193*404b540aSrobert      reset()
194*404b540aSrobert      {
195*404b540aSrobert	_Base::reset();
196*404b540aSrobert	return *this;
197*404b540aSrobert      }
198*404b540aSrobert
199*404b540aSrobert      bitset<_Nb>&
200*404b540aSrobert      reset(size_t __pos)
201*404b540aSrobert      {
202*404b540aSrobert	_Base::reset(__pos);
203*404b540aSrobert	return *this;
204*404b540aSrobert      }
205*404b540aSrobert
206*404b540aSrobert      bitset<_Nb> operator~() const { return bitset(~_M_base()); }
207*404b540aSrobert
208*404b540aSrobert      bitset<_Nb>&
209*404b540aSrobert      flip()
210*404b540aSrobert      {
211*404b540aSrobert	_Base::flip();
212*404b540aSrobert	return *this;
213*404b540aSrobert      }
214*404b540aSrobert
215*404b540aSrobert      bitset<_Nb>&
216*404b540aSrobert      flip(size_t __pos)
217*404b540aSrobert      {
218*404b540aSrobert	_Base::flip(__pos);
219*404b540aSrobert	return *this;
220*404b540aSrobert      }
221*404b540aSrobert
222*404b540aSrobert      // element access:
223*404b540aSrobert      // _GLIBCXX_RESOLVE_LIB_DEFECTS
224*404b540aSrobert      // 11. Bitset minor problems
225*404b540aSrobert      reference
226*404b540aSrobert      operator[](size_t __pos)
227*404b540aSrobert      {
228*404b540aSrobert	__glibcxx_check_subscript(__pos);
229*404b540aSrobert	return reference(_M_base()[__pos], this);
230*404b540aSrobert      }
231*404b540aSrobert
232*404b540aSrobert      // _GLIBCXX_RESOLVE_LIB_DEFECTS
233*404b540aSrobert      // 11. Bitset minor problems
234*404b540aSrobert      bool
235*404b540aSrobert      operator[](size_t __pos) const
236*404b540aSrobert      {
237*404b540aSrobert	__glibcxx_check_subscript(__pos);
238*404b540aSrobert	return _M_base()[__pos];
239*404b540aSrobert      }
240*404b540aSrobert
241*404b540aSrobert      using _Base::to_ulong;
242*404b540aSrobert
243*404b540aSrobert      template <typename _CharT, typename _Traits, typename _Allocator>
244*404b540aSrobert        std::basic_string<_CharT, _Traits, _Allocator>
245*404b540aSrobert        to_string() const
246*404b540aSrobert        { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
247*404b540aSrobert
248*404b540aSrobert      // _GLIBCXX_RESOLVE_LIB_DEFECTS
249*404b540aSrobert      // 434. bitset::to_string() hard to use.
250*404b540aSrobert      template<typename _CharT, typename _Traits>
251*404b540aSrobert        std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
252*404b540aSrobert        to_string() const
253*404b540aSrobert        { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
254*404b540aSrobert
255*404b540aSrobert      template<typename _CharT>
256*404b540aSrobert        std::basic_string<_CharT, std::char_traits<_CharT>,
257*404b540aSrobert                          std::allocator<_CharT> >
258*404b540aSrobert        to_string() const
259*404b540aSrobert        {
260*404b540aSrobert          return to_string<_CharT, std::char_traits<_CharT>,
261*404b540aSrobert                           std::allocator<_CharT> >();
262*404b540aSrobert        }
263*404b540aSrobert
264*404b540aSrobert      std::basic_string<char, std::char_traits<char>, std::allocator<char> >
265*404b540aSrobert        to_string() const
266*404b540aSrobert        {
267*404b540aSrobert          return to_string<char,std::char_traits<char>,std::allocator<char> >();
268*404b540aSrobert        }
269*404b540aSrobert
270*404b540aSrobert      using _Base::count;
271*404b540aSrobert      using _Base::size;
272*404b540aSrobert
273*404b540aSrobert      bool
274*404b540aSrobert      operator==(const bitset<_Nb>& __rhs) const
275*404b540aSrobert      { return _M_base() == __rhs; }
276*404b540aSrobert
277*404b540aSrobert      bool
278*404b540aSrobert      operator!=(const bitset<_Nb>& __rhs) const
279*404b540aSrobert      { return _M_base() != __rhs; }
280*404b540aSrobert
281*404b540aSrobert      using _Base::test;
282*404b540aSrobert      using _Base::any;
283*404b540aSrobert      using _Base::none;
284*404b540aSrobert
285*404b540aSrobert      bitset<_Nb>
286*404b540aSrobert      operator<<(size_t __pos) const
287*404b540aSrobert      { return bitset<_Nb>(_M_base() << __pos); }
288*404b540aSrobert
289*404b540aSrobert      bitset<_Nb>
290*404b540aSrobert      operator>>(size_t __pos) const
291*404b540aSrobert      { return bitset<_Nb>(_M_base() >> __pos); }
292*404b540aSrobert
293*404b540aSrobert      _Base&
294*404b540aSrobert      _M_base() { return *this; }
295*404b540aSrobert
296*404b540aSrobert      const _Base&
297*404b540aSrobert      _M_base() const { return *this; }
298*404b540aSrobert    };
299*404b540aSrobert
300*404b540aSrobert  template<size_t _Nb>
301*404b540aSrobert    bitset<_Nb>
302*404b540aSrobert    operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
303*404b540aSrobert    { return bitset<_Nb>(__x) &= __y; }
304*404b540aSrobert
305*404b540aSrobert  template<size_t _Nb>
306*404b540aSrobert    bitset<_Nb>
307*404b540aSrobert    operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
308*404b540aSrobert    { return bitset<_Nb>(__x) |= __y; }
309*404b540aSrobert
310*404b540aSrobert  template<size_t _Nb>
311*404b540aSrobert    bitset<_Nb>
312*404b540aSrobert    operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
313*404b540aSrobert    { return bitset<_Nb>(__x) ^= __y; }
314*404b540aSrobert
315*404b540aSrobert  template<typename _CharT, typename _Traits, size_t _Nb>
316*404b540aSrobert    std::basic_istream<_CharT, _Traits>&
317*404b540aSrobert    operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
318*404b540aSrobert    { return __is >> __x._M_base(); }
319*404b540aSrobert
320*404b540aSrobert  template<typename _CharT, typename _Traits, size_t _Nb>
321*404b540aSrobert    std::basic_ostream<_CharT, _Traits>&
322*404b540aSrobert    operator<<(std::basic_ostream<_CharT, _Traits>& __os,
323*404b540aSrobert	       const bitset<_Nb>& __x)
324*404b540aSrobert    { return __os << __x._M_base(); }
325*404b540aSrobert} // namespace __debug
326*404b540aSrobert} // namespace std
327*404b540aSrobert
328*404b540aSrobert#endif
329