libstdc++
|
00001 // Safe container/iterator base implementation -*- C++ -*- 00002 00003 // Copyright (C) 2011-2015 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_unordered_base.h 00026 * This file is a GNU debug extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 00030 #define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1 00031 00032 #include <debug/safe_base.h> 00033 00034 namespace __gnu_debug 00035 { 00036 class _Safe_unordered_container_base; 00037 00038 /** \brief Basic functionality for a @a safe iterator. 00039 * 00040 * The %_Safe_local_iterator_base base class implements the functionality 00041 * of a safe local iterator that is not specific to a particular iterator 00042 * type. It contains a pointer back to the container it references 00043 * along with iterator version information and pointers to form a 00044 * doubly-linked list of local 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_local_iterator_base : public _Safe_iterator_base 00051 { 00052 protected: 00053 /** Initializes the iterator and makes it singular. */ 00054 _Safe_local_iterator_base() 00055 { } 00056 00057 /** Initialize the iterator to reference the container pointed to 00058 * by @p __seq. @p __constant is true when we are initializing a 00059 * constant local iterator, and false if it is a mutable local iterator. 00060 * Note that @p __seq may be NULL, in which case the iterator will be 00061 * singular. Otherwise, the iterator will reference @p __seq and 00062 * be nonsingular. 00063 */ 00064 _Safe_local_iterator_base(const _Safe_sequence_base* __seq, bool __constant) 00065 { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); } 00066 00067 /** Initializes the iterator to reference the same container that 00068 @p __x does. @p __constant is true if this is a constant 00069 iterator, and false if it is mutable. */ 00070 _Safe_local_iterator_base(const _Safe_local_iterator_base& __x, 00071 bool __constant) 00072 { this->_M_attach(__x._M_sequence, __constant); } 00073 00074 ~_Safe_local_iterator_base() { this->_M_detach(); } 00075 00076 _Safe_unordered_container_base* 00077 _M_get_container() const noexcept; 00078 00079 public: 00080 /** Attaches this iterator to the given container, detaching it 00081 * from whatever container it was attached to originally. If the 00082 * new container is the NULL pointer, the iterator is left 00083 * unattached. 00084 */ 00085 void _M_attach(_Safe_sequence_base* __seq, bool __constant); 00086 00087 /** Likewise, but not thread-safe. */ 00088 void _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw (); 00089 00090 /** Detach the iterator for whatever container it is attached to, 00091 * if any. 00092 */ 00093 void _M_detach(); 00094 00095 /** Likewise, but not thread-safe. */ 00096 void _M_detach_single() throw (); 00097 }; 00098 00099 /** 00100 * @brief Base class that supports tracking of local iterators that 00101 * reference an unordered container. 00102 * 00103 * The %_Safe_unordered_container_base class provides basic support for 00104 * tracking iterators into an unordered container. Containers that track 00105 * iterators must derived from %_Safe_unordered_container_base publicly, so 00106 * that safe iterators (which inherit _Safe_iterator_base) can 00107 * attach to them. This class contains four linked lists of 00108 * iterators, one for constant iterators, one for mutable 00109 * iterators, one for constant local iterators, one for mutable local 00110 * iterators and a version number that allows very fast 00111 * invalidation of all iterators that reference the container. 00112 * 00113 * This class must ensure that no operation on it may throw an 00114 * exception, otherwise @a safe containers may fail to provide the 00115 * exception-safety guarantees required by the C++ standard. 00116 */ 00117 class _Safe_unordered_container_base : public _Safe_sequence_base 00118 { 00119 typedef _Safe_sequence_base _Base; 00120 public: 00121 /// The list of mutable local iterators that reference this container 00122 _Safe_iterator_base* _M_local_iterators; 00123 00124 /// The list of constant local iterators that reference this container 00125 _Safe_iterator_base* _M_const_local_iterators; 00126 00127 protected: 00128 // Initialize with a version number of 1 and no iterators 00129 _Safe_unordered_container_base() noexcept 00130 : _M_local_iterators(nullptr), _M_const_local_iterators(nullptr) 00131 { } 00132 00133 // Copy constructor does not copy iterators. 00134 _Safe_unordered_container_base(const _Safe_unordered_container_base&) 00135 noexcept 00136 : _Safe_unordered_container_base() { } 00137 00138 // When moved unordered containers iterators are swapped. 00139 _Safe_unordered_container_base(_Safe_unordered_container_base&& __x) 00140 noexcept 00141 : _Safe_unordered_container_base() 00142 { this->_M_swap(__x); } 00143 00144 /** Notify all iterators that reference this container that the 00145 container is being destroyed. */ 00146 ~_Safe_unordered_container_base() noexcept 00147 { this->_M_detach_all(); } 00148 00149 /** Detach all iterators, leaving them singular. */ 00150 void 00151 _M_detach_all(); 00152 00153 /** Swap this container with the given container. This operation 00154 * also swaps ownership of the iterators, so that when the 00155 * operation is complete all iterators that originally referenced 00156 * one container now reference the other container. 00157 */ 00158 void 00159 _M_swap(_Safe_unordered_container_base& __x) noexcept; 00160 00161 public: 00162 /** Attach an iterator to this container. */ 00163 void 00164 _M_attach_local(_Safe_iterator_base* __it, bool __constant); 00165 00166 /** Likewise but not thread safe. */ 00167 void 00168 _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw (); 00169 00170 /** Detach an iterator from this container */ 00171 void 00172 _M_detach_local(_Safe_iterator_base* __it); 00173 00174 /** Likewise but not thread safe. */ 00175 void 00176 _M_detach_local_single(_Safe_iterator_base* __it) throw (); 00177 }; 00178 } // namespace __gnu_debug 00179 00180 #endif