2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / debug / safe_base.h
blob41ab648f5b95eb7e59e276cbdc37e4b5946d12be
1 // Safe sequence/iterator base implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
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.
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 // 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
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.
31 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
32 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
34 namespace std
36 namespace __gnu_debug
38 class _Safe_sequence_base;
40 /** \brief Basic functionality for a "safe" iterator.
42 * The %_Safe_iterator_base base class implements the functionality
43 * of a safe iterator that is not specific to a particular iterator
44 * type. It contains a pointer back to the sequence it references
45 * along with iterator version information and pointers to form a
46 * doubly-linked list of iterators referenced by the container.
48 * This class must not perform any operations that can throw an
49 * exception, or the exception guarantees of derived iterators will
50 * be broken.
52 class _Safe_iterator_base
54 public:
55 /** The sequence this iterator references; may be NULL to indicate
56 a singular iterator. */
57 _Safe_sequence_base* _M_sequence;
59 /** The version number of this iterator. The sentinel value 0 is
60 * used to indicate an invalidated iterator (i.e., one that is
61 * singular because of an operation on the container). This
62 * version number must equal the version number in the sequence
63 * referenced by _M_sequence for the iterator to be
64 * non-singular.
66 unsigned int _M_version;
68 /** Pointer to the previous iterator in the sequence's list of
69 iterators. Only valid when _M_sequence != NULL. */
70 _Safe_iterator_base* _M_prior;
72 /** Pointer to the next iterator in the sequence's list of
73 iterators. Only valid when _M_sequence != NULL. */
74 _Safe_iterator_base* _M_next;
76 protected:
77 /** Initializes the iterator and makes it singular. */
78 _Safe_iterator_base()
79 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
80 { }
82 /** Initialize the iterator to reference the sequence pointed to
83 * by @p__seq. @p __constant is true when we are initializing a
84 * constant iterator, and false if it is a mutable iterator. Note
85 * that @p __seq may be NULL, in which case the iterator will be
86 * singular. Otherwise, the iterator will reference @p __seq and
87 * be nonsingular.
89 _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
90 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
91 { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
93 /** Initializes the iterator to reference the same sequence that
94 @p __x does. @p __constant is true if this is a constant
95 iterator, and false if it is mutable. */
96 _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
97 : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
98 { this->_M_attach(__x._M_sequence, __constant); }
100 _Safe_iterator_base&
101 operator=(const _Safe_iterator_base&);
103 explicit
104 _Safe_iterator_base(const _Safe_iterator_base&);
106 ~_Safe_iterator_base() { this->_M_detach(); }
108 public:
109 /** Attaches this iterator to the given sequence, detaching it
110 * from whatever sequence it was attached to originally. If the
111 * new sequence is the NULL pointer, the iterator is left
112 * unattached.
114 void _M_attach(_Safe_sequence_base* __seq, bool __constant);
116 /** Detach the iterator for whatever sequence it is attached to,
117 * if any.
119 void _M_detach();
121 /** Determines if we are attached to the given sequence. */
122 bool _M_attached_to(const _Safe_sequence_base* __seq) const
123 { return _M_sequence == __seq; }
125 /** Is this iterator singular? */
126 bool _M_singular() const;
128 /** Can we compare this iterator to the given iterator @p __x?
129 Returns true if both iterators are nonsingular and reference
130 the same sequence. */
131 bool _M_can_compare(const _Safe_iterator_base& __x) const;
135 * @brief Base class that supports tracking of iterators that
136 * reference a sequence.
138 * The %_Safe_sequence_base class provides basic support for
139 * tracking iterators into a sequence. Sequences that track
140 * iterators must derived from %_Safe_sequence_base publicly, so
141 * that safe iterators (which inherit _Safe_iterator_base) can
142 * attach to them. This class contains two linked lists of
143 * iterators, one for constant iterators and one for mutable
144 * iterators, and a version number that allows very fast
145 * invalidation of all iterators that reference the container.
147 * This class must ensure that no operation on it may throw an
148 * exception, otherwise "safe" sequences may fail to provide the
149 * exception-safety guarantees required by the C++ standard.
151 class _Safe_sequence_base
153 public:
154 /// The list of mutable iterators that reference this container
155 _Safe_iterator_base* _M_iterators;
157 /// The list of constant iterators that reference this container
158 _Safe_iterator_base* _M_const_iterators;
160 /// The container version number. This number may never be 0.
161 mutable unsigned int _M_version;
163 protected:
164 // Initialize with a version number of 1 and no iterators
165 _Safe_sequence_base()
166 : _M_iterators(0), _M_const_iterators(0), _M_version(1)
169 /** Notify all iterators that reference this sequence that the
170 sequence is being destroyed. */
171 ~_Safe_sequence_base()
172 { this->_M_detach_all(); }
174 /** Detach all iterators, leaving them singular. */
175 void
176 _M_detach_all();
178 /** Detach all singular iterators.
179 * @post for all iterators i attached to this sequence,
180 * i->_M_version == _M_version.
182 void
183 _M_detach_singular();
185 /** Revalidates all attached singular iterators. This method may
186 * be used to validate iterators that were invalidated before
187 * (but for some reasion, such as an exception, need to become
188 * valid again).
190 void
191 _M_revalidate_singular();
193 /** Swap this sequence with the given sequence. This operation
194 * also swaps ownership of the iterators, so that when the
195 * operation is complete all iterators that originally referenced
196 * one container now reference the other container.
198 void
199 _M_swap(_Safe_sequence_base& __x);
201 public:
202 /** Invalidates all iterators. */
203 void
204 _M_invalidate_all() const
205 { if (++_M_version == 0) _M_version = 1; }
207 } // namespace __gnu_debug
208 } // namespace std
210 #endif