1*38fd1498Szrj // Safe container 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_container.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_CONTAINER_H 30*38fd1498Szrj #define _GLIBCXX_DEBUG_SAFE_UNORDERED_CONTAINER_H 1 31*38fd1498Szrj 32*38fd1498Szrj #include <debug/assertions.h> 33*38fd1498Szrj #include <debug/macros.h> 34*38fd1498Szrj #include <debug/functions.h> 35*38fd1498Szrj #include <debug/safe_unordered_base.h> 36*38fd1498Szrj 37*38fd1498Szrj namespace __gnu_debug 38*38fd1498Szrj { 39*38fd1498Szrj /** 40*38fd1498Szrj * @brief Base class for constructing a @a safe unordered container type 41*38fd1498Szrj * that tracks iterators that reference it. 42*38fd1498Szrj * 43*38fd1498Szrj * The class template %_Safe_unordered_container simplifies the 44*38fd1498Szrj * construction of @a safe unordered containers that track the iterators 45*38fd1498Szrj * that reference the container, so that the iterators are notified of 46*38fd1498Szrj * changes in the container that may affect their operation, e.g., if 47*38fd1498Szrj * the container invalidates its iterators or is destructed. This class 48*38fd1498Szrj * template may only be used by deriving from it and passing the name 49*38fd1498Szrj * of the derived class as its template parameter via the curiously 50*38fd1498Szrj * recurring template pattern. The derived class must have @c 51*38fd1498Szrj * iterator and @c const_iterator types that are instantiations of 52*38fd1498Szrj * class template _Safe_iterator for this container and @c local_iterator 53*38fd1498Szrj * and @c const_local_iterator types that are instantiations of class 54*38fd1498Szrj * template _Safe_local_iterator for this container. Iterators will 55*38fd1498Szrj * then be tracked automatically. 56*38fd1498Szrj */ 57*38fd1498Szrj template<typename _Container> 58*38fd1498Szrj class _Safe_unordered_container : public _Safe_unordered_container_base 59*38fd1498Szrj { 60*38fd1498Szrj private: 61*38fd1498Szrj _Container& _M_cont()62*38fd1498Szrj _M_cont() noexcept 63*38fd1498Szrj { return *static_cast<_Container*>(this); } 64*38fd1498Szrj 65*38fd1498Szrj protected: 66*38fd1498Szrj void _M_invalidate_locals()67*38fd1498Szrj _M_invalidate_locals() 68*38fd1498Szrj { 69*38fd1498Szrj auto __local_end = _M_cont()._M_base().end(0); 70*38fd1498Szrj this->_M_invalidate_local_if( 71*38fd1498Szrj [__local_end](__decltype(_M_cont()._M_base().cend(0)) __it) 72*38fd1498Szrj { return __it != __local_end; }); 73*38fd1498Szrj } 74*38fd1498Szrj 75*38fd1498Szrj void _M_invalidate_all()76*38fd1498Szrj _M_invalidate_all() 77*38fd1498Szrj { 78*38fd1498Szrj auto __end = _M_cont()._M_base().end(); 79*38fd1498Szrj this->_M_invalidate_if( 80*38fd1498Szrj [__end](__decltype(_M_cont()._M_base().cend()) __it) 81*38fd1498Szrj { return __it != __end; }); 82*38fd1498Szrj _M_invalidate_locals(); 83*38fd1498Szrj } 84*38fd1498Szrj 85*38fd1498Szrj /** Invalidates all iterators @c x that reference this container, 86*38fd1498Szrj are not singular, and for which @c __pred(x) returns @c 87*38fd1498Szrj true. @c __pred will be invoked with the normal iterators nested 88*38fd1498Szrj in the safe ones. */ 89*38fd1498Szrj template<typename _Predicate> 90*38fd1498Szrj void 91*38fd1498Szrj _M_invalidate_if(_Predicate __pred); 92*38fd1498Szrj 93*38fd1498Szrj /** Invalidates all local iterators @c x that reference this container, 94*38fd1498Szrj are not singular, and for which @c __pred(x) returns @c 95*38fd1498Szrj true. @c __pred will be invoked with the normal ilocal iterators 96*38fd1498Szrj nested in the safe ones. */ 97*38fd1498Szrj template<typename _Predicate> 98*38fd1498Szrj void 99*38fd1498Szrj _M_invalidate_local_if(_Predicate __pred); 100*38fd1498Szrj }; 101*38fd1498Szrj } // namespace __gnu_debug 102*38fd1498Szrj 103*38fd1498Szrj #include <debug/safe_unordered_container.tcc> 104*38fd1498Szrj 105*38fd1498Szrj #endif 106