libstdc++
|
00001 // Safe sequence/iterator base implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003-2016 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file debug/safe_base.h 00026 * This file is a GNU debug extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H 00030 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1 00031 00032 #include <ext/concurrence.h> 00033 00034 namespace __gnu_debug 00035 { 00036 class _Safe_sequence_base; 00037 00038 /** \brief Basic functionality for a @a safe iterator. 00039 * 00040 * The %_Safe_iterator_base base class implements the functionality 00041 * of a safe iterator that is not specific to a particular iterator 00042 * type. It contains a pointer back to the sequence it references 00043 * along with iterator version information and pointers to form a 00044 * doubly-linked list of iterators referenced by the container. 00045 * 00046 * This class must not perform any operations that can throw an 00047 * exception, or the exception guarantees of derived iterators will 00048 * be broken. 00049 */ 00050 class _Safe_iterator_base 00051 { 00052 public: 00053 /** The sequence this iterator references; may be NULL to indicate 00054 a singular iterator. */ 00055 _Safe_sequence_base* _M_sequence; 00056 00057 /** The version number of this iterator. The sentinel value 0 is 00058 * used to indicate an invalidated iterator (i.e., one that is 00059 * singular because of an operation on the container). This 00060 * version number must equal the version number in the sequence 00061 * referenced by _M_sequence for the iterator to be 00062 * non-singular. 00063 */ 00064 unsigned int _M_version; 00065 00066 /** Pointer to the previous iterator in the sequence's list of 00067 iterators. Only valid when _M_sequence != NULL. */ 00068 _Safe_iterator_base* _M_prior; 00069 00070 /** Pointer to the next iterator in the sequence's list of 00071 iterators. Only valid when _M_sequence != NULL. */ 00072 _Safe_iterator_base* _M_next; 00073 00074 protected: 00075 /** Initializes the iterator and makes it singular. */ 00076 _Safe_iterator_base() 00077 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) 00078 { } 00079 00080 /** Initialize the iterator to reference the sequence pointed to 00081 * by @p __seq. @p __constant is true when we are initializing a 00082 * constant iterator, and false if it is a mutable iterator. Note 00083 * that @p __seq may be NULL, in which case the iterator will be 00084 * singular. Otherwise, the iterator will reference @p __seq and 00085 * be nonsingular. 00086 */ 00087 _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant) 00088 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) 00089 { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); } 00090 00091 /** Initializes the iterator to reference the same sequence that 00092 @p __x does. @p __constant is true if this is a constant 00093 iterator, and false if it is mutable. */ 00094 _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant) 00095 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) 00096 { this->_M_attach(__x._M_sequence, __constant); } 00097 00098 ~_Safe_iterator_base() { this->_M_detach(); } 00099 00100 /** For use in _Safe_iterator. */ 00101 __gnu_cxx::__mutex& 00102 _M_get_mutex() throw (); 00103 00104 public: 00105 /** Attaches this iterator to the given sequence, detaching it 00106 * from whatever sequence it was attached to originally. If the 00107 * new sequence is the NULL pointer, the iterator is left 00108 * unattached. 00109 */ 00110 void 00111 _M_attach(_Safe_sequence_base* __seq, bool __constant); 00112 00113 /** Likewise, but not thread-safe. */ 00114 void 00115 _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw (); 00116 00117 /** Detach the iterator for whatever sequence it is attached to, 00118 * if any. 00119 */ 00120 void 00121 _M_detach(); 00122 00123 /** Likewise, but not thread-safe. */ 00124 void 00125 _M_detach_single() throw (); 00126 00127 /** Determines if we are attached to the given sequence. */ 00128 bool 00129 _M_attached_to(const _Safe_sequence_base* __seq) const 00130 { return _M_sequence == __seq; } 00131 00132 /** Is this iterator singular? */ 00133 _GLIBCXX_PURE bool 00134 _M_singular() const throw (); 00135 00136 /** Can we compare this iterator to the given iterator @p __x? 00137 Returns true if both iterators are nonsingular and reference 00138 the same sequence. */ 00139 _GLIBCXX_PURE bool 00140 _M_can_compare(const _Safe_iterator_base& __x) const throw (); 00141 00142 /** Invalidate the iterator, making it singular. */ 00143 void 00144 _M_invalidate() 00145 { _M_version = 0; } 00146 00147 /** Reset all member variables */ 00148 void 00149 _M_reset() throw (); 00150 00151 /** Unlink itself */ 00152 void 00153 _M_unlink() throw () 00154 { 00155 if (_M_prior) 00156 _M_prior->_M_next = _M_next; 00157 if (_M_next) 00158 _M_next->_M_prior = _M_prior; 00159 } 00160 }; 00161 00162 /** Iterators that derive from _Safe_iterator_base can be determined singular 00163 * or non-singular. 00164 **/ 00165 inline bool 00166 __check_singular_aux(const _Safe_iterator_base* __x) 00167 { return __x->_M_singular(); } 00168 00169 /** 00170 * @brief Base class that supports tracking of iterators that 00171 * reference a sequence. 00172 * 00173 * The %_Safe_sequence_base class provides basic support for 00174 * tracking iterators into a sequence. Sequences that track 00175 * iterators must derived from %_Safe_sequence_base publicly, so 00176 * that safe iterators (which inherit _Safe_iterator_base) can 00177 * attach to them. This class contains two linked lists of 00178 * iterators, one for constant iterators and one for mutable 00179 * iterators, and a version number that allows very fast 00180 * invalidation of all iterators that reference the container. 00181 * 00182 * This class must ensure that no operation on it may throw an 00183 * exception, otherwise @a safe sequences may fail to provide the 00184 * exception-safety guarantees required by the C++ standard. 00185 */ 00186 class _Safe_sequence_base 00187 { 00188 public: 00189 /// The list of mutable iterators that reference this container 00190 _Safe_iterator_base* _M_iterators; 00191 00192 /// The list of constant iterators that reference this container 00193 _Safe_iterator_base* _M_const_iterators; 00194 00195 /// The container version number. This number may never be 0. 00196 mutable unsigned int _M_version; 00197 00198 protected: 00199 // Initialize with a version number of 1 and no iterators 00200 _Safe_sequence_base() _GLIBCXX_NOEXCEPT 00201 : _M_iterators(0), _M_const_iterators(0), _M_version(1) 00202 { } 00203 00204 #if __cplusplus >= 201103L 00205 _Safe_sequence_base(const _Safe_sequence_base&) noexcept 00206 : _Safe_sequence_base() { } 00207 #endif 00208 00209 /** Notify all iterators that reference this sequence that the 00210 sequence is being destroyed. */ 00211 ~_Safe_sequence_base() 00212 { this->_M_detach_all(); } 00213 00214 /** Detach all iterators, leaving them singular. */ 00215 void 00216 _M_detach_all(); 00217 00218 /** Detach all singular iterators. 00219 * @post for all iterators i attached to this sequence, 00220 * i->_M_version == _M_version. 00221 */ 00222 void 00223 _M_detach_singular(); 00224 00225 /** Revalidates all attached singular iterators. This method may 00226 * be used to validate iterators that were invalidated before 00227 * (but for some reason, such as an exception, need to become 00228 * valid again). 00229 */ 00230 void 00231 _M_revalidate_singular(); 00232 00233 /** Swap this sequence with the given sequence. This operation 00234 * also swaps ownership of the iterators, so that when the 00235 * operation is complete all iterators that originally referenced 00236 * one container now reference the other container. 00237 */ 00238 void 00239 _M_swap(_Safe_sequence_base& __x) _GLIBCXX_USE_NOEXCEPT; 00240 00241 /** For use in _Safe_sequence. */ 00242 __gnu_cxx::__mutex& 00243 _M_get_mutex() throw (); 00244 00245 public: 00246 /** Invalidates all iterators. */ 00247 void 00248 _M_invalidate_all() const 00249 { if (++_M_version == 0) _M_version = 1; } 00250 00251 /** Attach an iterator to this sequence. */ 00252 void 00253 _M_attach(_Safe_iterator_base* __it, bool __constant); 00254 00255 /** Likewise but not thread safe. */ 00256 void 00257 _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw (); 00258 00259 /** Detach an iterator from this sequence */ 00260 void 00261 _M_detach(_Safe_iterator_base* __it); 00262 00263 /** Likewise but not thread safe. */ 00264 void 00265 _M_detach_single(_Safe_iterator_base* __it) throw (); 00266 }; 00267 } // namespace __gnu_debug 00268 00269 #endif