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