safe_sequence.h

Go to the documentation of this file.
00001 // Safe sequence implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006
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 /** @file debug/safe_sequence.h
00032  *  This file is a GNU debug extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
00036 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
00037 
00038 #include <debug/debug.h>
00039 #include <debug/macros.h>
00040 #include <debug/functions.h>
00041 #include <debug/safe_base.h>
00042 
00043 namespace __gnu_debug
00044 {
00045   template<typename _Iterator, typename _Sequence>
00046     class _Safe_iterator;
00047 
00048   /** A simple function object that returns true if the passed-in
00049    *  value is not equal to the stored value. It saves typing over
00050    *  using both bind1st and not_equal.
00051    */
00052   template<typename _Type>
00053     class _Not_equal_to
00054     {
00055       _Type __value;
00056 
00057     public:
00058       explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
00059 
00060       bool
00061       operator()(const _Type& __x) const
00062       { return __value != __x; }
00063     };
00064 
00065   /** A function object that returns true when the given random access
00066       iterator is at least @c n steps away from the given iterator. */
00067   template<typename _Iterator>
00068     class _After_nth_from
00069     {
00070       typedef typename std::iterator_traits<_Iterator>::difference_type
00071       difference_type;
00072 
00073       _Iterator _M_base;
00074       difference_type _M_n;
00075 
00076     public:
00077       _After_nth_from(const difference_type& __n, const _Iterator& __base)
00078       : _M_base(__base), _M_n(__n) { }
00079 
00080       bool
00081       operator()(const _Iterator& __x) const
00082       { return __x - _M_base >= _M_n; }
00083     };
00084 
00085   /**
00086    * @brief Base class for constructing a "safe" sequence type that
00087    * tracks iterators that reference it.
00088    *
00089    * The class template %_Safe_sequence simplifies the construction of
00090    * "safe" sequences that track the iterators that reference the
00091    * sequence, so that the iterators are notified of changes in the
00092    * sequence that may affect their operation, e.g., if the container
00093    * invalidates its iterators or is destructed. This class template
00094    * may only be used by deriving from it and passing the name of the
00095    * derived class as its template parameter via the curiously
00096    * recurring template pattern. The derived class must have @c
00097    * iterator and @const_iterator types that are instantiations of
00098    * class template _Safe_iterator for this sequence. Iterators will
00099    * then be tracked automatically.
00100    */
00101   template<typename _Sequence>
00102     class _Safe_sequence : public _Safe_sequence_base
00103     {
00104     public:
00105       /** Invalidates all iterators @c x that reference this sequence,
00106       are not singular, and for which @c pred(x) returns @c
00107       true. The user of this routine should be careful not to make
00108       copies of the iterators passed to @p pred, as the copies may
00109       interfere with the invalidation. */
00110       template<typename _Predicate>
00111         void
00112         _M_invalidate_if(_Predicate __pred);
00113 
00114       /** Transfers all iterators that reference this memory location
00115       to this sequence from whatever sequence they are attached
00116       to. */
00117       template<typename _Iterator>
00118         void
00119         _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x);
00120     };
00121 
00122   template<typename _Sequence>
00123     template<typename _Predicate>
00124       void
00125       _Safe_sequence<_Sequence>::
00126       _M_invalidate_if(_Predicate __pred)
00127       {
00128         typedef typename _Sequence::iterator iterator;
00129         typedef typename _Sequence::const_iterator const_iterator;
00130 
00131     __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
00132         for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
00133       {
00134         iterator* __victim = static_cast<iterator*>(__iter);
00135         __iter = __iter->_M_next;
00136         if (!__victim->_M_singular())
00137           {
00138         if (__pred(__victim->base()))
00139           __victim->_M_invalidate_single();
00140           }
00141       }
00142 
00143         for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
00144       {
00145         const_iterator* __victim = static_cast<const_iterator*>(__iter2);
00146         __iter2 = __iter2->_M_next;
00147         if (!__victim->_M_singular())
00148           {
00149         if (__pred(__victim->base()))
00150           __victim->_M_invalidate_single();
00151           }
00152       }
00153       }
00154 
00155   template<typename _Sequence>
00156     template<typename _Iterator>
00157       void
00158       _Safe_sequence<_Sequence>::
00159       _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x)
00160       {
00161     _Safe_sequence_base* __from = __x._M_sequence;
00162     if (!__from)
00163       return;
00164 
00165         typedef typename _Sequence::iterator iterator;
00166         typedef typename _Sequence::const_iterator const_iterator;
00167 
00168     __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
00169         for (_Safe_iterator_base* __iter = __from->_M_iterators; __iter;)
00170       {
00171         iterator* __victim = static_cast<iterator*>(__iter);
00172         __iter = __iter->_M_next;
00173         if (!__victim->_M_singular() && __victim->base() == __x.base())
00174           __victim->_M_attach_single(static_cast<_Sequence*>(this));
00175       }
00176 
00177         for (_Safe_iterator_base* __iter2 = __from->_M_const_iterators; 
00178          __iter2;)
00179       {
00180         const_iterator* __victim = static_cast<const_iterator*>(__iter2);
00181         __iter2 = __iter2->_M_next;
00182         if (!__victim->_M_singular() && __victim->base() == __x.base())
00183           __victim->_M_attach_single(static_cast<_Sequence*>(this));
00184       }
00185       }
00186 } // namespace __gnu_debug
00187 
00188 #endif

Generated on Thu Nov 1 13:12:23 2007 for libstdc++ by  doxygen 1.5.1