debug/bitset

Go to the documentation of this file.
00001 // Debugging bitset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file debug/bitset
00032  *  This file is a GNU debug extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _GLIBCXX_DEBUG_BITSET
00036 #define _GLIBCXX_DEBUG_BITSET
00037 
00038 #include <bitset>
00039 #include <debug/safe_sequence.h>
00040 #include <debug/safe_iterator.h>
00041 
00042 namespace std
00043 {
00044 namespace __debug
00045 {
00046   template<size_t _Nb>
00047     class bitset
00048     : public _GLIBCXX_STD::bitset<_Nb>, 
00049       public __gnu_debug::_Safe_sequence_base
00050     {
00051       typedef _GLIBCXX_STD::bitset<_Nb> _Base;
00052       typedef __gnu_debug::_Safe_sequence_base  _Safe_base;
00053 
00054     public:
00055       // bit reference:
00056       class reference
00057       : private _Base::reference, public __gnu_debug::_Safe_iterator_base
00058       {
00059     typedef typename _Base::reference _Base_ref;
00060 
00061     friend class bitset;
00062     reference();
00063 
00064     reference(const _Base_ref& __base, bitset* __seq)
00065     : _Base_ref(__base), _Safe_iterator_base(__seq, false)
00066     { }
00067 
00068       public:
00069     reference(const reference& __x)
00070     : _Base_ref(__x), _Safe_iterator_base(__x, false)
00071     { }
00072 
00073     reference&
00074     operator=(bool __x)
00075     {
00076       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
00077                   _M_message(__gnu_debug::__msg_bad_bitset_write)
00078                 ._M_iterator(*this));
00079       *static_cast<_Base_ref*>(this) = __x;
00080       return *this;
00081     }
00082 
00083     reference&
00084     operator=(const reference& __x)
00085     {
00086       _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
00087                    _M_message(__gnu_debug::__msg_bad_bitset_read)
00088                 ._M_iterator(__x));
00089       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
00090                   _M_message(__gnu_debug::__msg_bad_bitset_write)
00091                 ._M_iterator(*this));
00092       *static_cast<_Base_ref*>(this) = __x;
00093       return *this;
00094     }
00095 
00096     bool
00097     operator~() const
00098     {
00099       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
00100                    _M_message(__gnu_debug::__msg_bad_bitset_read)
00101                 ._M_iterator(*this));
00102       return ~(*static_cast<const _Base_ref*>(this));
00103     }
00104 
00105     operator bool() const
00106     {
00107       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
00108                   _M_message(__gnu_debug::__msg_bad_bitset_read)
00109                 ._M_iterator(*this));
00110       return *static_cast<const _Base_ref*>(this);
00111     }
00112 
00113     reference&
00114     flip()
00115     {
00116       _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
00117                   _M_message(__gnu_debug::__msg_bad_bitset_flip)
00118                 ._M_iterator(*this));
00119       _Base_ref::flip();
00120       return *this;
00121     }
00122       };
00123 
00124       // 23.3.5.1 constructors:
00125       bitset() : _Base() { }
00126 
00127       bitset(unsigned long __val) : _Base(__val) { }
00128 
00129       template<typename _CharT, typename _Traits, typename _Allocator>
00130         explicit
00131         bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str,
00132            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
00133            __pos = 0,
00134            typename std::basic_string<_CharT,_Traits,_Allocator>::size_type
00135            __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos))
00136     : _Base(__str, __pos, __n) { }
00137 
00138       bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
00139 
00140       // 23.3.5.2 bitset operations:
00141       bitset<_Nb>&
00142       operator&=(const bitset<_Nb>& __rhs)
00143       {
00144     _M_base() &= __rhs;
00145     return *this;
00146       }
00147 
00148       bitset<_Nb>&
00149       operator|=(const bitset<_Nb>& __rhs)
00150       {
00151     _M_base() |= __rhs;
00152     return *this;
00153       }
00154 
00155       bitset<_Nb>&
00156       operator^=(const bitset<_Nb>& __rhs)
00157       {
00158     _M_base() ^= __rhs;
00159     return *this;
00160       }
00161 
00162       bitset<_Nb>&
00163       operator<<=(size_t __pos)
00164       {
00165     _M_base() <<= __pos;
00166     return *this;
00167       }
00168 
00169       bitset<_Nb>&
00170       operator>>=(size_t __pos)
00171       {
00172     _M_base() >>= __pos;
00173     return *this;
00174       }
00175 
00176       bitset<_Nb>&
00177       set()
00178       {
00179     _Base::set();
00180     return *this;
00181       }
00182 
00183       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00184       // 186. bitset::set() second parameter should be bool
00185       bitset<_Nb>&
00186       set(size_t __pos, bool __val = true)
00187       {
00188     _Base::set(__pos, __val);
00189     return *this;
00190       }
00191 
00192       bitset<_Nb>&
00193       reset()
00194       {
00195     _Base::reset();
00196     return *this;
00197       }
00198 
00199       bitset<_Nb>&
00200       reset(size_t __pos)
00201       {
00202     _Base::reset(__pos);
00203     return *this;
00204       }
00205 
00206       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
00207 
00208       bitset<_Nb>&
00209       flip()
00210       {
00211     _Base::flip();
00212     return *this;
00213       }
00214 
00215       bitset<_Nb>&
00216       flip(size_t __pos)
00217       {
00218     _Base::flip(__pos);
00219     return *this;
00220       }
00221 
00222       // element access:
00223       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00224       // 11. Bitset minor problems
00225       reference
00226       operator[](size_t __pos)
00227       {
00228     __glibcxx_check_subscript(__pos);
00229     return reference(_M_base()[__pos], this);
00230       }
00231 
00232       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00233       // 11. Bitset minor problems
00234       bool
00235       operator[](size_t __pos) const
00236       {
00237     __glibcxx_check_subscript(__pos);
00238     return _M_base()[__pos];
00239       }
00240 
00241       using _Base::to_ulong;
00242 
00243       template <typename _CharT, typename _Traits, typename _Allocator>
00244         std::basic_string<_CharT, _Traits, _Allocator>
00245         to_string() const
00246         { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); }
00247 
00248       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00249       // 434. bitset::to_string() hard to use.
00250       template<typename _CharT, typename _Traits>
00251         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00252         to_string() const
00253         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
00254 
00255       template<typename _CharT>
00256         std::basic_string<_CharT, std::char_traits<_CharT>,
00257                           std::allocator<_CharT> >
00258         to_string() const
00259         {
00260           return to_string<_CharT, std::char_traits<_CharT>,
00261                            std::allocator<_CharT> >();
00262         }
00263 
00264       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00265         to_string() const
00266         {
00267           return to_string<char,std::char_traits<char>,std::allocator<char> >();
00268         }
00269 
00270       using _Base::count;
00271       using _Base::size;
00272 
00273       bool
00274       operator==(const bitset<_Nb>& __rhs) const
00275       { return _M_base() == __rhs; }
00276 
00277       bool
00278       operator!=(const bitset<_Nb>& __rhs) const
00279       { return _M_base() != __rhs; }
00280 
00281       using _Base::test;
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

Generated on Thu Nov 1 13:11:22 2007 for libstdc++ by  doxygen 1.5.1