libstdc++
safe_sequence.h
Go to the documentation of this file.
1 // Safe sequence implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file debug/safe_sequence.h
27  * This file is a GNU debug extension to the Standard C++ Library.
28  */
29 
30 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
31 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
32 
33 #include <debug/debug.h>
34 #include <debug/macros.h>
35 #include <debug/functions.h>
36 #include <debug/safe_base.h>
37 
38 namespace __gnu_debug
39 {
40  template<typename _Iterator, typename _Sequence>
41  class _Safe_iterator;
42 
43  /** A simple function object that returns true if the passed-in
44  * value is not equal to the stored value. It saves typing over
45  * using both bind1st and not_equal.
46  */
47  template<typename _Type>
49  {
50  _Type __value;
51 
52  public:
53  explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
54 
55  bool
56  operator()(const _Type& __x) const
57  { return __value != __x; }
58  };
59 
60  /** A simple function object that returns true if the passed-in
61  * value is equal to the stored value. */
62  template <typename _Type>
63  class _Equal_to
64  {
65  _Type __value;
66 
67  public:
68  explicit _Equal_to(const _Type& __v) : __value(__v) { }
69 
70  bool
71  operator()(const _Type& __x) const
72  { return __value == __x; }
73  };
74 
75  /** A function object that returns true when the given random access
76  iterator is at least @c n steps away from the given iterator. */
77  template<typename _Iterator>
79  {
80  typedef typename std::iterator_traits<_Iterator>::difference_type
81  difference_type;
82 
83  _Iterator _M_base;
84  difference_type _M_n;
85 
86  public:
87  _After_nth_from(const difference_type& __n, const _Iterator& __base)
88  : _M_base(__base), _M_n(__n) { }
89 
90  bool
91  operator()(const _Iterator& __x) const
92  { return __x - _M_base >= _M_n; }
93  };
94 
95  /**
96  * @brief Base class for constructing a @a safe sequence type that
97  * tracks iterators that reference it.
98  *
99  * The class template %_Safe_sequence simplifies the construction of
100  * @a safe sequences that track the iterators that reference the
101  * sequence, so that the iterators are notified of changes in the
102  * sequence that may affect their operation, e.g., if the container
103  * invalidates its iterators or is destructed. This class template
104  * may only be used by deriving from it and passing the name of the
105  * derived class as its template parameter via the curiously
106  * recurring template pattern. The derived class must have @c
107  * iterator and @c const_iterator types that are instantiations of
108  * class template _Safe_iterator for this sequence. Iterators will
109  * then be tracked automatically.
110  */
111  template<typename _Sequence>
113  {
114  public:
115  /** Invalidates all iterators @c x that reference this sequence,
116  are not singular, and for which @c __pred(x) returns @c
117  true. @c __pred will be invoked with the normal iterators nested
118  in the safe ones. */
119  template<typename _Predicate>
120  void
121  _M_invalidate_if(_Predicate __pred);
122 
123  /** Transfers all iterators @c x that reference @c from sequence,
124  are not singular, and for which @c __pred(x) returns @c
125  true. @c __pred will be invoked with the normal iterators nested
126  in the safe ones. */
127  template<typename _Predicate>
128  void
129  _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred);
130  };
131 } // namespace __gnu_debug
132 
133 #include <debug/safe_sequence.tcc>
134 
135 #endif