1// Safe sequence/iterator base implementation  -*- C++ -*-
2
3// Copyright (C) 2003, 2004
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
34namespace __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&
99    operator=(const _Safe_iterator_base&);
100
101    explicit
102    _Safe_iterator_base(const _Safe_iterator_base&);
103
104    ~_Safe_iterator_base() { this->_M_detach(); }
105
106  public:
107    /** Attaches this iterator to the given sequence, detaching it
108     *	from whatever sequence it was attached to originally. If the
109     *	new sequence is the NULL pointer, the iterator is left
110     *	unattached.
111     */
112    void _M_attach(_Safe_sequence_base* __seq, bool __constant);
113
114    /** Detach the iterator for whatever sequence it is attached to,
115     *	if any.
116    */
117    void _M_detach();
118
119    /** Determines if we are attached to the given sequence. */
120    bool _M_attached_to(const _Safe_sequence_base* __seq) const
121    { return _M_sequence == __seq; }
122
123    /** Is this iterator singular? */
124    bool _M_singular() const;
125
126    /** Can we compare this iterator to the given iterator @p __x?
127	Returns true if both iterators are nonsingular and reference
128	the same sequence. */
129    bool _M_can_compare(const _Safe_iterator_base& __x) const;
130  };
131
132  /**
133   * @brief Base class that supports tracking of iterators that
134   * reference a sequence.
135   *
136   * The %_Safe_sequence_base class provides basic support for
137   * tracking iterators into a sequence. Sequences that track
138   * iterators must derived from %_Safe_sequence_base publicly, so
139   * that safe iterators (which inherit _Safe_iterator_base) can
140   * attach to them. This class contains two linked lists of
141   * iterators, one for constant iterators and one for mutable
142   * iterators, and a version number that allows very fast
143   * invalidation of all iterators that reference the container.
144   *
145   * This class must ensure that no operation on it may throw an
146   * exception, otherwise "safe" sequences may fail to provide the
147   * exception-safety guarantees required by the C++ standard.
148   */
149  class _Safe_sequence_base
150  {
151  public:
152    /// The list of mutable iterators that reference this container
153    _Safe_iterator_base* _M_iterators;
154
155    /// The list of constant iterators that reference this container
156    _Safe_iterator_base* _M_const_iterators;
157
158    /// The container version number. This number may never be 0.
159    mutable unsigned int _M_version;
160
161  protected:
162    // Initialize with a version number of 1 and no iterators
163    _Safe_sequence_base()
164    : _M_iterators(0), _M_const_iterators(0), _M_version(1)
165    { }
166
167    /** Notify all iterators that reference this sequence that the
168	sequence is being destroyed. */
169    ~_Safe_sequence_base()
170    { this->_M_detach_all(); }
171
172    /** Detach all iterators, leaving them singular. */
173    void
174    _M_detach_all();
175
176    /** Detach all singular iterators.
177     *  @post for all iterators i attached to this sequence,
178     *   i->_M_version == _M_version.
179     */
180    void
181    _M_detach_singular();
182
183    /** Revalidates all attached singular iterators.  This method may
184     *  be used to validate iterators that were invalidated before
185     *  (but for some reasion, such as an exception, need to become
186     *  valid again).
187     */
188    void
189    _M_revalidate_singular();
190
191    /** Swap this sequence with the given sequence. This operation
192     *  also swaps ownership of the iterators, so that when the
193     *  operation is complete all iterators that originally referenced
194     *  one container now reference the other container.
195     */
196    void
197    _M_swap(_Safe_sequence_base& __x);
198
199  public:
200    /** Invalidates all iterators. */
201    void
202    _M_invalidate_all() const
203    { if (++_M_version == 0) _M_version = 1; }
204  };
205} // namespace __gnu_debug
206
207#endif
208