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