]>
Commit | Line | Data |
---|---|---|
285b36d6 BK |
1 | // Safe sequence/iterator base implementation -*- C++ -*- |
2 | ||
aa118a03 | 3 | // Copyright (C) 2003-2014 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. | |
19 | ||
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_base.h |
26 | * This file is a GNU debug extension to the Standard C++ Library. | |
27 | */ | |
28 | ||
285b36d6 BK |
29 | #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H |
30 | #define _GLIBCXX_DEBUG_SAFE_BASE_H 1 | |
31 | ||
eebbe2c7 PC |
32 | #include <ext/concurrence.h> |
33 | ||
285b36d6 BK |
34 | namespace __gnu_debug |
35 | { | |
36 | class _Safe_sequence_base; | |
37 | ||
2a60a9f6 | 38 | /** \brief Basic functionality for a @a safe iterator. |
285b36d6 BK |
39 | * |
40 | * The %_Safe_iterator_base base class implements the functionality | |
41 | * of a safe iterator that is not specific to a particular iterator | |
42 | * type. It contains a pointer back to the sequence it references | |
43 | * along with iterator version information and pointers to form a | |
44 | * doubly-linked list of iterators referenced by the container. | |
45 | * | |
46 | * This class must not perform any operations that can throw an | |
47 | * exception, or the exception guarantees of derived iterators will | |
48 | * be broken. | |
49 | */ | |
50 | class _Safe_iterator_base | |
51 | { | |
52 | public: | |
53 | /** The sequence this iterator references; may be NULL to indicate | |
54 | a singular iterator. */ | |
55 | _Safe_sequence_base* _M_sequence; | |
56 | ||
57 | /** The version number of this iterator. The sentinel value 0 is | |
58 | * used to indicate an invalidated iterator (i.e., one that is | |
59 | * singular because of an operation on the container). This | |
60 | * version number must equal the version number in the sequence | |
61 | * referenced by _M_sequence for the iterator to be | |
526da49c | 62 | * non-singular. |
285b36d6 BK |
63 | */ |
64 | unsigned int _M_version; | |
65 | ||
66 | /** Pointer to the previous iterator in the sequence's list of | |
67 | iterators. Only valid when _M_sequence != NULL. */ | |
68 | _Safe_iterator_base* _M_prior; | |
69 | ||
70 | /** Pointer to the next iterator in the sequence's list of | |
71 | iterators. Only valid when _M_sequence != NULL. */ | |
72 | _Safe_iterator_base* _M_next; | |
73 | ||
74 | protected: | |
75 | /** Initializes the iterator and makes it singular. */ | |
526da49c | 76 | _Safe_iterator_base() |
285b36d6 BK |
77 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) |
78 | { } | |
79 | ||
80 | /** Initialize the iterator to reference the sequence pointed to | |
7897a1c0 | 81 | * by @p __seq. @p __constant is true when we are initializing a |
285b36d6 BK |
82 | * constant iterator, and false if it is a mutable iterator. Note |
83 | * that @p __seq may be NULL, in which case the iterator will be | |
84 | * singular. Otherwise, the iterator will reference @p __seq and | |
526da49c | 85 | * be nonsingular. |
285b36d6 BK |
86 | */ |
87 | _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant) | |
88 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) | |
89 | { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); } | |
90 | ||
91 | /** Initializes the iterator to reference the same sequence that | |
92 | @p __x does. @p __constant is true if this is a constant | |
93 | iterator, and false if it is mutable. */ | |
94 | _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant) | |
95 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) | |
96 | { this->_M_attach(__x._M_sequence, __constant); } | |
97 | ||
26c691a8 BK |
98 | _Safe_iterator_base& |
99 | operator=(const _Safe_iterator_base&); | |
100 | ||
4723805a | 101 | explicit |
26c691a8 BK |
102 | _Safe_iterator_base(const _Safe_iterator_base&); |
103 | ||
285b36d6 BK |
104 | ~_Safe_iterator_base() { this->_M_detach(); } |
105 | ||
eebbe2c7 | 106 | /** For use in _Safe_iterator. */ |
5d51b87a | 107 | __gnu_cxx::__mutex& _M_get_mutex() throw (); |
eebbe2c7 | 108 | |
285b36d6 BK |
109 | public: |
110 | /** Attaches this iterator to the given sequence, detaching it | |
111 | * from whatever sequence it was attached to originally. If the | |
112 | * new sequence is the NULL pointer, the iterator is left | |
113 | * unattached. | |
526da49c | 114 | */ |
285b36d6 BK |
115 | void _M_attach(_Safe_sequence_base* __seq, bool __constant); |
116 | ||
eebbe2c7 | 117 | /** Likewise, but not thread-safe. */ |
5d51b87a | 118 | void _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw (); |
eebbe2c7 | 119 | |
285b36d6 | 120 | /** Detach the iterator for whatever sequence it is attached to, |
526da49c | 121 | * if any. |
285b36d6 BK |
122 | */ |
123 | void _M_detach(); | |
124 | ||
eebbe2c7 | 125 | /** Likewise, but not thread-safe. */ |
5d51b87a | 126 | void _M_detach_single() throw (); |
eebbe2c7 | 127 | |
285b36d6 BK |
128 | /** Determines if we are attached to the given sequence. */ |
129 | bool _M_attached_to(const _Safe_sequence_base* __seq) const | |
130 | { return _M_sequence == __seq; } | |
131 | ||
132 | /** Is this iterator singular? */ | |
5d51b87a | 133 | _GLIBCXX_PURE bool _M_singular() const throw (); |
285b36d6 BK |
134 | |
135 | /** Can we compare this iterator to the given iterator @p __x? | |
136 | Returns true if both iterators are nonsingular and reference | |
137 | the same sequence. */ | |
5d51b87a | 138 | _GLIBCXX_PURE bool _M_can_compare(const _Safe_iterator_base& __x) const throw (); |
afe96d41 FD |
139 | |
140 | /** Invalidate the iterator, making it singular. */ | |
141 | void | |
142 | _M_invalidate() | |
143 | { _M_version = 0; } | |
144 | ||
145 | /** Reset all member variables */ | |
146 | void | |
147 | _M_reset() throw (); | |
8c9f4dfa FD |
148 | |
149 | /** Unlink itself */ | |
150 | void | |
151 | _M_unlink() throw () | |
152 | { | |
153 | if (_M_prior) | |
154 | _M_prior->_M_next = _M_next; | |
155 | if (_M_next) | |
156 | _M_next->_M_prior = _M_prior; | |
157 | } | |
285b36d6 BK |
158 | }; |
159 | ||
160 | /** | |
161 | * @brief Base class that supports tracking of iterators that | |
162 | * reference a sequence. | |
163 | * | |
164 | * The %_Safe_sequence_base class provides basic support for | |
165 | * tracking iterators into a sequence. Sequences that track | |
166 | * iterators must derived from %_Safe_sequence_base publicly, so | |
167 | * that safe iterators (which inherit _Safe_iterator_base) can | |
168 | * attach to them. This class contains two linked lists of | |
169 | * iterators, one for constant iterators and one for mutable | |
170 | * iterators, and a version number that allows very fast | |
171 | * invalidation of all iterators that reference the container. | |
172 | * | |
173 | * This class must ensure that no operation on it may throw an | |
2a60a9f6 | 174 | * exception, otherwise @a safe sequences may fail to provide the |
285b36d6 BK |
175 | * exception-safety guarantees required by the C++ standard. |
176 | */ | |
177 | class _Safe_sequence_base | |
178 | { | |
179 | public: | |
180 | /// The list of mutable iterators that reference this container | |
181 | _Safe_iterator_base* _M_iterators; | |
526da49c | 182 | |
285b36d6 BK |
183 | /// The list of constant iterators that reference this container |
184 | _Safe_iterator_base* _M_const_iterators; | |
526da49c | 185 | |
285b36d6 BK |
186 | /// The container version number. This number may never be 0. |
187 | mutable unsigned int _M_version; | |
526da49c | 188 | |
285b36d6 BK |
189 | protected: |
190 | // Initialize with a version number of 1 and no iterators | |
191 | _Safe_sequence_base() | |
192 | : _M_iterators(0), _M_const_iterators(0), _M_version(1) | |
193 | { } | |
526da49c | 194 | |
ace295af FD |
195 | #if __cplusplus >= 201103L |
196 | _Safe_sequence_base(_Safe_sequence_base&& __x) noexcept | |
197 | : _Safe_sequence_base() | |
198 | { _M_swap(__x); } | |
199 | #endif | |
200 | ||
285b36d6 BK |
201 | /** Notify all iterators that reference this sequence that the |
202 | sequence is being destroyed. */ | |
203 | ~_Safe_sequence_base() | |
204 | { this->_M_detach_all(); } | |
526da49c | 205 | |
285b36d6 | 206 | /** Detach all iterators, leaving them singular. */ |
526da49c | 207 | void |
285b36d6 | 208 | _M_detach_all(); |
526da49c BI |
209 | |
210 | /** Detach all singular iterators. | |
211 | * @post for all iterators i attached to this sequence, | |
285b36d6 BK |
212 | * i->_M_version == _M_version. |
213 | */ | |
214 | void | |
215 | _M_detach_singular(); | |
526da49c | 216 | |
285b36d6 BK |
217 | /** Revalidates all attached singular iterators. This method may |
218 | * be used to validate iterators that were invalidated before | |
28dac70a | 219 | * (but for some reason, such as an exception, need to become |
285b36d6 BK |
220 | * valid again). |
221 | */ | |
222 | void | |
223 | _M_revalidate_singular(); | |
526da49c | 224 | |
285b36d6 BK |
225 | /** Swap this sequence with the given sequence. This operation |
226 | * also swaps ownership of the iterators, so that when the | |
227 | * operation is complete all iterators that originally referenced | |
228 | * one container now reference the other container. | |
229 | */ | |
526da49c | 230 | void |
285b36d6 | 231 | _M_swap(_Safe_sequence_base& __x); |
526da49c | 232 | |
eebbe2c7 | 233 | /** For use in _Safe_sequence. */ |
5d51b87a | 234 | __gnu_cxx::__mutex& _M_get_mutex() throw (); |
eebbe2c7 | 235 | |
285b36d6 BK |
236 | public: |
237 | /** Invalidates all iterators. */ | |
526da49c | 238 | void |
285b36d6 BK |
239 | _M_invalidate_all() const |
240 | { if (++_M_version == 0) _M_version = 1; } | |
afe96d41 FD |
241 | |
242 | /** Attach an iterator to this sequence. */ | |
243 | void | |
244 | _M_attach(_Safe_iterator_base* __it, bool __constant); | |
245 | ||
246 | /** Likewise but not thread safe. */ | |
247 | void | |
248 | _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw (); | |
249 | ||
250 | /** Detach an iterator from this sequence */ | |
251 | void | |
252 | _M_detach(_Safe_iterator_base* __it); | |
253 | ||
254 | /** Likewise but not thread safe. */ | |
255 | void | |
256 | _M_detach_single(_Safe_iterator_base* __it) throw (); | |
285b36d6 BK |
257 | }; |
258 | } // namespace __gnu_debug | |
259 | ||
526da49c | 260 | #endif |