2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / debug / bitset
blob75474c806b5156aa4759204b5d84c865f15c2f42
1 // Debugging bitset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32  */
34 #ifndef _GLIBCXX_DEBUG_BITSET
35 #define _GLIBCXX_DEBUG_BITSET
37 #include <bitset>
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
41 namespace std
43 namespace __debug
45   template<size_t _Nb>
46     class bitset
47     : public _GLIBCXX_STD_D::bitset<_Nb>, 
48       public __gnu_debug::_Safe_sequence_base
49     {
50       typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
51       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
53     public:
54       // bit reference:
55       class reference
56       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
57       {
58         typedef typename _Base::reference _Base_ref;
60         friend class bitset;
61         reference();
63         reference(const _Base_ref& __base, bitset* __seq)
64         : _Base_ref(__base), _Safe_iterator_base(__seq, false)
65         { }
67       public:
68         reference(const reference& __x)
69         : _Base_ref(__x), _Safe_iterator_base(__x, false)
70         { }
72         reference&
73         operator=(bool __x)
74         {
75           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
76                               _M_message(__gnu_debug::__msg_bad_bitset_write)
77                                 ._M_iterator(*this));
78           *static_cast<_Base_ref*>(this) = __x;
79           return *this;
80         }
82         reference&
83         operator=(const reference& __x)
84         {
85           _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
86                                _M_message(__gnu_debug::__msg_bad_bitset_read)
87                                 ._M_iterator(__x));
88           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
89                               _M_message(__gnu_debug::__msg_bad_bitset_write)
90                                 ._M_iterator(*this));
91           *static_cast<_Base_ref*>(this) = __x;
92           return *this;
93         }
95         bool
96         operator~() const
97         {
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));
102         }
104         operator bool() const
105         {
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);
110         }
112         reference&
113         flip()
114         {
115           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
116                               _M_message(__gnu_debug::__msg_bad_bitset_flip)
117                                 ._M_iterator(*this));
118           _Base_ref::flip();
119           return *this;
120         }
121       };
123       // 23.3.5.1 constructors:
124       bitset() : _Base() { }
126       bitset(unsigned long __val) : _Base(__val) { }
128       template<typename _CharT, typename _Traits, typename _Allocator>
129         explicit
130         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
131                typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
132                __pos = 0,
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       // _GLIBCXX_RESOLVE_LIB_DEFECTS
138       // 778. std::bitset does not have any constructor taking a string literal
139       explicit
140       bitset(const char* __s)
141       : _Base(__s) { }
143       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
145       // 23.3.5.2 bitset operations:
146       bitset<_Nb>&
147       operator&=(const bitset<_Nb>& __rhs)
148       {
149         _M_base() &= __rhs;
150         return *this;
151       }
153       bitset<_Nb>&
154       operator|=(const bitset<_Nb>& __rhs)
155       {
156         _M_base() |= __rhs;
157         return *this;
158       }
160       bitset<_Nb>&
161       operator^=(const bitset<_Nb>& __rhs)
162       {
163         _M_base() ^= __rhs;
164         return *this;
165       }
167       bitset<_Nb>&
168       operator<<=(size_t __pos)
169       {
170         _M_base() <<= __pos;
171         return *this;
172       }
174       bitset<_Nb>&
175       operator>>=(size_t __pos)
176       {
177         _M_base() >>= __pos;
178         return *this;
179       }
181       bitset<_Nb>&
182       set()
183       {
184         _Base::set();
185         return *this;
186       }
188       // _GLIBCXX_RESOLVE_LIB_DEFECTS
189       // 186. bitset::set() second parameter should be bool
190       bitset<_Nb>&
191       set(size_t __pos, bool __val = true)
192       {
193         _Base::set(__pos, __val);
194         return *this;
195       }
197       bitset<_Nb>&
198       reset()
199       {
200         _Base::reset();
201         return *this;
202       }
204       bitset<_Nb>&
205       reset(size_t __pos)
206       {
207         _Base::reset(__pos);
208         return *this;
209       }
211       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
213       bitset<_Nb>&
214       flip()
215       {
216         _Base::flip();
217         return *this;
218       }
220       bitset<_Nb>&
221       flip(size_t __pos)
222       {
223         _Base::flip(__pos);
224         return *this;
225       }
227       // element access:
228       // _GLIBCXX_RESOLVE_LIB_DEFECTS
229       // 11. Bitset minor problems
230       reference
231       operator[](size_t __pos)
232       {
233         __glibcxx_check_subscript(__pos);
234         return reference(_M_base()[__pos], this);
235       }
237       // _GLIBCXX_RESOLVE_LIB_DEFECTS
238       // 11. Bitset minor problems
239       bool
240       operator[](size_t __pos) const
241       {
242         __glibcxx_check_subscript(__pos);
243         return _M_base()[__pos];
244       }
246       using _Base::to_ulong;
248       template <typename _CharT, typename _Traits, typename _Allocator>
249         std::basic_string<_CharT, _Traits, _Allocator>
250         to_string() const
251         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
253       // _GLIBCXX_RESOLVE_LIB_DEFECTS
254       // 434. bitset::to_string() hard to use.
255       template<typename _CharT, typename _Traits>
256         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
257         to_string() const
258         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
260       template<typename _CharT>
261         std::basic_string<_CharT, std::char_traits<_CharT>,
262                           std::allocator<_CharT> >
263         to_string() const
264         {
265           return to_string<_CharT, std::char_traits<_CharT>,
266                            std::allocator<_CharT> >();
267         }
269       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
270         to_string() const
271         {
272           return to_string<char,std::char_traits<char>,std::allocator<char> >();
273         }
275       using _Base::count;
276       using _Base::size;
278       bool
279       operator==(const bitset<_Nb>& __rhs) const
280       { return _M_base() == __rhs; }
282       bool
283       operator!=(const bitset<_Nb>& __rhs) const
284       { return _M_base() != __rhs; }
286       using _Base::test;
287       using _Base::all;
288       using _Base::any;
289       using _Base::none;
291       bitset<_Nb>
292       operator<<(size_t __pos) const
293       { return bitset<_Nb>(_M_base() << __pos); }
295       bitset<_Nb>
296       operator>>(size_t __pos) const
297       { return bitset<_Nb>(_M_base() >> __pos); }
299       _Base&
300       _M_base() { return *this; }
302       const _Base&
303       _M_base() const { return *this; }
304     };
306   template<size_t _Nb>
307     bitset<_Nb>
308     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
309     { return bitset<_Nb>(__x) &= __y; }
311   template<size_t _Nb>
312     bitset<_Nb>
313     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
314     { return bitset<_Nb>(__x) |= __y; }
316   template<size_t _Nb>
317     bitset<_Nb>
318     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
319     { return bitset<_Nb>(__x) ^= __y; }
321   template<typename _CharT, typename _Traits, size_t _Nb>
322     std::basic_istream<_CharT, _Traits>&
323     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
324     { return __is >> __x._M_base(); }
326   template<typename _CharT, typename _Traits, size_t _Nb>
327     std::basic_ostream<_CharT, _Traits>&
328     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
329                const bitset<_Nb>& __x)
330     { return __os << __x._M_base(); }
331 } // namespace __debug
332 } // namespace std
334 #endif