# debug/bitset

Go to the documentation of this file.

    00001 // Debugging bitset implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file debug/bitset
    00031  *  This file is a GNU debug extension to the Standard C++ Library.
    00032  */
    00033
    00034 #ifndef _GLIBCXX_DEBUG_BITSET
    00035 #define _GLIBCXX_DEBUG_BITSET
    00036
    00037 #include <bitset>
    00038 #include <debug/safe_sequence.h>
    00039 #include <debug/safe_iterator.h>
    00040
    00041 namespace std
    00042 {
    00043 namespace __debug
    00044 {
    00045   template<size_t _Nb>
    00046     class bitset
    00047     : public _GLIBCXX_STD_D::bitset<_Nb>,
    00048       public __gnu_debug::_Safe_sequence_base
    00049     {
    00050       typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
    00051       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
    00052
    00053     public:
    00054       // bit reference:
    00055       class reference
    00056       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
    00057       {
    00058     typedef typename _Base::reference _Base_ref;
    00059
    00060     friend class bitset;
    00061     reference();
    00062
    00063     reference(const _Base_ref& __base, bitset* __seq)
    00064     : _Base_ref(__base), _Safe_iterator_base(__seq, false)
    00065     { }
    00066
    00067       public:
    00068     reference(const reference& __x)
    00069     : _Base_ref(__x), _Safe_iterator_base(__x, false)
    00070     { }
    00071
    00072     reference&
    00073     operator=(bool __x)
    00074     {
    00075       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00076                   _M_message(__gnu_debug::__msg_bad_bitset_write)
    00077                 ._M_iterator(*this));
    00078       *static_cast<_Base_ref*>(this) = __x;
    00079       return *this;
    00080     }
    00081
    00082     reference&
    00083     operator=(const reference& __x)
    00084     {
    00085       _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
    00086                    _M_message(__gnu_debug::__msg_bad_bitset_read)
    00087                 ._M_iterator(__x));
    00088       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00089                   _M_message(__gnu_debug::__msg_bad_bitset_write)
    00090                 ._M_iterator(*this));
    00091       *static_cast<_Base_ref*>(this) = __x;
    00092       return *this;
    00093     }
    00094
    00095     bool
    00096     operator~() const
    00097     {
    00098       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00099                    _M_message(__gnu_debug::__msg_bad_bitset_read)
    00100                 ._M_iterator(*this));
    00101       return ~(*static_cast<const _Base_ref*>(this));
    00102     }
    00103
    00104     operator bool() const
    00105     {
    00106       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00107                   _M_message(__gnu_debug::__msg_bad_bitset_read)
    00108                 ._M_iterator(*this));
    00109       return *static_cast<const _Base_ref*>(this);
    00110     }
    00111
    00112     reference&
    00113     flip()
    00114     {
    00115       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00116                   _M_message(__gnu_debug::__msg_bad_bitset_flip)
    00117                 ._M_iterator(*this));
    00118       _Base_ref::flip();
    00119       return *this;
    00120     }
    00121       };
    00122
    00123       // 23.3.5.1 constructors:
    00124       bitset() : _Base() { }
    00125
    00126       bitset(unsigned long __val) : _Base(__val) { }
    00127
    00128       template<typename _CharT, typename _Traits, typename _Allocator>
    00129         explicit
    00130         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
    00131            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
    00132            __pos = 0,
    00133            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
    00134            __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
    00135     : _Base(__str, __pos, __n) { }
    00136
    00137       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
    00138
    00139       // 23.3.5.2 bitset operations:
    00140       bitset<_Nb>&
    00141       operator&=(const bitset<_Nb>& __rhs)
    00142       {
    00143     _M_base() &= __rhs;
    00144     return *this;
    00145       }
    00146
    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<<=(size_t __pos)
    00163       {
    00164     _M_base() <<= __pos;
    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       set()
    00177       {
    00178     _Base::set();
    00179     return *this;
    00180       }
    00181
    00182       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00183       // 186. bitset::set() second parameter should be bool
    00184       bitset<_Nb>&
    00185       set(size_t __pos, bool __val = true)
    00186       {
    00187     _Base::set(__pos, __val);
    00188     return *this;
    00189       }
    00190
    00191       bitset<_Nb>&
    00192       reset()
    00193       {
    00194     _Base::reset();
    00195     return *this;
    00196       }
    00197
    00198       bitset<_Nb>&
    00199       reset(size_t __pos)
    00200       {
    00201     _Base::reset(__pos);
    00202     return *this;
    00203       }
    00204
    00205       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
    00206
    00207       bitset<_Nb>&
    00208       flip()
    00209       {
    00210     _Base::flip();
    00211     return *this;
    00212       }
    00213
    00214       bitset<_Nb>&
    00215       flip(size_t __pos)
    00216       {
    00217     _Base::flip(__pos);
    00218     return *this;
    00219       }
    00220
    00221       // element access:
    00222       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00223       // 11. Bitset minor problems
    00224       reference
    00225       operator[](size_t __pos)
    00226       {
    00227     __glibcxx_check_subscript(__pos);
    00228     return reference(_M_base()[__pos], this);
    00229       }
    00230
    00231       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00232       // 11. Bitset minor problems
    00233       bool
    00234       operator[](size_t __pos) const
    00235       {
    00236     __glibcxx_check_subscript(__pos);
    00237     return _M_base()[__pos];
    00238       }
    00239
    00240       using _Base::to_ulong;
    00241
    00242       template <typename _CharT, typename _Traits, typename _Allocator>
    00243         std::basic_string<_CharT, _Traits, _Allocator>
    00244         to_string() const
    00245         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
    00246
    00247       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00248       // 434. bitset::to_string() hard to use.
    00249       template<typename _CharT, typename _Traits>
    00250         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
    00251         to_string() const
    00252         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
    00253
    00254       template<typename _CharT>
    00255         std::basic_string<_CharT, std::char_traits<_CharT>,
    00256                           std::allocator<_CharT> >
    00257         to_string() const
    00258         {
    00259           return to_string<_CharT, std::char_traits<_CharT>,
    00260                            std::allocator<_CharT> >();
    00261         }
    00262
    00263       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
    00264         to_string() const
    00265         {
    00266           return to_string<char,std::char_traits<char>,std::allocator<char> >();
    00267         }
    00268
    00269       using _Base::count;
    00270       using _Base::size;
    00271
    00272       bool
    00273       operator==(const bitset<_Nb>& __rhs) const
    00274       { return _M_base() == __rhs; }
    00275
    00276       bool
    00277       operator!=(const bitset<_Nb>& __rhs) const
    00278       { return _M_base() != __rhs; }
    00279
    00280       using _Base::test;
    00281       using _Base::all;
    00282       using _Base::any;
    00283       using _Base::none;
    00284
    00285       bitset<_Nb>
    00286       operator<<(size_t __pos) const
    00287       { return bitset<_Nb>(_M_base() << __pos); }
    00288
    00289       bitset<_Nb>
    00290       operator>>(size_t __pos) const
    00291       { return bitset<_Nb>(_M_base() >> __pos); }
    00292
    00293       _Base&
    00294       _M_base() { return *this; }
    00295
    00296       const _Base&
    00297       _M_base() const { return *this; }
    00298     };
    00299
    00300   template<size_t _Nb>
    00301     bitset<_Nb>
    00302     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00303     { return bitset<_Nb>(__x) &= __y; }
    00304
    00305   template<size_t _Nb>
    00306     bitset<_Nb>
    00307     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00308     { return bitset<_Nb>(__x) |= __y; }
    00309
    00310   template<size_t _Nb>
    00311     bitset<_Nb>
    00312     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00313     { return bitset<_Nb>(__x) ^= __y; }
    00314
    00315   template<typename _CharT, typename _Traits, size_t _Nb>
    00316     std::basic_istream<_CharT, _Traits>&
    00317     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
    00318     { return __is >> __x._M_base(); }
    00319
    00320   template<typename _CharT, typename _Traits, size_t _Nb>
    00321     std::basic_ostream<_CharT, _Traits>&
    00322     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
    00323            const bitset<_Nb>& __x)
    00324     { return __os << __x._M_base(); }
    00325 } // namespace __debug
    00326 } // namespace std
    00327
    00328 #endif