Fix PR56035.
[official-gcc.git] / libstdc++-v3 / include / debug / safe_base.h
blob7e6222fe1b7c672a1be79f08251cb1409b102e94
1 // Safe sequence/iterator base implementation -*- C++ -*-
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.
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.
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.
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/>.
26 /** @file debug/safe_base.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
31 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
33 #include <ext/concurrence.h>
35 namespace __gnu_debug
37 class _Safe_sequence_base;
39 /** \brief Basic functionality for a @a safe iterator.
41 * The %_Safe_iterator_base base class implements the functionality
42 * of a safe iterator that is not specific to a particular iterator
43 * type. It contains a pointer back to the sequence it references
44 * along with iterator version information and pointers to form a
45 * doubly-linked list of iterators referenced by the container.
47 * This class must not perform any operations that can throw an
48 * exception, or the exception guarantees of derived iterators will
49 * be broken.
51 class _Safe_iterator_base
53 public:
54 /** The sequence this iterator references; may be NULL to indicate
55 a singular iterator. */
56 _Safe_sequence_base* _M_sequence;
58 /** The version number of this iterator. The sentinel value 0 is
59 * used to indicate an invalidated iterator (i.e., one that is
60 * singular because of an operation on the container). This
61 * version number must equal the version number in the sequence
62 * referenced by _M_sequence for the iterator to be
63 * non-singular.
65 unsigned int _M_version;
67 /** Pointer to the previous iterator in the sequence's list of
68 iterators. Only valid when _M_sequence != NULL. */
69 _Safe_iterator_base* _M_prior;
71 /** Pointer to the next iterator in the sequence's list of
72 iterators. Only valid when _M_sequence != NULL. */
73 _Safe_iterator_base* _M_next;
75 protected:
76 /** Initializes the iterator and makes it singular. */
77 _Safe_iterator_base()
78 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
79 { }
81 /** Initialize the iterator to reference the sequence pointed to
82 * by @p __seq. @p __constant is true when we are initializing a
83 * constant iterator, and false if it is a mutable iterator. Note
84 * that @p __seq may be NULL, in which case the iterator will be
85 * singular. Otherwise, the iterator will reference @p __seq and
86 * be nonsingular.
88 _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
89 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
90 { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
92 /** Initializes the iterator to reference the same sequence that
93 @p __x does. @p __constant is true if this is a constant
94 iterator, and false if it is mutable. */
95 _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
96 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
97 { this->_M_attach(__x._M_sequence, __constant); }
99 _Safe_iterator_base&
100 operator=(const _Safe_iterator_base&);
102 explicit
103 _Safe_iterator_base(const _Safe_iterator_base&);
105 ~_Safe_iterator_base() { this->_M_detach(); }
107 /** For use in _Safe_iterator. */
108 __gnu_cxx::__mutex& _M_get_mutex() throw ();
110 public:
111 /** Attaches this iterator to the given sequence, detaching it
112 * from whatever sequence it was attached to originally. If the
113 * new sequence is the NULL pointer, the iterator is left
114 * unattached.
116 void _M_attach(_Safe_sequence_base* __seq, bool __constant);
118 /** Likewise, but not thread-safe. */
119 void _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
121 /** Detach the iterator for whatever sequence it is attached to,
122 * if any.
124 void _M_detach();
126 /** Likewise, but not thread-safe. */
127 void _M_detach_single() throw ();
129 /** Determines if we are attached to the given sequence. */
130 bool _M_attached_to(const _Safe_sequence_base* __seq) const
131 { return _M_sequence == __seq; }
133 /** Is this iterator singular? */
134 _GLIBCXX_PURE bool _M_singular() const throw ();
136 /** Can we compare this iterator to the given iterator @p __x?
137 Returns true if both iterators are nonsingular and reference
138 the same sequence. */
139 _GLIBCXX_PURE bool _M_can_compare(const _Safe_iterator_base& __x) const throw ();
141 /** Invalidate the iterator, making it singular. */
142 void
143 _M_invalidate()
144 { _M_version = 0; }
146 /** Reset all member variables */
147 void
148 _M_reset() throw ();
150 /** Unlink itself */
151 void
152 _M_unlink() throw ()
154 if (_M_prior)
155 _M_prior->_M_next = _M_next;
156 if (_M_next)
157 _M_next->_M_prior = _M_prior;
162 * @brief Base class that supports tracking of iterators that
163 * reference a sequence.
165 * The %_Safe_sequence_base class provides basic support for
166 * tracking iterators into a sequence. Sequences that track
167 * iterators must derived from %_Safe_sequence_base publicly, so
168 * that safe iterators (which inherit _Safe_iterator_base) can
169 * attach to them. This class contains two linked lists of
170 * iterators, one for constant iterators and one for mutable
171 * iterators, and a version number that allows very fast
172 * invalidation of all iterators that reference the container.
174 * This class must ensure that no operation on it may throw an
175 * exception, otherwise @a safe sequences may fail to provide the
176 * exception-safety guarantees required by the C++ standard.
178 class _Safe_sequence_base
180 public:
181 /// The list of mutable iterators that reference this container
182 _Safe_iterator_base* _M_iterators;
184 /// The list of constant iterators that reference this container
185 _Safe_iterator_base* _M_const_iterators;
187 /// The container version number. This number may never be 0.
188 mutable unsigned int _M_version;
190 protected:
191 // Initialize with a version number of 1 and no iterators
192 _Safe_sequence_base()
193 : _M_iterators(0), _M_const_iterators(0), _M_version(1)
196 /** Notify all iterators that reference this sequence that the
197 sequence is being destroyed. */
198 ~_Safe_sequence_base()
199 { this->_M_detach_all(); }
201 /** Detach all iterators, leaving them singular. */
202 void
203 _M_detach_all();
205 /** Detach all singular iterators.
206 * @post for all iterators i attached to this sequence,
207 * i->_M_version == _M_version.
209 void
210 _M_detach_singular();
212 /** Revalidates all attached singular iterators. This method may
213 * be used to validate iterators that were invalidated before
214 * (but for some reason, such as an exception, need to become
215 * valid again).
217 void
218 _M_revalidate_singular();
220 /** Swap this sequence with the given sequence. This operation
221 * also swaps ownership of the iterators, so that when the
222 * operation is complete all iterators that originally referenced
223 * one container now reference the other container.
225 void
226 _M_swap(_Safe_sequence_base& __x);
228 /** For use in _Safe_sequence. */
229 __gnu_cxx::__mutex& _M_get_mutex() throw ();
231 public:
232 /** Invalidates all iterators. */
233 void
234 _M_invalidate_all() const
235 { if (++_M_version == 0) _M_version = 1; }
237 /** Attach an iterator to this sequence. */
238 void
239 _M_attach(_Safe_iterator_base* __it, bool __constant);
241 /** Likewise but not thread safe. */
242 void
243 _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
245 /** Detach an iterator from this sequence */
246 void
247 _M_detach(_Safe_iterator_base* __it);
249 /** Likewise but not thread safe. */
250 void
251 _M_detach_single(_Safe_iterator_base* __it) throw ();
253 } // namespace __gnu_debug
255 #endif