1*e4b17023SJohn Marino // Safe sequence implementation -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino // Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010, 2011 4*e4b17023SJohn Marino // Free Software Foundation, Inc. 5*e4b17023SJohn Marino // 6*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free 7*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the 8*e4b17023SJohn Marino // terms of the GNU General Public License as published by the 9*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option) 10*e4b17023SJohn Marino // any later version. 11*e4b17023SJohn Marino 12*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, 13*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of 14*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*e4b17023SJohn Marino // GNU General Public License for more details. 16*e4b17023SJohn Marino 17*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional 18*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version 19*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation. 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and 22*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program; 23*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>. 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino /** @file debug/safe_sequence.h 27*e4b17023SJohn Marino * This file is a GNU debug extension to the Standard C++ Library. 28*e4b17023SJohn Marino */ 29*e4b17023SJohn Marino 30*e4b17023SJohn Marino #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 31*e4b17023SJohn Marino #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1 32*e4b17023SJohn Marino 33*e4b17023SJohn Marino #include <debug/debug.h> 34*e4b17023SJohn Marino #include <debug/macros.h> 35*e4b17023SJohn Marino #include <debug/functions.h> 36*e4b17023SJohn Marino #include <debug/safe_base.h> 37*e4b17023SJohn Marino 38*e4b17023SJohn Marino namespace __gnu_debug 39*e4b17023SJohn Marino { 40*e4b17023SJohn Marino template<typename _Iterator, typename _Sequence> 41*e4b17023SJohn Marino class _Safe_iterator; 42*e4b17023SJohn Marino 43*e4b17023SJohn Marino /** A simple function object that returns true if the passed-in 44*e4b17023SJohn Marino * value is not equal to the stored value. It saves typing over 45*e4b17023SJohn Marino * using both bind1st and not_equal. 46*e4b17023SJohn Marino */ 47*e4b17023SJohn Marino template<typename _Type> 48*e4b17023SJohn Marino class _Not_equal_to 49*e4b17023SJohn Marino { 50*e4b17023SJohn Marino _Type __value; 51*e4b17023SJohn Marino 52*e4b17023SJohn Marino public: _Not_equal_to(const _Type & __v)53*e4b17023SJohn Marino explicit _Not_equal_to(const _Type& __v) : __value(__v) { } 54*e4b17023SJohn Marino 55*e4b17023SJohn Marino bool operator()56*e4b17023SJohn Marino operator()(const _Type& __x) const 57*e4b17023SJohn Marino { return __value != __x; } 58*e4b17023SJohn Marino }; 59*e4b17023SJohn Marino 60*e4b17023SJohn Marino /** A simple function object that returns true if the passed-in 61*e4b17023SJohn Marino * value is equal to the stored value. */ 62*e4b17023SJohn Marino template <typename _Type> 63*e4b17023SJohn Marino class _Equal_to 64*e4b17023SJohn Marino { 65*e4b17023SJohn Marino _Type __value; 66*e4b17023SJohn Marino 67*e4b17023SJohn Marino public: _Equal_to(const _Type & __v)68*e4b17023SJohn Marino explicit _Equal_to(const _Type& __v) : __value(__v) { } 69*e4b17023SJohn Marino 70*e4b17023SJohn Marino bool operator()71*e4b17023SJohn Marino operator()(const _Type& __x) const 72*e4b17023SJohn Marino { return __value == __x; } 73*e4b17023SJohn Marino }; 74*e4b17023SJohn Marino 75*e4b17023SJohn Marino /** A function object that returns true when the given random access 76*e4b17023SJohn Marino iterator is at least @c n steps away from the given iterator. */ 77*e4b17023SJohn Marino template<typename _Iterator> 78*e4b17023SJohn Marino class _After_nth_from 79*e4b17023SJohn Marino { 80*e4b17023SJohn Marino typedef typename std::iterator_traits<_Iterator>::difference_type 81*e4b17023SJohn Marino difference_type; 82*e4b17023SJohn Marino 83*e4b17023SJohn Marino _Iterator _M_base; 84*e4b17023SJohn Marino difference_type _M_n; 85*e4b17023SJohn Marino 86*e4b17023SJohn Marino public: _After_nth_from(const difference_type & __n,const _Iterator & __base)87*e4b17023SJohn Marino _After_nth_from(const difference_type& __n, const _Iterator& __base) 88*e4b17023SJohn Marino : _M_base(__base), _M_n(__n) { } 89*e4b17023SJohn Marino 90*e4b17023SJohn Marino bool operator()91*e4b17023SJohn Marino operator()(const _Iterator& __x) const 92*e4b17023SJohn Marino { return __x - _M_base >= _M_n; } 93*e4b17023SJohn Marino }; 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino /** 96*e4b17023SJohn Marino * @brief Base class for constructing a @a safe sequence type that 97*e4b17023SJohn Marino * tracks iterators that reference it. 98*e4b17023SJohn Marino * 99*e4b17023SJohn Marino * The class template %_Safe_sequence simplifies the construction of 100*e4b17023SJohn Marino * @a safe sequences that track the iterators that reference the 101*e4b17023SJohn Marino * sequence, so that the iterators are notified of changes in the 102*e4b17023SJohn Marino * sequence that may affect their operation, e.g., if the container 103*e4b17023SJohn Marino * invalidates its iterators or is destructed. This class template 104*e4b17023SJohn Marino * may only be used by deriving from it and passing the name of the 105*e4b17023SJohn Marino * derived class as its template parameter via the curiously 106*e4b17023SJohn Marino * recurring template pattern. The derived class must have @c 107*e4b17023SJohn Marino * iterator and @c const_iterator types that are instantiations of 108*e4b17023SJohn Marino * class template _Safe_iterator for this sequence. Iterators will 109*e4b17023SJohn Marino * then be tracked automatically. 110*e4b17023SJohn Marino */ 111*e4b17023SJohn Marino template<typename _Sequence> 112*e4b17023SJohn Marino class _Safe_sequence : public _Safe_sequence_base 113*e4b17023SJohn Marino { 114*e4b17023SJohn Marino public: 115*e4b17023SJohn Marino /** Invalidates all iterators @c x that reference this sequence, 116*e4b17023SJohn Marino are not singular, and for which @c __pred(x) returns @c 117*e4b17023SJohn Marino true. @c __pred will be invoked with the normal iterators nested 118*e4b17023SJohn Marino in the safe ones. */ 119*e4b17023SJohn Marino template<typename _Predicate> 120*e4b17023SJohn Marino void 121*e4b17023SJohn Marino _M_invalidate_if(_Predicate __pred); 122*e4b17023SJohn Marino 123*e4b17023SJohn Marino /** Transfers all iterators @c x that reference @c from sequence, 124*e4b17023SJohn Marino are not singular, and for which @c __pred(x) returns @c 125*e4b17023SJohn Marino true. @c __pred will be invoked with the normal iterators nested 126*e4b17023SJohn Marino in the safe ones. */ 127*e4b17023SJohn Marino template<typename _Predicate> 128*e4b17023SJohn Marino void 129*e4b17023SJohn Marino _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred); 130*e4b17023SJohn Marino }; 131*e4b17023SJohn Marino } // namespace __gnu_debug 132*e4b17023SJohn Marino 133*e4b17023SJohn Marino #include <debug/safe_sequence.tcc> 134*e4b17023SJohn Marino 135*e4b17023SJohn Marino #endif 136