]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libstdc++/include/debug/bitset
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libstdc++ / include / debug / bitset
1 // Debugging bitset implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005
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.
11
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.
16
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,
20 // USA.
21
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.
30
31 /** @file debug/bitset
32  *  This file is a GNU debug extension to the Standard C++ Library.
33  */
34
35 #ifndef _GLIBCXX_DEBUG_BITSET
36 #define _GLIBCXX_DEBUG_BITSET
37
38 #include <bitset>
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
41
42 namespace std
43 {
44 namespace __debug
45 {
46   template<size_t _Nb>
47     class bitset
48     : public _GLIBCXX_STD::bitset<_Nb>, 
49       public __gnu_debug::_Safe_sequence_base
50     {
51       typedef _GLIBCXX_STD::bitset<_Nb> _Base;
52       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
53
54     public:
55       // bit reference:
56       class reference
57       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
58       {
59         typedef typename _Base::reference _Base_ref;
60
61         friend class bitset;
62         reference();
63
64         reference(const _Base_ref& __base, bitset* __seq)
65         : _Base_ref(__base), _Safe_iterator_base(__seq, false)
66         { }
67
68       public:
69         reference(const reference& __x)
70         : _Base_ref(__x), _Safe_iterator_base(__x, false)
71         { }
72
73         reference&
74         operator=(bool __x)
75         {
76           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
77                               _M_message(__gnu_debug::__msg_bad_bitset_write)
78                                 ._M_iterator(*this));
79           *static_cast<_Base_ref*>(this) = __x;
80           return *this;
81         }
82
83         reference&
84         operator=(const reference& __x)
85         {
86           _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
87                                _M_message(__gnu_debug::__msg_bad_bitset_read)
88                                 ._M_iterator(__x));
89           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
90                               _M_message(__gnu_debug::__msg_bad_bitset_write)
91                                 ._M_iterator(*this));
92           *static_cast<_Base_ref*>(this) = __x;
93           return *this;
94         }
95
96         bool
97         operator~() const
98         {
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));
103         }
104
105         operator bool() const
106         {
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);
111         }
112
113         reference&
114         flip()
115         {
116           _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
117                               _M_message(__gnu_debug::__msg_bad_bitset_flip)
118                                 ._M_iterator(*this));
119           _Base_ref::flip();
120           return *this;
121         }
122       };
123
124       // 23.3.5.1 constructors:
125       bitset() : _Base() { }
126
127       bitset(unsigned long __val) : _Base(__val) { }
128
129       template<typename _CharT, typename _Traits, typename _Allocator>
130         explicit
131         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
132                typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
133                __pos = 0,
134                typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
135                __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
136         : _Base(__str, __pos, __n) { }
137
138       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
139
140       // 23.3.5.2 bitset operations:
141       bitset<_Nb>&
142       operator&=(const bitset<_Nb>& __rhs)
143       {
144         _M_base() &= __rhs;
145         return *this;
146       }
147
148       bitset<_Nb>&
149       operator|=(const bitset<_Nb>& __rhs)
150       {
151         _M_base() |= __rhs;
152         return *this;
153       }
154
155       bitset<_Nb>&
156       operator^=(const bitset<_Nb>& __rhs)
157       {
158         _M_base() ^= __rhs;
159         return *this;
160       }
161
162       bitset<_Nb>&
163       operator<<=(size_t __pos)
164       {
165         _M_base() <<= __pos;
166         return *this;
167       }
168
169       bitset<_Nb>&
170       operator>>=(size_t __pos)
171       {
172         _M_base() >>= __pos;
173         return *this;
174       }
175
176       bitset<_Nb>&
177       set()
178       {
179         _Base::set();
180         return *this;
181       }
182
183       // _GLIBCXX_RESOLVE_LIB_DEFECTS
184       // 186. bitset::set() second parameter should be bool
185       bitset<_Nb>&
186       set(size_t __pos, bool __val = true)
187       {
188         _Base::set(__pos, __val);
189         return *this;
190       }
191
192       bitset<_Nb>&
193       reset()
194       {
195         _Base::reset();
196         return *this;
197       }
198
199       bitset<_Nb>&
200       reset(size_t __pos)
201       {
202         _Base::reset(__pos);
203         return *this;
204       }
205
206       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
207
208       bitset<_Nb>&
209       flip()
210       {
211         _Base::flip();
212         return *this;
213       }
214
215       bitset<_Nb>&
216       flip(size_t __pos)
217       {
218         _Base::flip(__pos);
219         return *this;
220       }
221
222       // element access:
223       // _GLIBCXX_RESOLVE_LIB_DEFECTS
224       // 11. Bitset minor problems
225       reference
226       operator[](size_t __pos)
227       {
228         __glibcxx_check_subscript(__pos);
229         return reference(_M_base()[__pos], this);
230       }
231
232       // _GLIBCXX_RESOLVE_LIB_DEFECTS
233       // 11. Bitset minor problems
234       bool
235       operator[](size_t __pos) const
236       {
237         __glibcxx_check_subscript(__pos);
238         return _M_base()[__pos];
239       }
240
241       using _Base::to_ulong;
242
243       template <typename _CharT, typename _Traits, typename _Allocator>
244         std::basic_string<_CharT, _Traits, _Allocator>
245         to_string() const
246         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
247
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> >
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