| 1 | // Debugging bitset implementation -*- C++ -*- |
|---|
| 2 | |
|---|
| 3 | // Copyright (C) 2003, 2004 |
|---|
| 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|---|
| 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 | #ifndef _GLIBCXX_DEBUG_BITSET |
|---|
| 32 | #define _GLIBCXX_DEBUG_BITSET |
|---|
| 33 | |
|---|
| 34 | #include <bitset> |
|---|
| 35 | #include <debug/safe_sequence.h> |
|---|
| 36 | #include <debug/safe_iterator.h> |
|---|
| 37 | |
|---|
| 38 | namespace __gnu_debug_def |
|---|
| 39 | { |
|---|
| 40 | template<size_t _Nb> |
|---|
| 41 | class bitset |
|---|
| 42 | : public _GLIBCXX_STD::bitset<_Nb>, |
|---|
| 43 | public __gnu_debug::_Safe_sequence_base |
|---|
| 44 | { |
|---|
| 45 | typedef _GLIBCXX_STD::bitset<_Nb> _Base; |
|---|
| 46 | typedef __gnu_debug::_Safe_sequence_base _Safe_base; |
|---|
| 47 | |
|---|
| 48 | public: |
|---|
| 49 | // bit reference: |
|---|
| 50 | class reference |
|---|
| 51 | : private _Base::reference, public __gnu_debug::_Safe_iterator_base |
|---|
| 52 | { |
|---|
| 53 | typedef typename _Base::reference _Base_ref; |
|---|
| 54 | |
|---|
| 55 | friend class bitset; |
|---|
| 56 | reference(); |
|---|
| 57 | |
|---|
| 58 | reference(const _Base_ref& __base, bitset* __seq) |
|---|
| 59 | : _Base_ref(__base), _Safe_iterator_base(__seq, false) |
|---|
| 60 | { } |
|---|
| 61 | |
|---|
| 62 | public: |
|---|
| 63 | reference(const reference& __x) |
|---|
| 64 | : _Base_ref(__x), _Safe_iterator_base(__x, false) |
|---|
| 65 | { } |
|---|
| 66 | |
|---|
| 67 | reference& |
|---|
| 68 | operator=(bool __x) |
|---|
| 69 | { |
|---|
| 70 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), |
|---|
| 71 | _M_message(::__gnu_debug::__msg_bad_bitset_write) |
|---|
| 72 | ._M_iterator(*this)); |
|---|
| 73 | *static_cast<_Base_ref*>(this) = __x; |
|---|
| 74 | return *this; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | reference& |
|---|
| 78 | operator=(const reference& __x) |
|---|
| 79 | { |
|---|
| 80 | _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(), |
|---|
| 81 | _M_message(::__gnu_debug::__msg_bad_bitset_read) |
|---|
| 82 | ._M_iterator(__x)); |
|---|
| 83 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), |
|---|
| 84 | _M_message(::__gnu_debug::__msg_bad_bitset_write) |
|---|
| 85 | ._M_iterator(*this)); |
|---|
| 86 | *static_cast<_Base_ref*>(this) = __x; |
|---|
| 87 | return *this; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | bool |
|---|
| 91 | operator~() const |
|---|
| 92 | { |
|---|
| 93 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), |
|---|
| 94 | _M_message(::__gnu_debug::__msg_bad_bitset_read) |
|---|
| 95 | ._M_iterator(*this)); |
|---|
| 96 | return ~(*static_cast<const _Base_ref*>(this)); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | operator bool() const |
|---|
| 100 | { |
|---|
| 101 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), |
|---|
| 102 | _M_message(::__gnu_debug::__msg_bad_bitset_read) |
|---|
| 103 | ._M_iterator(*this)); |
|---|
| 104 | return *static_cast<const _Base_ref*>(this); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | reference& |
|---|
| 108 | flip() |
|---|
| 109 | { |
|---|
| 110 | _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(), |
|---|
| 111 | _M_message(::__gnu_debug::__msg_bad_bitset_flip) |
|---|
| 112 | ._M_iterator(*this)); |
|---|
| 113 | _Base_ref::flip(); |
|---|
| 114 | return *this; |
|---|
| 115 | } |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | // 23.3.5.1 constructors: |
|---|
| 119 | bitset() : _Base() { } |
|---|
| 120 | |
|---|
| 121 | bitset(unsigned long __val) : _Base(__val) { } |
|---|
| 122 | |
|---|
| 123 | template<typename _CharT, typename _Traits, typename _Allocator> |
|---|
| 124 | explicit |
|---|
| 125 | bitset(const std::basic_string<_CharT,_Traits,_Allocator>& __str, |
|---|
| 126 | typename std::basic_string<_CharT,_Traits,_Allocator>::size_type |
|---|
| 127 | __pos = 0, |
|---|
| 128 | typename std::basic_string<_CharT,_Traits,_Allocator>::size_type |
|---|
| 129 | __n = (std::basic_string<_CharT,_Traits,_Allocator>::npos)) |
|---|
| 130 | : _Base(__str, __pos, __n) { } |
|---|
| 131 | |
|---|
| 132 | bitset(const _Base& __x) : _Base(__x), _Safe_base() { } |
|---|
| 133 | |
|---|
| 134 | // 23.3.5.2 bitset operations: |
|---|
| 135 | bitset<_Nb>& |
|---|
| 136 | operator&=(const bitset<_Nb>& __rhs) |
|---|
| 137 | { |
|---|
| 138 | _M_base() &= __rhs; |
|---|
| 139 | return *this; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | bitset<_Nb>& |
|---|
| 143 | operator|=(const bitset<_Nb>& __rhs) |
|---|
| 144 | { |
|---|
| 145 | _M_base() |= __rhs; |
|---|
| 146 | return *this; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | bitset<_Nb>& |
|---|
| 150 | operator^=(const bitset<_Nb>& __rhs) |
|---|
| 151 | { |
|---|
| 152 | _M_base() ^= __rhs; |
|---|
| 153 | return *this; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | bitset<_Nb>& |
|---|
| 157 | operator<<=(size_t __pos) |
|---|
| 158 | { |
|---|
| 159 | _M_base() <<= __pos; |
|---|
| 160 | return *this; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | bitset<_Nb>& |
|---|
| 164 | operator>>=(size_t __pos) |
|---|
| 165 | { |
|---|
| 166 | _M_base() >>= __pos; |
|---|
| 167 | return *this; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | bitset<_Nb>& |
|---|
| 171 | set() |
|---|
| 172 | { |
|---|
| 173 | _Base::set(); |
|---|
| 174 | return *this; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
|---|
| 178 | // 186. bitset::set() second parameter should be bool |
|---|
| 179 | bitset<_Nb>& |
|---|
| 180 | set(size_t __pos, bool __val = true) |
|---|
| 181 | { |
|---|
| 182 | _Base::set(__pos, __val); |
|---|
| 183 | return *this; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | bitset<_Nb>& |
|---|
| 187 | reset() |
|---|
| 188 | { |
|---|
| 189 | _Base::reset(); |
|---|
| 190 | return *this; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | bitset<_Nb>& |
|---|
| 194 | reset(size_t __pos) |
|---|
| 195 | { |
|---|
| 196 | _Base::reset(__pos); |
|---|
| 197 | return *this; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | bitset<_Nb> operator~() const { return bitset(~_M_base()); } |
|---|
| 201 | |
|---|
| 202 | bitset<_Nb>& |
|---|
| 203 | flip() |
|---|
| 204 | { |
|---|
| 205 | _Base::flip(); |
|---|
| 206 | return *this; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | bitset<_Nb>& |
|---|
| 210 | flip(size_t __pos) |
|---|
| 211 | { |
|---|
| 212 | _Base::flip(__pos); |
|---|
| 213 | return *this; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // element access: |
|---|
| 217 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
|---|
| 218 | // 11. Bitset minor problems |
|---|
| 219 | reference |
|---|
| 220 | operator[](size_t __pos) |
|---|
| 221 | { |
|---|
| 222 | __glibcxx_check_subscript(__pos); |
|---|
| 223 | return reference(_M_base()[__pos], this); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
|---|
| 227 | // 11. Bitset minor problems |
|---|
| 228 | bool |
|---|
| 229 | operator[](size_t __pos) const |
|---|
| 230 | { |
|---|
| 231 | __glibcxx_check_subscript(__pos); |
|---|
| 232 | return _M_base()[__pos]; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | using _Base::to_ulong; |
|---|
| 236 | |
|---|
| 237 | template <typename _CharT, typename _Traits, typename _Allocator> |
|---|
| 238 | std::basic_string<_CharT, _Traits, _Allocator> |
|---|
| 239 | to_string() const |
|---|
| 240 | { return _M_base().template to_string<_CharT, _Traits, _Allocator>(); } |
|---|
| 241 | |
|---|
| 242 | using _Base::count; |
|---|
| 243 | using _Base::size; |
|---|
| 244 | |
|---|
| 245 | bool |
|---|
| 246 | operator==(const bitset<_Nb>& __rhs) const |
|---|
| 247 | { return _M_base() == __rhs; } |
|---|
| 248 | |
|---|
| 249 | bool |
|---|
| 250 | operator!=(const bitset<_Nb>& __rhs) const |
|---|
| 251 | { return _M_base() != __rhs; } |
|---|
| 252 | |
|---|
| 253 | using _Base::test; |
|---|
| 254 | using _Base::any; |
|---|
| 255 | using _Base::none; |
|---|
| 256 | |
|---|
| 257 | bitset<_Nb> |
|---|
| 258 | operator<<(size_t __pos) const |
|---|
| 259 | { return bitset<_Nb>(_M_base() << __pos); } |
|---|
| 260 | |
|---|
| 261 | bitset<_Nb> |
|---|
| 262 | operator>>(size_t __pos) const |
|---|
| 263 | { return bitset<_Nb>(_M_base() >> __pos); } |
|---|
| 264 | |
|---|
| 265 | _Base& |
|---|
| 266 | _M_base() { return *this; } |
|---|
| 267 | |
|---|
| 268 | const _Base& |
|---|
| 269 | _M_base() const { return *this; } |
|---|
| 270 | }; |
|---|
| 271 | |
|---|
| 272 | template<size_t _Nb> |
|---|
| 273 | bitset<_Nb> |
|---|
| 274 | operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) |
|---|
| 275 | { return bitset<_Nb>(__x) &= __y; } |
|---|
| 276 | |
|---|
| 277 | template<size_t _Nb> |
|---|
| 278 | bitset<_Nb> |
|---|
| 279 | operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) |
|---|
| 280 | { return bitset<_Nb>(__x) |= __y; } |
|---|
| 281 | |
|---|
| 282 | template<size_t _Nb> |
|---|
| 283 | bitset<_Nb> |
|---|
| 284 | operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) |
|---|
| 285 | { return bitset<_Nb>(__x) ^= __y; } |
|---|
| 286 | |
|---|
| 287 | template<typename _CharT, typename _Traits, size_t _Nb> |
|---|
| 288 | std::basic_istream<_CharT, _Traits>& |
|---|
| 289 | operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) |
|---|
| 290 | { return __is >> __x._M_base(); } |
|---|
| 291 | |
|---|
| 292 | template<typename _CharT, typename _Traits, size_t _Nb> |
|---|
| 293 | std::basic_ostream<_CharT, _Traits>& |
|---|
| 294 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
|---|
| 295 | const bitset<_Nb>& __x) |
|---|
| 296 | { return __os << __x._M_base(); } |
|---|
| 297 | } // namespace __gnu_debug_def |
|---|
| 298 | |
|---|
| 299 | #endif |
|---|