OSDN Git Service

2851fc3fa9c08c60f97fc696ea803e365d400f08
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / safe_base.h
1 // Safe sequence/iterator base implementation  -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006
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
34 #include <ext/concurrence.h>
35
36 namespace __gnu_debug
37 {
38   class _Safe_sequence_base;
39
40   /** \brief Basic functionality for a "safe" iterator.
41    *
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.
47    *
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.
51    */
52   class _Safe_iterator_base
53   {
54   public:
55     /** The sequence this iterator references; may be NULL to indicate
56         a singular iterator. */
57     _Safe_sequence_base* _M_sequence;
58
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.
65      */
66     unsigned int         _M_version;
67
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;
71
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;
75
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     { }
81
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.
88      */
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); }
92
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); }
99
100     _Safe_iterator_base&
101     operator=(const _Safe_iterator_base&);
102
103     explicit
104     _Safe_iterator_base(const _Safe_iterator_base&);
105
106     ~_Safe_iterator_base() { this->_M_detach(); }
107
108     /** For use in _Safe_iterator. */
109     __gnu_cxx::__mutex& _M_get_mutex();
110
111   public:
112     /** Attaches this iterator to the given sequence, detaching it
113      *  from whatever sequence it was attached to originally. If the
114      *  new sequence is the NULL pointer, the iterator is left
115      *  unattached.
116      */
117     void _M_attach(_Safe_sequence_base* __seq, bool __constant);
118
119     /** Likewise, but not thread-safe. */
120     void _M_attach_single(_Safe_sequence_base* __seq, bool __constant);
121
122     /** Detach the iterator for whatever sequence it is attached to,
123      *  if any.
124     */
125     void _M_detach();
126
127     /** Likewise, but not thread-safe. */
128     void _M_detach_single();
129
130     /** Determines if we are attached to the given sequence. */
131     bool _M_attached_to(const _Safe_sequence_base* __seq) const
132     { return _M_sequence == __seq; }
133
134     /** Is this iterator singular? */
135     bool _M_singular() const;
136
137     /** Can we compare this iterator to the given iterator @p __x?
138         Returns true if both iterators are nonsingular and reference
139         the same sequence. */
140     bool _M_can_compare(const _Safe_iterator_base& __x) const;
141   };
142
143   /**
144    * @brief Base class that supports tracking of iterators that
145    * reference a sequence.
146    *
147    * The %_Safe_sequence_base class provides basic support for
148    * tracking iterators into a sequence. Sequences that track
149    * iterators must derived from %_Safe_sequence_base publicly, so
150    * that safe iterators (which inherit _Safe_iterator_base) can
151    * attach to them. This class contains two linked lists of
152    * iterators, one for constant iterators and one for mutable
153    * iterators, and a version number that allows very fast
154    * invalidation of all iterators that reference the container.
155    *
156    * This class must ensure that no operation on it may throw an
157    * exception, otherwise "safe" sequences may fail to provide the
158    * exception-safety guarantees required by the C++ standard.
159    */
160   class _Safe_sequence_base
161   {
162   public:
163     /// The list of mutable iterators that reference this container
164     _Safe_iterator_base* _M_iterators;
165
166     /// The list of constant iterators that reference this container
167     _Safe_iterator_base* _M_const_iterators;
168
169     /// The container version number. This number may never be 0.
170     mutable unsigned int _M_version;
171
172   protected:
173     // Initialize with a version number of 1 and no iterators
174     _Safe_sequence_base()
175     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
176     { }
177
178     /** Notify all iterators that reference this sequence that the
179         sequence is being destroyed. */
180     ~_Safe_sequence_base()
181     { this->_M_detach_all(); }
182
183     /** Detach all iterators, leaving them singular. */
184     void
185     _M_detach_all();
186
187     /** Detach all singular iterators.
188      *  @post for all iterators i attached to this sequence,
189      *   i->_M_version == _M_version.
190      */
191     void
192     _M_detach_singular();
193
194     /** Revalidates all attached singular iterators.  This method may
195      *  be used to validate iterators that were invalidated before
196      *  (but for some reasion, such as an exception, need to become
197      *  valid again).
198      */
199     void
200     _M_revalidate_singular();
201
202     /** Swap this sequence with the given sequence. This operation
203      *  also swaps ownership of the iterators, so that when the
204      *  operation is complete all iterators that originally referenced
205      *  one container now reference the other container.
206      */
207     void
208     _M_swap(_Safe_sequence_base& __x);
209
210     /** For use in _Safe_sequence. */
211     __gnu_cxx::__mutex& _M_get_mutex();
212
213   public:
214     /** Invalidates all iterators. */
215     void
216     _M_invalidate_all() const
217     { if (++_M_version == 0) _M_version = 1; }
218   };
219 } // namespace __gnu_debug
220
221 #endif