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