# debug/bitset

Go to the documentation of this file.

    00001 // Debugging bitset implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 3, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /** @file debug/bitset
    00027  *  This file is a GNU debug extension to the Standard C++ Library.
    00028  */
    00029
    00030 #ifndef _GLIBCXX_DEBUG_BITSET
    00031 #define _GLIBCXX_DEBUG_BITSET
    00032
    00033 #include <bitset>
    00034 #include <debug/safe_sequence.h>
    00035 #include <debug/safe_iterator.h>
    00036
    00037 namespace std
    00038 {
    00039 namespace __debug
    00040 {
    00041   template<size_t _Nb>
    00042     class bitset
    00043     : public _GLIBCXX_STD_D::bitset<_Nb>,
    00044       public __gnu_debug::_Safe_sequence_base
    00045     {
    00046       typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
    00047       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
    00048
    00049     public:
    00050       // bit reference:
    00051       class reference
    00052       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
    00053       {
    00054     typedef typename _Base::reference _Base_ref;
    00055
    00056     friend class bitset;
    00057     reference();
    00058
    00059     reference(const _Base_ref& __base, bitset* __seq)
    00060     : _Base_ref(__base), _Safe_iterator_base(__seq, false)
    00061     { }
    00062
    00063       public:
    00064     reference(const reference& __x)
    00065     : _Base_ref(__x), _Safe_iterator_base(__x, false)
    00066     { }
    00067
    00068     reference&
    00069     operator=(bool __x)
    00070     {
    00071       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00072                   _M_message(__gnu_debug::__msg_bad_bitset_write)
    00073                 ._M_iterator(*this));
    00074       *static_cast<_Base_ref*>(this) = __x;
    00075       return *this;
    00076     }
    00077
    00078     reference&
    00079     operator=(const reference& __x)
    00080     {
    00081       _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
    00082                    _M_message(__gnu_debug::__msg_bad_bitset_read)
    00083                 ._M_iterator(__x));
    00084       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00085                   _M_message(__gnu_debug::__msg_bad_bitset_write)
    00086                 ._M_iterator(*this));
    00087       *static_cast<_Base_ref*>(this) = __x;
    00088       return *this;
    00089     }
    00090
    00091     bool
    00092     operator~() const
    00093     {
    00094       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00095                    _M_message(__gnu_debug::__msg_bad_bitset_read)
    00096                 ._M_iterator(*this));
    00097       return ~(*static_cast<const _Base_ref*>(this));
    00098     }
    00099
    00100     operator bool() const
    00101     {
    00102       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00103                   _M_message(__gnu_debug::__msg_bad_bitset_read)
    00104                 ._M_iterator(*this));
    00105       return *static_cast<const _Base_ref*>(this);
    00106     }
    00107
    00108     reference&
    00109     flip()
    00110     {
    00111       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00112                   _M_message(__gnu_debug::__msg_bad_bitset_flip)
    00113                 ._M_iterator(*this));
    00114       _Base_ref::flip();
    00115       return *this;
    00116     }
    00117       };
    00118
    00119       // 23.3.5.1 constructors:
    00120       bitset() : _Base() { }
    00121
    00122       bitset(unsigned long __val) : _Base(__val) { }
    00123
    00124       template<typename _CharT, typename _Traits, typename _Alloc>
    00125         explicit
    00126         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
    00127            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
    00128            __pos = 0,
    00129            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
    00130            __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
    00131     : _Base(__str, __pos, __n) { }
    00132
    00133       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00134       // 396. what are characters zero and one.
    00135       template<class _CharT, class _Traits, class _Alloc>
    00136     bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
    00137            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
    00138            __pos,
    00139            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
    00140            __n,
    00141            _CharT __zero, _CharT __one = _CharT('1'))
    00142     : _Base(__str, __pos, __n, __zero, __one) { }
    00143
    00144       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
    00145
    00146       // 23.3.5.2 bitset operations:
    00147       bitset<_Nb>&
    00148       operator&=(const bitset<_Nb>& __rhs)
    00149       {
    00150     _M_base() &= __rhs;
    00151     return *this;
    00152       }
    00153
    00154       bitset<_Nb>&
    00155       operator|=(const bitset<_Nb>& __rhs)
    00156       {
    00157     _M_base() |= __rhs;
    00158     return *this;
    00159       }
    00160
    00161       bitset<_Nb>&
    00162       operator^=(const bitset<_Nb>& __rhs)
    00163       {
    00164     _M_base() ^= __rhs;
    00165     return *this;
    00166       }
    00167
    00168       bitset<_Nb>&
    00169       operator<<=(size_t __pos)
    00170       {
    00171     _M_base() <<= __pos;
    00172     return *this;
    00173       }
    00174
    00175       bitset<_Nb>&
    00176       operator>>=(size_t __pos)
    00177       {
    00178     _M_base() >>= __pos;
    00179     return *this;
    00180       }
    00181
    00182       bitset<_Nb>&
    00183       set()
    00184       {
    00185     _Base::set();
    00186     return *this;
    00187       }
    00188
    00189       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00190       // 186. bitset::set() second parameter should be bool
    00191       bitset<_Nb>&
    00192       set(size_t __pos, bool __val = true)
    00193       {
    00194     _Base::set(__pos, __val);
    00195     return *this;
    00196       }
    00197
    00198       bitset<_Nb>&
    00199       reset()
    00200       {
    00201     _Base::reset();
    00202     return *this;
    00203       }
    00204
    00205       bitset<_Nb>&
    00206       reset(size_t __pos)
    00207       {
    00208     _Base::reset(__pos);
    00209     return *this;
    00210       }
    00211
    00212       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
    00213
    00214       bitset<_Nb>&
    00215       flip()
    00216       {
    00217     _Base::flip();
    00218     return *this;
    00219       }
    00220
    00221       bitset<_Nb>&
    00222       flip(size_t __pos)
    00223       {
    00224     _Base::flip(__pos);
    00225     return *this;
    00226       }
    00227
    00228       // element access:
    00229       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00230       // 11. Bitset minor problems
    00231       reference
    00232       operator[](size_t __pos)
    00233       {
    00234     __glibcxx_check_subscript(__pos);
    00235     return reference(_M_base()[__pos], this);
    00236       }
    00237
    00238       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00239       // 11. Bitset minor problems
    00240       bool
    00241       operator[](size_t __pos) const
    00242       {
    00243     __glibcxx_check_subscript(__pos);
    00244     return _M_base()[__pos];
    00245       }
    00246
    00247       using _Base::to_ulong;
    00248
    00249       template <typename _CharT, typename _Traits, typename _Alloc>
    00250         std::basic_string<_CharT, _Traits, _Alloc>
    00251         to_string() const
    00252         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
    00253
    00254       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00255       // 396. what are characters zero and one.
    00256       template<class _CharT, class _Traits, class _Alloc>
    00257     std::basic_string<_CharT, _Traits, _Alloc>
    00258     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
    00259     {
    00260       return _M_base().template
    00261         to_string<_CharT, _Traits, _Alloc>(__zero, __one);
    00262     }
    00263
    00264       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00265       // 434. bitset::to_string() hard to use.
    00266       template<typename _CharT, typename _Traits>
    00267         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
    00268         to_string() const
    00269         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
    00270
    00271       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00272       // 853. to_string needs updating with zero and one.
    00273       template<class _CharT, class _Traits>
    00274     std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
    00275     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
    00276     { return to_string<_CharT, _Traits,
    00277                        std::allocator<_CharT> >(__zero, __one); }
    00278
    00279       template<typename _CharT>
    00280         std::basic_string<_CharT, std::char_traits<_CharT>,
    00281                           std::allocator<_CharT> >
    00282         to_string() const
    00283         {
    00284           return to_string<_CharT, std::char_traits<_CharT>,
    00285                            std::allocator<_CharT> >();
    00286         }
    00287
    00288       template<class _CharT>
    00289     std::basic_string<_CharT, std::char_traits<_CharT>,
    00290                       std::allocator<_CharT> >
    00291     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
    00292     {
    00293       return to_string<_CharT, std::char_traits<_CharT>,
    00294                        std::allocator<_CharT> >(__zero, __one);
    00295     }
    00296
    00297       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
    00298       to_string() const
    00299       {
    00300     return to_string<char,std::char_traits<char>,std::allocator<char> >();
    00301       }
    00302
    00303       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
    00304       to_string(char __zero, char __one = '1') const
    00305       {
    00306     return to_string<char, std::char_traits<char>,
    00307                      std::allocator<char> >(__zero, __one);
    00308       }
    00309
    00310       using _Base::count;
    00311       using _Base::size;
    00312
    00313       bool
    00314       operator==(const bitset<_Nb>& __rhs) const
    00315       { return _M_base() == __rhs; }
    00316
    00317       bool
    00318       operator!=(const bitset<_Nb>& __rhs) const
    00319       { return _M_base() != __rhs; }
    00320
    00321       using _Base::test;
    00322       using _Base::all;
    00323       using _Base::any;
    00324       using _Base::none;
    00325
    00326       bitset<_Nb>
    00327       operator<<(size_t __pos) const
    00328       { return bitset<_Nb>(_M_base() << __pos); }
    00329
    00330       bitset<_Nb>
    00331       operator>>(size_t __pos) const
    00332       { return bitset<_Nb>(_M_base() >> __pos); }
    00333
    00334       _Base&
    00335       _M_base() { return *this; }
    00336
    00337       const _Base&
    00338       _M_base() const { return *this; }
    00339     };
    00340
    00341   template<size_t _Nb>
    00342     bitset<_Nb>
    00343     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00344     { return bitset<_Nb>(__x) &= __y; }
    00345
    00346   template<size_t _Nb>
    00347     bitset<_Nb>
    00348     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00349     { return bitset<_Nb>(__x) |= __y; }
    00350
    00351   template<size_t _Nb>
    00352     bitset<_Nb>
    00353     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00354     { return bitset<_Nb>(__x) ^= __y; }
    00355
    00356   template<typename _CharT, typename _Traits, size_t _Nb>
    00357     std::basic_istream<_CharT, _Traits>&
    00358     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
    00359     { return __is >> __x._M_base(); }
    00360
    00361   template<typename _CharT, typename _Traits, size_t _Nb>
    00362     std::basic_ostream<_CharT, _Traits>&
    00363     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
    00364            const bitset<_Nb>& __x)
    00365     { return __os << __x._M_base(); }
    00366 } // namespace __debug
    00367 } // namespace std
    00368
    00369 #endif