# bitset

    00001 // Debugging bitset implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 #ifndef _GLIBCXX_DEBUG_BITSET
    00032 #define _GLIBCXX_DEBUG_BITSET
    00033
    00034 #include <bitset>
    00035 #include <debug/safe_sequence.h>
    00036 #include <debug/safe_iterator.h>
    00037
    00038 namespace __gnu_debug_def
    00039 {
    00040   template<size_t _Nb>
    00041     class bitset
    00042     : public _GLIBCXX_STD::bitset<_Nb>,
    00043       public __gnu_debug::_Safe_sequence_base
    00044     {
    00045       typedef _GLIBCXX_STD::bitset<_Nb> _Base;
    00046       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
    00047
    00048     public:
    00049       // bit reference:
    00050       class reference
    00051       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
    00052       {
    00053     typedef typename _Base::reference _Base_ref;
    00054
    00055     friend class bitset;
    00056     reference();
    00057
    00058     reference(const _Base_ref& __base, bitset* __seq)
    00059     : _Base_ref(__base), _Safe_iterator_base(__seq, false)
    00060     { }
    00061
    00062       public:
    00063     reference(const reference& __x)
    00064     : _Base_ref(__x), _Safe_iterator_base(__x, false)
    00065     { }
    00066
    00067     reference&
    00068     operator=(bool __x)
    00069     {
    00070       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00071                   _M_message(::__gnu_debug::__msg_bad_bitset_write)
    00072                 ._M_iterator(*this));
    00073       *static_cast<_Base_ref*>(this) = __x;
    00074       return *this;
    00075     }
    00076
    00077     reference&
    00078     operator=(const reference& __x)
    00079     {
    00080       _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
    00081                    _M_message(::__gnu_debug::__msg_bad_bitset_read)
    00082                 ._M_iterator(__x));
    00083       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00084                   _M_message(::__gnu_debug::__msg_bad_bitset_write)
    00085                 ._M_iterator(*this));
    00086       *static_cast<_Base_ref*>(this) = __x;
    00087       return *this;
    00088     }
    00089
    00090     bool
    00091     operator~() const
    00092     {
    00093       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00094                    _M_message(::__gnu_debug::__msg_bad_bitset_read)
    00095                 ._M_iterator(*this));
    00096       return ~(*static_cast<const _Base_ref*>(this));
    00097     }
    00098
    00099     operator bool() const
    00100     {
    00101       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00102                   _M_message(::__gnu_debug::__msg_bad_bitset_read)
    00103                 ._M_iterator(*this));
    00104       return *static_cast<const _Base_ref*>(this);
    00105     }
    00106
    00107     reference&
    00108     flip()
    00109     {
    00110       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
    00111                   _M_message(::__gnu_debug::__msg_bad_bitset_flip)
    00112                 ._M_iterator(*this));
    00113       _Base_ref::flip();
    00114       return *this;
    00115     }
    00116       };
    00117
    00118       // 23.3.5.1 constructors:
    00119       bitset() : _Base() { }
    00120
    00121       bitset(unsigned long __val) : _Base(__val) { }
    00122
    00123       template<typename _CharT, typename _Traits, typename _Allocator>
    00124         explicit
    00125         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
    00126            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
    00127            __pos = 0,
    00128            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
    00129            __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
    00130     : _Base(__str, __pos, __n) { }
    00131
    00132       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
    00133
    00134       // 23.3.5.2 bitset operations:
    00135       bitset<_Nb>&
    00136       operator&=(const bitset<_Nb>& __rhs)
    00137       {
    00138     _M_base() &= __rhs;
    00139     return *this;
    00140       }
    00141
    00142       bitset<_Nb>&
    00143       operator|=(const bitset<_Nb>& __rhs)
    00144       {
    00145     _M_base() |= __rhs;
    00146     return *this;
    00147       }
    00148
    00149       bitset<_Nb>&
    00150       operator^=(const bitset<_Nb>& __rhs)
    00151       {
    00152     _M_base() ^= __rhs;
    00153     return *this;
    00154       }
    00155
    00156       bitset<_Nb>&
    00157       operator<<=(size_t __pos)
    00158       {
    00159     _M_base() <<= __pos;
    00160     return *this;
    00161       }
    00162
    00163       bitset<_Nb>&
    00164       operator>>=(size_t __pos)
    00165       {
    00166     _M_base() >>= __pos;
    00167     return *this;
    00168       }
    00169
    00170       bitset<_Nb>&
    00171       set()
    00172       {
    00173     _Base::set();
    00174     return *this;
    00175       }
    00176
    00177       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00178       // 186. bitset::set() second parameter should be bool
    00179       bitset<_Nb>&
    00180       set(size_t __pos, bool __val = true)
    00181       {
    00182     _Base::set(__pos, __val);
    00183     return *this;
    00184       }
    00185
    00186       bitset<_Nb>&
    00187       reset()
    00188       {
    00189     _Base::reset();
    00190     return *this;
    00191       }
    00192
    00193       bitset<_Nb>&
    00194       reset(size_t __pos)
    00195       {
    00196     _Base::reset(__pos);
    00197     return *this;
    00198       }
    00199
    00200       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
    00201
    00202       bitset<_Nb>&
    00203       flip()
    00204       {
    00205     _Base::flip();
    00206     return *this;
    00207       }
    00208
    00209       bitset<_Nb>&
    00210       flip(size_t __pos)
    00211       {
    00212     _Base::flip(__pos);
    00213     return *this;
    00214       }
    00215
    00216       // element access:
    00217       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00218       // 11. Bitset minor problems
    00219       reference
    00220       operator[](size_t __pos)
    00221       {
    00222     __glibcxx_check_subscript(__pos);
    00223     return reference(_M_base()[__pos], this);
    00224       }
    00225
    00226       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00227       // 11. Bitset minor problems
    00228       bool
    00229       operator[](size_t __pos) const
    00230       {
    00231     __glibcxx_check_subscript(__pos);
    00232     return _M_base()[__pos];
    00233       }
    00234
    00235       using _Base::to_ulong;
    00236
    00237       template <typename _CharT, typename _Traits, typename _Allocator>
    00238         std::basic_string<_CharT, _Traits, _Allocator>
    00239         to_string() const
    00240         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
    00241
    00242       using _Base::count;
    00243       using _Base::size;
    00244
    00245       bool
    00246       operator==(const bitset<_Nb>& __rhs) const
    00247       { return _M_base() == __rhs; }
    00248
    00249       bool
    00250       operator!=(const bitset<_Nb>& __rhs) const
    00251       { return _M_base() != __rhs; }
    00252
    00253       using _Base::test;
    00254       using _Base::any;
    00255       using _Base::none;
    00256
    00257       bitset<_Nb>
    00258       operator<<(size_t __pos) const
    00259       { return bitset<_Nb>(_M_base() << __pos); }
    00260
    00261       bitset<_Nb>
    00262       operator>>(size_t __pos) const
    00263       { return bitset<_Nb>(_M_base() >> __pos); }
    00264
    00265       _Base&
    00266       _M_base() { return *this; }
    00267
    00268       const _Base&
    00269       _M_base() const { return *this; }
    00270     };
    00271
    00272   template<size_t _Nb>
    00273     bitset<_Nb>
    00274     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00275     { return bitset<_Nb>(__x) &= __y; }
    00276
    00277   template<size_t _Nb>
    00278     bitset<_Nb>
    00279     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00280     { return bitset<_Nb>(__x) |= __y; }
    00281
    00282   template<size_t _Nb>
    00283     bitset<_Nb>
    00284     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
    00285     { return bitset<_Nb>(__x) ^= __y; }
    00286
    00287   template<typename _CharT, typename _Traits, size_t _Nb>
    00288     std::basic_istream<_CharT, _Traits>&
    00289     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
    00290     { return __is >> __x._M_base(); }
    00291
    00292   template<typename _CharT, typename _Traits, size_t _Nb>
    00293     std::basic_ostream<_CharT, _Traits>&
    00294     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
    00295            const bitset<_Nb>& __x)
    00296     { return __os << __x._M_base(); }
    00297 } // namespace __gnu_debug_def
    00298
    00299 #endif