libstdc++
|
00001 // Safe sequence implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003, 2004, 2005, 2006, 2009 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /** @file debug/safe_sequence.h 00027 * This file is a GNU debug extension to the Standard C++ Library. 00028 */ 00029 00030 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 00031 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1 00032 00033 #include <debug/debug.h> 00034 #include <debug/macros.h> 00035 #include <debug/functions.h> 00036 #include <debug/safe_base.h> 00037 00038 namespace __gnu_debug 00039 { 00040 template<typename _Iterator, typename _Sequence> 00041 class _Safe_iterator; 00042 00043 /** A simple function object that returns true if the passed-in 00044 * value is not equal to the stored value. It saves typing over 00045 * using both bind1st and not_equal. 00046 */ 00047 template<typename _Type> 00048 class _Not_equal_to 00049 { 00050 _Type __value; 00051 00052 public: 00053 explicit _Not_equal_to(const _Type& __v) : __value(__v) { } 00054 00055 bool 00056 operator()(const _Type& __x) const 00057 { return __value != __x; } 00058 }; 00059 00060 /** A function object that returns true when the given random access 00061 iterator is at least @c n steps away from the given iterator. */ 00062 template<typename _Iterator> 00063 class _After_nth_from 00064 { 00065 typedef typename std::iterator_traits<_Iterator>::difference_type 00066 difference_type; 00067 00068 _Iterator _M_base; 00069 difference_type _M_n; 00070 00071 public: 00072 _After_nth_from(const difference_type& __n, const _Iterator& __base) 00073 : _M_base(__base), _M_n(__n) { } 00074 00075 bool 00076 operator()(const _Iterator& __x) const 00077 { return __x - _M_base >= _M_n; } 00078 }; 00079 00080 /** 00081 * @brief Base class for constructing a "safe" sequence type that 00082 * tracks iterators that reference it. 00083 * 00084 * The class template %_Safe_sequence simplifies the construction of 00085 * "safe" sequences that track the iterators that reference the 00086 * sequence, so that the iterators are notified of changes in the 00087 * sequence that may affect their operation, e.g., if the container 00088 * invalidates its iterators or is destructed. This class template 00089 * may only be used by deriving from it and passing the name of the 00090 * derived class as its template parameter via the curiously 00091 * recurring template pattern. The derived class must have @c 00092 * iterator and @const_iterator types that are instantiations of 00093 * class template _Safe_iterator for this sequence. Iterators will 00094 * then be tracked automatically. 00095 */ 00096 template<typename _Sequence> 00097 class _Safe_sequence : public _Safe_sequence_base 00098 { 00099 public: 00100 /** Invalidates all iterators @c x that reference this sequence, 00101 are not singular, and for which @c pred(x) returns @c 00102 true. The user of this routine should be careful not to make 00103 copies of the iterators passed to @p pred, as the copies may 00104 interfere with the invalidation. */ 00105 template<typename _Predicate> 00106 void 00107 _M_invalidate_if(_Predicate __pred); 00108 00109 /** Transfers all iterators that reference this memory location 00110 to this sequence from whatever sequence they are attached 00111 to. */ 00112 template<typename _Iterator> 00113 void 00114 _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x); 00115 }; 00116 00117 template<typename _Sequence> 00118 template<typename _Predicate> 00119 void 00120 _Safe_sequence<_Sequence>:: 00121 _M_invalidate_if(_Predicate __pred) 00122 { 00123 typedef typename _Sequence::iterator iterator; 00124 typedef typename _Sequence::const_iterator const_iterator; 00125 00126 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex()); 00127 for (_Safe_iterator_base* __iter = _M_iterators; __iter;) 00128 { 00129 iterator* __victim = static_cast<iterator*>(__iter); 00130 __iter = __iter->_M_next; 00131 if (!__victim->_M_singular()) 00132 { 00133 if (__pred(__victim->base())) 00134 __victim->_M_invalidate_single(); 00135 } 00136 } 00137 00138 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;) 00139 { 00140 const_iterator* __victim = static_cast<const_iterator*>(__iter2); 00141 __iter2 = __iter2->_M_next; 00142 if (!__victim->_M_singular()) 00143 { 00144 if (__pred(__victim->base())) 00145 __victim->_M_invalidate_single(); 00146 } 00147 } 00148 } 00149 00150 template<typename _Sequence> 00151 template<typename _Iterator> 00152 void 00153 _Safe_sequence<_Sequence>:: 00154 _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x) 00155 { 00156 _Safe_sequence_base* __from = __x._M_sequence; 00157 if (!__from) 00158 return; 00159 00160 typedef typename _Sequence::iterator iterator; 00161 typedef typename _Sequence::const_iterator const_iterator; 00162 00163 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex()); 00164 for (_Safe_iterator_base* __iter = __from->_M_iterators; __iter;) 00165 { 00166 iterator* __victim = static_cast<iterator*>(__iter); 00167 __iter = __iter->_M_next; 00168 if (!__victim->_M_singular() && __victim->base() == __x.base()) 00169 __victim->_M_attach_single(static_cast<_Sequence*>(this)); 00170 } 00171 00172 for (_Safe_iterator_base* __iter2 = __from->_M_const_iterators; 00173 __iter2;) 00174 { 00175 const_iterator* __victim = static_cast<const_iterator*>(__iter2); 00176 __iter2 = __iter2->_M_next; 00177 if (!__victim->_M_singular() && __victim->base() == __x.base()) 00178 __victim->_M_attach_single(static_cast<_Sequence*>(this)); 00179 } 00180 } 00181 } // namespace __gnu_debug 00182 00183 #endif