2010-11-27 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / libstdc++-v3 / include / debug / safe_base.h
blob2ebdd89b58f5a35e5efa850e33097a3930cd15a0
1 // Safe sequence/iterator base implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010
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 ();
152 * @brief Base class that supports tracking of iterators that
153 * reference a sequence.
155 * The %_Safe_sequence_base class provides basic support for
156 * tracking iterators into a sequence. Sequences that track
157 * iterators must derived from %_Safe_sequence_base publicly, so
158 * that safe iterators (which inherit _Safe_iterator_base) can
159 * attach to them. This class contains two linked lists of
160 * iterators, one for constant iterators and one for mutable
161 * iterators, and a version number that allows very fast
162 * invalidation of all iterators that reference the container.
164 * This class must ensure that no operation on it may throw an
165 * exception, otherwise @a safe sequences may fail to provide the
166 * exception-safety guarantees required by the C++ standard.
168 class _Safe_sequence_base
170 public:
171 /// The list of mutable iterators that reference this container
172 _Safe_iterator_base* _M_iterators;
174 /// The list of constant iterators that reference this container
175 _Safe_iterator_base* _M_const_iterators;
177 /// The container version number. This number may never be 0.
178 mutable unsigned int _M_version;
180 protected:
181 // Initialize with a version number of 1 and no iterators
182 _Safe_sequence_base()
183 : _M_iterators(0), _M_const_iterators(0), _M_version(1)
186 /** Notify all iterators that reference this sequence that the
187 sequence is being destroyed. */
188 ~_Safe_sequence_base()
189 { this->_M_detach_all(); }
191 /** Detach all iterators, leaving them singular. */
192 void
193 _M_detach_all();
195 /** Detach all singular iterators.
196 * @post for all iterators i attached to this sequence,
197 * i->_M_version == _M_version.
199 void
200 _M_detach_singular();
202 /** Revalidates all attached singular iterators. This method may
203 * be used to validate iterators that were invalidated before
204 * (but for some reason, such as an exception, need to become
205 * valid again).
207 void
208 _M_revalidate_singular();
210 /** Swap this sequence with the given sequence. This operation
211 * also swaps ownership of the iterators, so that when the
212 * operation is complete all iterators that originally referenced
213 * one container now reference the other container.
215 void
216 _M_swap(_Safe_sequence_base& __x);
218 /** For use in _Safe_sequence. */
219 __gnu_cxx::__mutex& _M_get_mutex() throw ();
221 public:
222 /** Invalidates all iterators. */
223 void
224 _M_invalidate_all() const
225 { if (++_M_version == 0) _M_version = 1; }
227 /** Attach an iterator to this sequence. */
228 void
229 _M_attach(_Safe_iterator_base* __it, bool __constant);
231 /** Likewise but not thread safe. */
232 void
233 _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
235 /** Detach an iterator from this sequence */
236 void
237 _M_detach(_Safe_iterator_base* __it);
239 /** Likewise but not thread safe. */
240 void
241 _M_detach_single(_Safe_iterator_base* __it) throw ();
243 } // namespace __gnu_debug
245 #endif