libstdc++
|
00001 // Safe sequence implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003-2019 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_sequence.h 00026 * This file is a GNU debug extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 00030 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1 00031 00032 #include <debug/assertions.h> 00033 #include <debug/macros.h> 00034 #include <debug/functions.h> 00035 #include <debug/safe_base.h> 00036 00037 namespace __gnu_debug 00038 { 00039 /** A simple function object that returns true if the passed-in 00040 * value is not equal to the stored value. It saves typing over 00041 * using both bind1st and not_equal. 00042 */ 00043 template<typename _Type> 00044 class _Not_equal_to 00045 { 00046 _Type __value; 00047 00048 public: 00049 explicit _Not_equal_to(const _Type& __v) : __value(__v) { } 00050 00051 bool 00052 operator()(const _Type& __x) const 00053 { return __value != __x; } 00054 }; 00055 00056 /** A simple function object that returns true if the passed-in 00057 * value is equal to the stored value. */ 00058 template <typename _Type> 00059 class _Equal_to 00060 { 00061 _Type __value; 00062 00063 public: 00064 explicit _Equal_to(const _Type& __v) : __value(__v) { } 00065 00066 bool 00067 operator()(const _Type& __x) const 00068 { return __value == __x; } 00069 }; 00070 00071 /** A function object that returns true when the given random access 00072 iterator is at least @c n steps away from the given iterator. */ 00073 template<typename _Iterator> 00074 class _After_nth_from 00075 { 00076 typedef typename std::iterator_traits<_Iterator>::difference_type 00077 difference_type; 00078 00079 _Iterator _M_base; 00080 difference_type _M_n; 00081 00082 public: 00083 _After_nth_from(const difference_type& __n, const _Iterator& __base) 00084 : _M_base(__base), _M_n(__n) { } 00085 00086 bool 00087 operator()(const _Iterator& __x) const 00088 { return __x - _M_base >= _M_n; } 00089 }; 00090 00091 /** 00092 * @brief Base class for constructing a @a safe sequence type that 00093 * tracks iterators that reference it. 00094 * 00095 * The class template %_Safe_sequence simplifies the construction of 00096 * @a safe sequences that track the iterators that reference the 00097 * sequence, so that the iterators are notified of changes in the 00098 * sequence that may affect their operation, e.g., if the container 00099 * invalidates its iterators or is destructed. This class template 00100 * may only be used by deriving from it and passing the name of the 00101 * derived class as its template parameter via the curiously 00102 * recurring template pattern. The derived class must have @c 00103 * iterator and @c const_iterator types that are instantiations of 00104 * class template _Safe_iterator for this sequence. Iterators will 00105 * then be tracked automatically. 00106 */ 00107 template<typename _Sequence> 00108 class _Safe_sequence : public _Safe_sequence_base 00109 { 00110 public: 00111 /** Invalidates all iterators @c x that reference this sequence, 00112 are not singular, and for which @c __pred(x) returns @c 00113 true. @c __pred will be invoked with the normal iterators nested 00114 in the safe ones. */ 00115 template<typename _Predicate> 00116 void 00117 _M_invalidate_if(_Predicate __pred); 00118 00119 /** Transfers all iterators @c x that reference @c from sequence, 00120 are not singular, and for which @c __pred(x) returns @c 00121 true. @c __pred will be invoked with the normal iterators nested 00122 in the safe ones. */ 00123 template<typename _Predicate> 00124 void 00125 _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred); 00126 }; 00127 00128 /// Like _Safe_sequence but with a special _M_invalidate_all implementation 00129 /// not invalidating past-the-end iterators. Used by node based sequence. 00130 template<typename _Sequence> 00131 class _Safe_node_sequence 00132 : public _Safe_sequence<_Sequence> 00133 { 00134 protected: 00135 void 00136 _M_invalidate_all() 00137 { 00138 typedef typename _Sequence::const_iterator _Const_iterator; 00139 typedef typename _Const_iterator::iterator_type _Base_const_iterator; 00140 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; 00141 const _Sequence& __seq = *static_cast<_Sequence*>(this); 00142 this->_M_invalidate_if(_Not_equal(__seq._M_base().end())); 00143 } 00144 }; 00145 00146 } // namespace __gnu_debug 00147 00148 #include <debug/safe_sequence.tcc> 00149 00150 #endif