2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / debug / bitset
blob89244226dd7adfae9fd8ffc148b7459a5ba8a422
1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
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 #ifndef _GLIBCXX_DEBUG_BITSET
32 #define _GLIBCXX_DEBUG_BITSET
34 #include <bitset>
35 #include <debug/safe_sequence.h>
36 #include <debug/safe_iterator.h>
38 namespace __gnu_debug_def
40   template<size_t _Nb>
41     class bitset
42     : public __gnu_norm::bitset<_Nb>, public __gnu_debug::_Safe_sequence_base
43     {
44       typedef  __gnu_norm::bitset<_Nb>          _Base;
45       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
47     public:
48       // bit reference:
49       class reference
50       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
51       {
52         typedef typename _Base::reference _Base_ref;
54         friend class bitset;
55         reference();
57         reference(const _Base_ref& __base, bitset* __seq)
58         : _Base_ref(__base), _Safe_iterator_base(__seq, false)
59         { }
61       public:
62         reference(const reference& __x)
63         : _Base_ref(__x), _Safe_iterator_base(__x, false)
64         { }
66         reference&
67         operator=(bool __x)
68         {
69           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
70                               _M_message(::__gnu_debug::__msg_bad_bitset_write)
71                                 ._M_iterator(*this));
72           *static_cast<_Base_ref*>(this) = __x;
73           return *this;
74         }
76         reference&
77         operator=(const reference& __x)
78         {
79           _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
80                                _M_message(::__gnu_debug::__msg_bad_bitset_read)
81                                 ._M_iterator(__x));
82           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
83                               _M_message(::__gnu_debug::__msg_bad_bitset_write)
84                                 ._M_iterator(*this));
85           *static_cast<_Base_ref*>(this) = __x;
86           return *this;
87         }
89         bool
90         operator~() const
91         {
92           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
93                                _M_message(::__gnu_debug::__msg_bad_bitset_read)
94                                 ._M_iterator(*this));
95           return ~(*static_cast<const _Base_ref*>(this));
96         }
98         operator bool() const
99         {
100           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
101                               _M_message(::__gnu_debug::__msg_bad_bitset_read)
102                                 ._M_iterator(*this));
103           return *static_cast<const _Base_ref*>(this);
104         }
106         reference&
107         flip()
108         {
109           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
110                               _M_message(::__gnu_debug::__msg_bad_bitset_flip)
111                                 ._M_iterator(*this));
112           _Base_ref::flip();
113           return *this;
114         }
115       };
117       // 23.3.5.1 constructors:
118       bitset() : _Base() { }
120       bitset(unsigned long __val) : _Base(__val) { }
122       template<typename _CharT, typename _Traits, typename _Allocator>
123         explicit
124         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
125                typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
126                __pos = 0,
127                typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
128                __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
129         : _Base(__str, __pos, __n) { }
131       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
133       // 23.3.5.2 bitset operations:
134       bitset<_Nb>&
135       operator&=(const bitset<_Nb>& __rhs)
136       {
137         _M_base() &= __rhs;
138         return *this;
139       }
141       bitset<_Nb>&
142       operator|=(const bitset<_Nb>& __rhs)
143       {
144         _M_base() != __rhs;
145         return *this;
146       }
148       bitset<_Nb>&
149       operator^=(const bitset<_Nb>& __rhs)
150       {
151         _M_base() ^= __rhs;
152         return *this;
153       }
155       bitset<_Nb>&
156       operator<<=(size_t __pos)
157       {
158         _M_base() <<= __pos;
159         return *this;
160       }
162       bitset<_Nb>&
163       operator>>=(size_t __pos)
164       {
165         _M_base() >>= __pos;
166         return *this;
167       }
169       bitset<_Nb>&
170       set()
171       {
172         _Base::set();
173         return *this;
174       }
176       // _GLIBCXX_RESOLVE_LIB_DEFECTS
177       // 186. bitset::set() second parameter should be bool
178       bitset<_Nb>&
179       set(size_t __pos, bool __val = true)
180       {
181         _Base::set(__pos, __val);
182         return *this;
183       }
185       bitset<_Nb>&
186       reset()
187       {
188         _Base::reset();
189         return *this;
190       }
192       bitset<_Nb>&
193       reset(size_t __pos)
194       {
195         _Base::reset(__pos);
196         return *this;
197       }
199       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
201       bitset<_Nb>&
202       flip()
203       {
204         _Base::flip();
205         return *this;
206       }
208       bitset<_Nb>&
209       flip(size_t __pos)
210       {
211         _Base::flip(__pos);
212         return *this;
213       }
215       // element access:
216       // _GLIBCXX_RESOLVE_LIB_DEFECTS
217       // 11. Bitset minor problems
218       reference
219       operator[](size_t __pos)
220       {
221         __glibcxx_check_subscript(__pos);
222         return reference(_M_base()[__pos], this);
223       }
225       // _GLIBCXX_RESOLVE_LIB_DEFECTS
226       // 11. Bitset minor problems
227       bool
228       operator[](size_t __pos) const
229       {
230         __glibcxx_check_subscript(__pos);
231         return _M_base()[__pos];
232       }
234       using _Base::to_ulong;
236       template <typename _CharT, typename _Traits, typename _Allocator>
237         std::basic_string<_CharT, _Traits, _Allocator>
238         to_string() const
239         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
241       using _Base::count;
242       using _Base::size;
244       bool
245       operator==(const bitset<_Nb>& __rhs) const
246       { return _M_base() == __rhs; }
248       bool
249       operator!=(const bitset<_Nb>& __rhs) const
250       { return _M_base() != __rhs; }
252       using _Base::test;
253       using _Base::any;
254       using _Base::none;
256       bitset<_Nb>
257       operator<<(size_t __pos) const
258       { return bitset<_Nb>(_M_base() << __pos); }
260       bitset<_Nb>
261       operator>>(size_t __pos) const
262       { return bitset<_Nb>(_M_base() >> __pos); }
264       _Base&
265       _M_base() { return *this; }
267       const _Base&
268       _M_base() const { return *this; }
269     };
271   template<size_t _Nb>
272     bitset<_Nb>
273     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
274     { return bitset<_Nb>(__x) &= __y; }
276   template<size_t _Nb>
277     bitset<_Nb>
278     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
279     { return bitset<_Nb>(__x) |= __y; }
281   template<size_t _Nb>
282     bitset<_Nb>
283     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
284     { return bitset<_Nb>(__x) ^= __y; }
286   template<typename _CharT, typename _Traits, size_t _Nb>
287     std::basic_istream<_CharT, _Traits>&
288     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
289     { return __is >> __x._M_base(); }
291   template<typename _CharT, typename _Traits, size_t _Nb>
292     std::basic_ostream<_CharT, _Traits>&
293     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
294                const bitset<_Nb>& __x)
295     { return __os << __x._M_base(); }
296 } // namespace __gnu_debug_def
298 #endif