]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libstdc++/include/debug/safe_base.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libstdc++ / 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 /** @file debug/safe_base.h
32  *  This file is a GNU debug extension to the Standard C++ Library.
33  */
34
35 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
36 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
37
38 #include <ext/concurrence.h>
39
40 namespace __gnu_debug
41 {
42   class _Safe_sequence_base;
43
44   /** \brief Basic functionality for a "safe" iterator.
45    *
46    *  The %_Safe_iterator_base base class implements the functionality
47    *  of a safe iterator that is not specific to a particular iterator
48    *  type. It contains a pointer back to the sequence it references
49    *  along with iterator version information and pointers to form a
50    *  doubly-linked list of iterators referenced by the container.
51    *
52    *  This class must not perform any operations that can throw an
53    *  exception, or the exception guarantees of derived iterators will
54    *  be broken.
55    */
56   class _Safe_iterator_base
57   {
58   public:
59     /** The sequence this iterator references; may be NULL to indicate
60         a singular iterator. */
61     _Safe_sequence_base* _M_sequence;
62
63     /** The version number of this iterator. The sentinel value 0 is
64      *  used to indicate an invalidated iterator (i.e., one that is
65      *  singular because of an operation on the container). This
66      *  version number must equal the version number in the sequence
67      *  referenced by _M_sequence for the iterator to be
68      *  non-singular.
69      */
70     unsigned int         _M_version;
71
72     /** Pointer to the previous iterator in the sequence's list of
73         iterators. Only valid when _M_sequence != NULL. */
74     _Safe_iterator_base* _M_prior;
75
76     /** Pointer to the next iterator in the sequence's list of
77         iterators. Only valid when _M_sequence != NULL. */
78     _Safe_iterator_base* _M_next;
79
80   protected:
81     /** Initializes the iterator and makes it singular. */
82     _Safe_iterator_base()
83     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
84     { }
85
86     /** Initialize the iterator to reference the sequence pointed to
87      *  by @p__seq. @p __constant is true when we are initializing a
88      *  constant iterator, and false if it is a mutable iterator. Note
89      *  that @p __seq may be NULL, in which case the iterator will be
90      *  singular. Otherwise, the iterator will reference @p __seq and
91      *  be nonsingular.
92      */
93     _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
94     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
95     { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
96
97     /** Initializes the iterator to reference the same sequence that
98         @p __x does. @p __constant is true if this is a constant
99         iterator, and false if it is mutable. */
100     _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
101     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
102     { this->_M_attach(__x._M_sequence, __constant); }
103
104     _Safe_iterator_base&
105     operator=(const _Safe_iterator_base&);
106
107     explicit
108     _Safe_iterator_base(const _Safe_iterator_base&);
109
110     ~_Safe_iterator_base() { this->_M_detach(); }
111
112     /** For use in _Safe_iterator. */
113     __gnu_cxx::__mutex& _M_get_mutex();
114
115   public:
116     /** Attaches this iterator to the given sequence, detaching it
117      *  from whatever sequence it was attached to originally. If the
118      *  new sequence is the NULL pointer, the iterator is left
119      *  unattached.
120      */
121     void _M_attach(_Safe_sequence_base* __seq, bool __constant);
122
123     /** Likewise, but not thread-safe. */
124     void _M_attach_single(_Safe_sequence_base* __seq, bool __constant);
125
126     /** Detach the iterator for whatever sequence it is attached to,
127      *  if any.
128     */
129     void _M_detach();
130
131     /** Likewise, but not thread-safe. */
132     void _M_detach_single();
133
134     /** Determines if we are attached to the given sequence. */
135     bool _M_attached_to(const _Safe_sequence_base* __seq) const
136     { return _M_sequence == __seq; }
137
138     /** Is this iterator singular? */
139     bool _M_singular() const;
140
141     /** Can we compare this iterator to the given iterator @p __x?
142         Returns true if both iterators are nonsingular and reference
143         the same sequence. */
144     bool _M_can_compare(const _Safe_iterator_base& __x) const;
145   };
146
147   /**
148    * @brief Base class that supports tracking of iterators that
149    * reference a sequence.
150    *
151    * The %_Safe_sequence_base class provides basic support for
152    * tracking iterators into a sequence. Sequences that track
153    * iterators must derived from %_Safe_sequence_base publicly, so
154    * that safe iterators (which inherit _Safe_iterator_base) can
155    * attach to them. This class contains two linked lists of
156    * iterators, one for constant iterators and one for mutable
157    * iterators, and a version number that allows very fast
158    * invalidation of all iterators that reference the container.
159    *
160    * This class must ensure that no operation on it may throw an
161    * exception, otherwise "safe" sequences may fail to provide the
162    * exception-safety guarantees required by the C++ standard.
163    */
164   class _Safe_sequence_base
165   {
166   public:
167     /// The list of mutable iterators that reference this container
168     _Safe_iterator_base* _M_iterators;
169
170     /// The list of constant iterators that reference this container
171     _Safe_iterator_base* _M_const_iterators;
172
173     /// The container version number. This number may never be 0.
174     mutable unsigned int _M_version;
175
176   protected:
177     // Initialize with a version number of 1 and no iterators
178     _Safe_sequence_base()
179     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
180     { }
181
182     /** Notify all iterators that reference this sequence that the
183         sequence is being destroyed. */
184     ~_Safe_sequence_base()
185     { this->_M_detach_all(); }
186
187     /** Detach all iterators, leaving them singular. */
188     void
189     _M_detach_all();
190
191     /** Detach all singular iterators.
192      *  @post for all iterators i attached to this sequence,
193      *   i->_M_version == _M_version.
194      */
195     void
196     _M_detach_singular();
197
198     /** Revalidates all attached singular iterators.  This method may
199      *  be used to validate iterators that were invalidated before
200      *  (but for some reasion, such as an exception, need to become
201      *  valid again).
202      */
203     void
204     _M_revalidate_singular();
205
206     /** Swap this sequence with the given sequence. This operation
207      *  also swaps ownership of the iterators, so that when the
208      *  operation is complete all iterators that originally referenced
209      *  one container now reference the other container.
210      */
211     void
212     _M_swap(_Safe_sequence_base& __x);
213
214     /** For use in _Safe_sequence. */
215     __gnu_cxx::__mutex& _M_get_mutex();
216
217   public:
218     /** Invalidates all iterators. */
219     void
220     _M_invalidate_all() const
221     { if (++_M_version == 0) _M_version = 1; }
222   };
223 } // namespace __gnu_debug
224
225 #endif