00001 // Safe sequence implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003, 2004, 2005 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 2, 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 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 00032 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1 00033 00034 #include <debug/debug.h> 00035 #include <debug/macros.h> 00036 #include <debug/functions.h> 00037 #include <debug/safe_base.h> 00038 00039 namespace __gnu_debug 00040 { 00041 template<typename _Iterator, typename _Sequence> 00042 class _Safe_iterator; 00043 00048 template<typename _Type> 00049 class _Not_equal_to 00050 { 00051 _Type __value; 00052 00053 public: 00054 explicit _Not_equal_to(const _Type& __v) : __value(__v) { } 00055 00056 bool 00057 operator()(const _Type& __x) const 00058 { return __value != __x; } 00059 }; 00060 00063 template<typename _Iterator> 00064 class _After_nth_from 00065 { 00066 typedef typename std::iterator_traits<_Iterator>::difference_type 00067 difference_type; 00068 00069 _Iterator _M_base; 00070 difference_type _M_n; 00071 00072 public: 00073 _After_nth_from(const difference_type& __n, const _Iterator& __base) 00074 : _M_base(__base), _M_n(__n) { } 00075 00076 bool 00077 operator()(const _Iterator& __x) const 00078 { return __x - _M_base >= _M_n; } 00079 }; 00080 00097 template<typename _Sequence> 00098 class _Safe_sequence : public _Safe_sequence_base 00099 { 00100 public: 00106 template<typename _Predicate> 00107 void 00108 _M_invalidate_if(_Predicate __pred); 00109 00113 template<typename _Iterator> 00114 void 00115 _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x); 00116 }; 00117 00118 template<typename _Sequence> 00119 template<typename _Predicate> 00120 void 00121 _Safe_sequence<_Sequence>:: 00122 _M_invalidate_if(_Predicate __pred) 00123 { 00124 typedef typename _Sequence::iterator iterator; 00125 typedef typename _Sequence::const_iterator const_iterator; 00126 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(); 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(); 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 for (_Safe_iterator_base* __iter = __from->_M_iterators; __iter; ) 00164 { 00165 iterator* __victim = static_cast<iterator*>(__iter); 00166 __iter = __iter->_M_next; 00167 if (!__victim->_M_singular() && __victim->base() == __x.base()) 00168 __victim->_M_attach(static_cast<_Sequence*>(this)); 00169 } 00170 00171 for (_Safe_iterator_base* __iter2 = __from->_M_const_iterators; 00172 __iter2;) 00173 { 00174 const_iterator* __victim = static_cast<const_iterator*>(__iter2); 00175 __iter2 = __iter2->_M_next; 00176 if (!__victim->_M_singular() && __victim->base() == __x.base()) 00177 __victim->_M_attach(static_cast<_Sequence*>(this)); 00178 } 00179 } 00180 } // namespace __gnu_debug 00181 00182 #endif