safe_sequence.h

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

Generated on Fri May 6 01:09:03 2005 for libstdc++-v3 Source by  doxygen 1.4.2