]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/safe_base.h
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / safe_base.h
1 // Safe sequence/iterator base implementation -*- C++ -*-
2
3 // Copyright (C) 2003
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
32 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
33
34 namespace __gnu_debug
35 {
36 class _Safe_sequence_base;
37
38 /** \brief Basic functionality for a "safe" iterator.
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
62 * non-singular.
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. */
76 _Safe_iterator_base()
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
81 * by @p__seq. @p __constant is true when we are initializing a
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
85 * be nonsingular.
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
98 ~_Safe_iterator_base() { this->_M_detach(); }
99
100 public:
101 /** Attaches this iterator to the given sequence, detaching it
102 * from whatever sequence it was attached to originally. If the
103 * new sequence is the NULL pointer, the iterator is left
104 * unattached.
105 */
106 void _M_attach(_Safe_sequence_base* __seq, bool __constant);
107
108 /** Detach the iterator for whatever sequence it is attached to,
109 * if any.
110 */
111 void _M_detach();
112
113 /** Determines if we are attached to the given sequence. */
114 bool _M_attached_to(const _Safe_sequence_base* __seq) const
115 { return _M_sequence == __seq; }
116
117 /** Is this iterator singular? */
118 bool _M_singular() const;
119
120 /** Can we compare this iterator to the given iterator @p __x?
121 Returns true if both iterators are nonsingular and reference
122 the same sequence. */
123 bool _M_can_compare(const _Safe_iterator_base& __x) const;
124 };
125
126 /**
127 * @brief Base class that supports tracking of iterators that
128 * reference a sequence.
129 *
130 * The %_Safe_sequence_base class provides basic support for
131 * tracking iterators into a sequence. Sequences that track
132 * iterators must derived from %_Safe_sequence_base publicly, so
133 * that safe iterators (which inherit _Safe_iterator_base) can
134 * attach to them. This class contains two linked lists of
135 * iterators, one for constant iterators and one for mutable
136 * iterators, and a version number that allows very fast
137 * invalidation of all iterators that reference the container.
138 *
139 * This class must ensure that no operation on it may throw an
140 * exception, otherwise "safe" sequences may fail to provide the
141 * exception-safety guarantees required by the C++ standard.
142 */
143 class _Safe_sequence_base
144 {
145 public:
146 /// The list of mutable iterators that reference this container
147 _Safe_iterator_base* _M_iterators;
148
149 /// The list of constant iterators that reference this container
150 _Safe_iterator_base* _M_const_iterators;
151
152 /// The container version number. This number may never be 0.
153 mutable unsigned int _M_version;
154
155 protected:
156 // Initialize with a version number of 1 and no iterators
157 _Safe_sequence_base()
158 : _M_iterators(0), _M_const_iterators(0), _M_version(1)
159 { }
160
161 /** Notify all iterators that reference this sequence that the
162 sequence is being destroyed. */
163 ~_Safe_sequence_base()
164 { this->_M_detach_all(); }
165
166 /** Detach all iterators, leaving them singular. */
167 void
168 _M_detach_all();
169
170 /** Detach all singular iterators.
171 * @post for all iterators i attached to this sequence,
172 * i->_M_version == _M_version.
173 */
174 void
175 _M_detach_singular();
176
177 /** Revalidates all attached singular iterators. This method may
178 * be used to validate iterators that were invalidated before
179 * (but for some reasion, such as an exception, need to become
180 * valid again).
181 */
182 void
183 _M_revalidate_singular();
184
185 /** Swap this sequence with the given sequence. This operation
186 * also swaps ownership of the iterators, so that when the
187 * operation is complete all iterators that originally referenced
188 * one container now reference the other container.
189 */
190 void
191 _M_swap(_Safe_sequence_base& __x);
192
193 public:
194 /** Invalidates all iterators. */
195 void
196 _M_invalidate_all() const
197 { if (++_M_version == 0) _M_version = 1; }
198 };
199 } // namespace __gnu_debug
200
201 #endif