1*38fd1498Szrj // Safe container/iterator base implementation -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2011-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file debug/safe_unordered_base.h 26*38fd1498Szrj * This file is a GNU debug extension to the Standard C++ Library. 27*38fd1498Szrj */ 28*38fd1498Szrj 29*38fd1498Szrj #ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 30*38fd1498Szrj #define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1 31*38fd1498Szrj 32*38fd1498Szrj #include <debug/safe_base.h> 33*38fd1498Szrj 34*38fd1498Szrj namespace __gnu_debug 35*38fd1498Szrj { 36*38fd1498Szrj class _Safe_unordered_container_base; 37*38fd1498Szrj 38*38fd1498Szrj /** \brief Basic functionality for a @a safe iterator. 39*38fd1498Szrj * 40*38fd1498Szrj * The %_Safe_local_iterator_base base class implements the functionality 41*38fd1498Szrj * of a safe local iterator that is not specific to a particular iterator 42*38fd1498Szrj * type. It contains a pointer back to the container it references 43*38fd1498Szrj * along with iterator version information and pointers to form a 44*38fd1498Szrj * doubly-linked list of local iterators referenced by the container. 45*38fd1498Szrj * 46*38fd1498Szrj * This class must not perform any operations that can throw an 47*38fd1498Szrj * exception, or the exception guarantees of derived iterators will 48*38fd1498Szrj * be broken. 49*38fd1498Szrj */ 50*38fd1498Szrj class _Safe_local_iterator_base : public _Safe_iterator_base 51*38fd1498Szrj { 52*38fd1498Szrj protected: 53*38fd1498Szrj /** Initializes the iterator and makes it singular. */ _Safe_local_iterator_base()54*38fd1498Szrj _Safe_local_iterator_base() 55*38fd1498Szrj { } 56*38fd1498Szrj 57*38fd1498Szrj /** Initialize the iterator to reference the container pointed to 58*38fd1498Szrj * by @p __seq. @p __constant is true when we are initializing a 59*38fd1498Szrj * constant local iterator, and false if it is a mutable local iterator. 60*38fd1498Szrj * Note that @p __seq may be NULL, in which case the iterator will be 61*38fd1498Szrj * singular. Otherwise, the iterator will reference @p __seq and 62*38fd1498Szrj * be nonsingular. 63*38fd1498Szrj */ _Safe_local_iterator_base(const _Safe_sequence_base * __seq,bool __constant)64*38fd1498Szrj _Safe_local_iterator_base(const _Safe_sequence_base* __seq, bool __constant) 65*38fd1498Szrj { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); } 66*38fd1498Szrj 67*38fd1498Szrj /** Initializes the iterator to reference the same container that 68*38fd1498Szrj @p __x does. @p __constant is true if this is a constant 69*38fd1498Szrj iterator, and false if it is mutable. */ _Safe_local_iterator_base(const _Safe_local_iterator_base & __x,bool __constant)70*38fd1498Szrj _Safe_local_iterator_base(const _Safe_local_iterator_base& __x, 71*38fd1498Szrj bool __constant) 72*38fd1498Szrj { this->_M_attach(__x._M_sequence, __constant); } 73*38fd1498Szrj ~_Safe_local_iterator_base()74*38fd1498Szrj ~_Safe_local_iterator_base() { this->_M_detach(); } 75*38fd1498Szrj 76*38fd1498Szrj _Safe_unordered_container_base* 77*38fd1498Szrj _M_get_container() const noexcept; 78*38fd1498Szrj 79*38fd1498Szrj /** Attaches this iterator to the given container, detaching it 80*38fd1498Szrj * from whatever container it was attached to originally. If the 81*38fd1498Szrj * new container is the NULL pointer, the iterator is left 82*38fd1498Szrj * unattached. 83*38fd1498Szrj */ 84*38fd1498Szrj void 85*38fd1498Szrj _M_attach(_Safe_sequence_base* __seq, bool __constant); 86*38fd1498Szrj 87*38fd1498Szrj /** Likewise, but not thread-safe. */ 88*38fd1498Szrj void 89*38fd1498Szrj _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw (); 90*38fd1498Szrj 91*38fd1498Szrj /** Detach the iterator for whatever container it is attached to, 92*38fd1498Szrj * if any. 93*38fd1498Szrj */ 94*38fd1498Szrj void 95*38fd1498Szrj _M_detach(); 96*38fd1498Szrj 97*38fd1498Szrj /** Likewise, but not thread-safe. */ 98*38fd1498Szrj void 99*38fd1498Szrj _M_detach_single() throw (); 100*38fd1498Szrj }; 101*38fd1498Szrj 102*38fd1498Szrj /** 103*38fd1498Szrj * @brief Base class that supports tracking of local iterators that 104*38fd1498Szrj * reference an unordered container. 105*38fd1498Szrj * 106*38fd1498Szrj * The %_Safe_unordered_container_base class provides basic support for 107*38fd1498Szrj * tracking iterators into an unordered container. Containers that track 108*38fd1498Szrj * iterators must derived from %_Safe_unordered_container_base publicly, so 109*38fd1498Szrj * that safe iterators (which inherit _Safe_iterator_base) can 110*38fd1498Szrj * attach to them. This class contains four linked lists of 111*38fd1498Szrj * iterators, one for constant iterators, one for mutable 112*38fd1498Szrj * iterators, one for constant local iterators, one for mutable local 113*38fd1498Szrj * iterators and a version number that allows very fast 114*38fd1498Szrj * invalidation of all iterators that reference the container. 115*38fd1498Szrj * 116*38fd1498Szrj * This class must ensure that no operation on it may throw an 117*38fd1498Szrj * exception, otherwise @a safe containers may fail to provide the 118*38fd1498Szrj * exception-safety guarantees required by the C++ standard. 119*38fd1498Szrj */ 120*38fd1498Szrj class _Safe_unordered_container_base : public _Safe_sequence_base 121*38fd1498Szrj { 122*38fd1498Szrj friend class _Safe_local_iterator_base; 123*38fd1498Szrj typedef _Safe_sequence_base _Base; 124*38fd1498Szrj 125*38fd1498Szrj public: 126*38fd1498Szrj /// The list of mutable local iterators that reference this container 127*38fd1498Szrj _Safe_iterator_base* _M_local_iterators; 128*38fd1498Szrj 129*38fd1498Szrj /// The list of constant local iterators that reference this container 130*38fd1498Szrj _Safe_iterator_base* _M_const_local_iterators; 131*38fd1498Szrj 132*38fd1498Szrj protected: 133*38fd1498Szrj // Initialize with a version number of 1 and no iterators _Safe_unordered_container_base()134*38fd1498Szrj _Safe_unordered_container_base() noexcept 135*38fd1498Szrj : _M_local_iterators(nullptr), _M_const_local_iterators(nullptr) 136*38fd1498Szrj { } 137*38fd1498Szrj 138*38fd1498Szrj // Copy constructor does not copy iterators. _Safe_unordered_container_base(const _Safe_unordered_container_base &)139*38fd1498Szrj _Safe_unordered_container_base(const _Safe_unordered_container_base&) 140*38fd1498Szrj noexcept 141*38fd1498Szrj : _Safe_unordered_container_base() { } 142*38fd1498Szrj 143*38fd1498Szrj // When moved unordered containers iterators are swapped. _Safe_unordered_container_base(_Safe_unordered_container_base && __x)144*38fd1498Szrj _Safe_unordered_container_base(_Safe_unordered_container_base&& __x) 145*38fd1498Szrj noexcept 146*38fd1498Szrj : _Safe_unordered_container_base() 147*38fd1498Szrj { this->_M_swap(__x); } 148*38fd1498Szrj 149*38fd1498Szrj /** Notify all iterators that reference this container that the 150*38fd1498Szrj container is being destroyed. */ ~_Safe_unordered_container_base()151*38fd1498Szrj ~_Safe_unordered_container_base() noexcept 152*38fd1498Szrj { this->_M_detach_all(); } 153*38fd1498Szrj 154*38fd1498Szrj /** Detach all iterators, leaving them singular. */ 155*38fd1498Szrj void 156*38fd1498Szrj _M_detach_all(); 157*38fd1498Szrj 158*38fd1498Szrj /** Swap this container with the given container. This operation 159*38fd1498Szrj * also swaps ownership of the iterators, so that when the 160*38fd1498Szrj * operation is complete all iterators that originally referenced 161*38fd1498Szrj * one container now reference the other container. 162*38fd1498Szrj */ 163*38fd1498Szrj void 164*38fd1498Szrj _M_swap(_Safe_unordered_container_base& __x) noexcept; 165*38fd1498Szrj 166*38fd1498Szrj private: 167*38fd1498Szrj /** Attach an iterator to this container. */ 168*38fd1498Szrj void 169*38fd1498Szrj _M_attach_local(_Safe_iterator_base* __it, bool __constant); 170*38fd1498Szrj 171*38fd1498Szrj /** Likewise but not thread safe. */ 172*38fd1498Szrj void 173*38fd1498Szrj _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw (); 174*38fd1498Szrj 175*38fd1498Szrj /** Detach an iterator from this container */ 176*38fd1498Szrj void 177*38fd1498Szrj _M_detach_local(_Safe_iterator_base* __it); 178*38fd1498Szrj 179*38fd1498Szrj /** Likewise but not thread safe. */ 180*38fd1498Szrj void 181*38fd1498Szrj _M_detach_local_single(_Safe_iterator_base* __it) throw (); 182*38fd1498Szrj }; 183*38fd1498Szrj } // namespace __gnu_debug 184*38fd1498Szrj 185*38fd1498Szrj #endif 186