1 // Safe iterator implementation -*- C++ -*-
3 // Copyright (C) 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/safe_local_iterator.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_SAFE_LOCAL_ITERATOR_H
30 #define _GLIBCXX_DEBUG_SAFE_LOCAL_ITERATOR_H 1
32 #include <debug/debug.h>
33 #include <debug/macros.h>
34 #include <debug/functions.h>
35 #include <debug/safe_unordered_base.h>
36 #include <ext/type_traits.h>
40 /** \brief Safe iterator wrapper.
42 * The class template %_Safe_local_iterator is a wrapper around an
43 * iterator that tracks the iterator's movement among sequences and
44 * checks that operations performed on the "safe" iterator are
45 * legal. In additional to the basic iterator operations (which are
46 * validated, and then passed to the underlying iterator),
47 * %_Safe_local_iterator has member functions for iterator invalidation,
48 * attaching/detaching the iterator from sequences, and querying
49 * the iterator's state.
51 template<typename _Iterator, typename _Sequence>
52 class _Safe_local_iterator : public _Safe_local_iterator_base
54 typedef _Safe_local_iterator _Self;
55 typedef typename _Sequence::size_type size_type;
57 /// The underlying iterator
60 /// The bucket this local iterator belongs to
63 /// Determine if this is a constant iterator.
67 typedef typename _Sequence::const_local_iterator const_iterator;
68 return std::__are_same<const_iterator, _Safe_local_iterator>::__value;
71 typedef std::iterator_traits<_Iterator> _Traits;
74 typedef _Iterator iterator_type;
75 typedef typename _Traits::iterator_category iterator_category;
76 typedef typename _Traits::value_type value_type;
77 typedef typename _Traits::difference_type difference_type;
78 typedef typename _Traits::reference reference;
79 typedef typename _Traits::pointer pointer;
81 /// @post the iterator is singular and unattached
82 _Safe_local_iterator() : _M_current() { }
85 * @brief Safe iterator construction from an unsafe iterator and
88 * @pre @p seq is not NULL
89 * @post this is not singular
91 _Safe_local_iterator(const _Iterator& __i, size_type __bucket,
92 const _Sequence* __seq)
93 : _Safe_local_iterator_base(__seq, _M_constant()), _M_current(__i),
96 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
97 _M_message(__msg_init_singular)
98 ._M_iterator(*this, "this"));
102 * @brief Copy construction.
104 _Safe_local_iterator(const _Safe_local_iterator& __x)
105 : _Safe_local_iterator_base(__x, _M_constant()),
106 _M_current(__x._M_current), _M_bucket(__x._M_bucket)
108 // _GLIBCXX_RESOLVE_LIB_DEFECTS
109 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
110 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
111 || __x._M_current == _Iterator(),
112 _M_message(__msg_init_copy_singular)
113 ._M_iterator(*this, "this")
114 ._M_iterator(__x, "other"));
118 * @brief Converting constructor from a mutable iterator to a
121 template<typename _MutableIterator>
122 _Safe_local_iterator(
123 const _Safe_local_iterator<_MutableIterator,
124 typename __gnu_cxx::__enable_if<std::__are_same<
126 typename _Sequence::local_iterator::iterator_type>::__value,
127 _Sequence>::__type>& __x)
128 : _Safe_local_iterator_base(__x, _M_constant()),
129 _M_current(__x.base()), _M_bucket(__x._M_bucket)
131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
132 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
133 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
134 || __x.base() == _Iterator(),
135 _M_message(__msg_init_const_singular)
136 ._M_iterator(*this, "this")
137 ._M_iterator(__x, "other"));
141 * @brief Copy assignment.
143 _Safe_local_iterator&
144 operator=(const _Safe_local_iterator& __x)
146 // _GLIBCXX_RESOLVE_LIB_DEFECTS
147 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
148 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
149 || __x._M_current == _Iterator(),
150 _M_message(__msg_copy_singular)
151 ._M_iterator(*this, "this")
152 ._M_iterator(__x, "other"));
153 _M_current = __x._M_current;
154 _M_bucket = __x._M_bucket;
155 this->_M_attach(__x._M_sequence);
160 * @brief Iterator dereference.
161 * @pre iterator is dereferenceable
166 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
167 _M_message(__msg_bad_deref)
168 ._M_iterator(*this, "this"));
173 * @brief Iterator dereference.
174 * @pre iterator is dereferenceable
175 * @todo Make this correct w.r.t. iterators that return proxies
176 * @todo Use addressof() instead of & operator
181 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
182 _M_message(__msg_bad_deref)
183 ._M_iterator(*this, "this"));
187 // ------ Input iterator requirements ------
189 * @brief Iterator preincrement
190 * @pre iterator is incrementable
192 _Safe_local_iterator&
195 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
196 _M_message(__msg_bad_inc)
197 ._M_iterator(*this, "this"));
203 * @brief Iterator postincrement
204 * @pre iterator is incrementable
209 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
210 _M_message(__msg_bad_inc)
211 ._M_iterator(*this, "this"));
212 _Safe_local_iterator __tmp(*this);
217 // ------ Utilities ------
219 * @brief Return the underlying iterator
222 base() const { return _M_current; }
225 * @brief Return the bucket
228 bucket() const { return _M_bucket; }
231 * @brief Conversion to underlying non-debug iterator to allow
232 * better interaction with non-debug containers.
234 operator _Iterator() const { return _M_current; }
236 /** Attach iterator to the given sequence. */
238 _M_attach(_Safe_sequence_base* __seq)
239 { _Safe_iterator_base::_M_attach(__seq, _M_constant()); }
241 /** Likewise, but not thread-safe. */
243 _M_attach_single(_Safe_sequence_base* __seq)
244 { _Safe_iterator_base::_M_attach_single(__seq, _M_constant()); }
246 /// Is the iterator dereferenceable?
248 _M_dereferenceable() const
249 { return !this->_M_singular() && !_M_is_end(); }
251 /// Is the iterator incrementable?
253 _M_incrementable() const
254 { return !this->_M_singular() && !_M_is_end(); }
256 // Is the iterator range [*this, __rhs) valid?
257 template<typename _Other>
259 _M_valid_range(const _Safe_local_iterator<_Other,
260 _Sequence>& __rhs) const;
262 // The sequence this iterator references.
264 _M_get_sequence() const
265 { return static_cast<const _Sequence*>(_M_sequence); }
267 /// Is this iterator equal to the sequence's begin() iterator?
268 bool _M_is_begin() const
269 { return base() == _M_get_sequence()->_M_base().begin(_M_bucket); }
271 /// Is this iterator equal to the sequence's end() iterator?
272 bool _M_is_end() const
273 { return base() == _M_get_sequence()->_M_base().end(_M_bucket); }
275 /// Is this iterator part of the same bucket as the other one?
276 template <typename _Other>
277 bool _M_in_same_bucket(const _Safe_local_iterator<_Other,
278 _Sequence>& __other) const
279 { return _M_bucket == __other.bucket(); }
282 template<typename _IteratorL, typename _IteratorR, typename _Sequence>
284 operator==(const _Safe_local_iterator<_IteratorL, _Sequence>& __lhs,
285 const _Safe_local_iterator<_IteratorR, _Sequence>& __rhs)
287 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
288 _M_message(__msg_iter_compare_bad)
289 ._M_iterator(__lhs, "lhs")
290 ._M_iterator(__rhs, "rhs"));
291 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
292 _M_message(__msg_compare_different)
293 ._M_iterator(__lhs, "lhs")
294 ._M_iterator(__rhs, "rhs"));
295 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
296 _M_message(__msg_compare_different)
297 ._M_iterator(__lhs, "lhs")
298 ._M_iterator(__rhs, "rhs"));
299 _GLIBCXX_DEBUG_VERIFY(__lhs._M_in_same_bucket(__rhs),
300 _M_message(__msg_local_iter_compare_bad)
301 ._M_iterator(__lhs, "lhs")
302 ._M_iterator(__rhs, "rhs"));
303 return __lhs.base() == __rhs.base();
306 template<typename _Iterator, typename _Sequence>
308 operator==(const _Safe_local_iterator<_Iterator, _Sequence>& __lhs,
309 const _Safe_local_iterator<_Iterator, _Sequence>& __rhs)
311 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
312 _M_message(__msg_iter_compare_bad)
313 ._M_iterator(__lhs, "lhs")
314 ._M_iterator(__rhs, "rhs"));
315 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
316 _M_message(__msg_compare_different)
317 ._M_iterator(__lhs, "lhs")
318 ._M_iterator(__rhs, "rhs"));
319 _GLIBCXX_DEBUG_VERIFY(__lhs._M_in_same_bucket(__rhs),
320 _M_message(__msg_local_iter_compare_bad)
321 ._M_iterator(__lhs, "lhs")
322 ._M_iterator(__rhs, "rhs"));
323 return __lhs.base() == __rhs.base();
326 template<typename _IteratorL, typename _IteratorR, typename _Sequence>
328 operator!=(const _Safe_local_iterator<_IteratorL, _Sequence>& __lhs,
329 const _Safe_local_iterator<_IteratorR, _Sequence>& __rhs)
331 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
332 _M_message(__msg_iter_compare_bad)
333 ._M_iterator(__lhs, "lhs")
334 ._M_iterator(__rhs, "rhs"));
335 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
336 _M_message(__msg_compare_different)
337 ._M_iterator(__lhs, "lhs")
338 ._M_iterator(__rhs, "rhs"));
339 _GLIBCXX_DEBUG_VERIFY(__lhs._M_in_same_bucket(__rhs),
340 _M_message(__msg_local_iter_compare_bad)
341 ._M_iterator(__lhs, "lhs")
342 ._M_iterator(__rhs, "rhs"));
343 return __lhs.base() != __rhs.base();
346 template<typename _Iterator, typename _Sequence>
348 operator!=(const _Safe_local_iterator<_Iterator, _Sequence>& __lhs,
349 const _Safe_local_iterator<_Iterator, _Sequence>& __rhs)
351 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
352 _M_message(__msg_iter_compare_bad)
353 ._M_iterator(__lhs, "lhs")
354 ._M_iterator(__rhs, "rhs"));
355 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
356 _M_message(__msg_compare_different)
357 ._M_iterator(__lhs, "lhs")
358 ._M_iterator(__rhs, "rhs"));
359 _GLIBCXX_DEBUG_VERIFY(__lhs._M_in_same_bucket(__rhs),
360 _M_message(__msg_local_iter_compare_bad)
361 ._M_iterator(__lhs, "lhs")
362 ._M_iterator(__rhs, "rhs"));
363 return __lhs.base() != __rhs.base();
365 } // namespace __gnu_debug
367 #include <debug/safe_local_iterator.tcc>