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