1 // Safe iterator implementation -*- C++ -*-
3 // Copyright (C) 2003-2019 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_iterator.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
30 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
32 #include <debug/assertions.h>
33 #include <debug/macros.h>
34 #include <debug/functions.h>
35 #include <debug/safe_base.h>
36 #include <bits/stl_pair.h>
37 #include <ext/type_traits.h>
39 #define _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, _BadMsgId, _DiffMsgId) \
40 _GLIBCXX_DEBUG_VERIFY(!_Lhs._M_singular() && !_Rhs._M_singular(), \
41 _M_message(_BadMsgId) \
42 ._M_iterator(_Lhs, #_Lhs) \
43 ._M_iterator(_Rhs, #_Rhs)); \
44 _GLIBCXX_DEBUG_VERIFY(_Lhs._M_can_compare(_Rhs), \
45 _M_message(_DiffMsgId) \
46 ._M_iterator(_Lhs, #_Lhs) \
47 ._M_iterator(_Rhs, #_Rhs))
49 #define _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(_Lhs, _Rhs) \
50 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_compare_bad, \
51 __msg_compare_different)
53 #define _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(_Lhs, _Rhs) \
54 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_order_bad, \
55 __msg_order_different)
57 #define _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(_Lhs, _Rhs) \
58 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_distance_bad, \
59 __msg_distance_different)
63 /** Helper struct to deal with sequence offering a before_begin
66 template<typename _Sequence
>
67 struct _BeforeBeginHelper
69 template<typename _Iterator
, typename _Category
>
71 _S_Is(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&)
74 template<typename _Iterator
, typename _Category
>
76 _S_Is_Beginnest(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>& __it
)
77 { return __it
.base() == __it
._M_get_sequence()->_M_base().begin(); }
80 /** Sequence traits giving the size of a container if possible. */
81 template<typename _Sequence
>
82 struct _Sequence_traits
84 typedef _Distance_traits
<typename
_Sequence::iterator
> _DistTraits
;
86 static typename
_DistTraits::__type
87 _S_size(const _Sequence
& __seq
)
88 { return std::make_pair(__seq
.size(), __dp_exact
); }
91 /** \brief Safe iterator wrapper.
93 * The class template %_Safe_iterator is a wrapper around an
94 * iterator that tracks the iterator's movement among sequences and
95 * checks that operations performed on the "safe" iterator are
96 * legal. In additional to the basic iterator operations (which are
97 * validated, and then passed to the underlying iterator),
98 * %_Safe_iterator has member functions for iterator invalidation,
99 * attaching/detaching the iterator from sequences, and querying
100 * the iterator's state.
102 * Note that _Iterator must be the first base class so that it gets
103 * initialized before the iterator is being attached to the container's list
104 * of iterators and it is being detached before _Iterator get
105 * destroyed. Otherwise it would result in a data race.
107 template<typename _Iterator
, typename _Sequence
, typename _Category
108 = typename
std::iterator_traits
<_Iterator
>::iterator_category
>
111 public _Safe_iterator_base
113 typedef _Iterator _Iter_base
;
114 typedef _Safe_iterator_base _Safe_base
;
116 typedef std::iterator_traits
<_Iterator
> _Traits
;
119 typedef std::__are_same
<typename
_Sequence::_Base::const_iterator
,
120 _Iterator
> _IsConstant
;
122 typedef typename
__gnu_cxx::__conditional_type
<
123 _IsConstant::__value
,
124 typename
_Sequence::_Base::iterator
,
125 typename
_Sequence::_Base::const_iterator
>::__type _OtherIterator
;
127 struct _Attach_single
130 _Safe_iterator(_Iterator __i
, _Safe_sequence_base
* __seq
, _Attach_single
)
133 { _M_attach_single(__seq
); }
136 typedef _Iterator iterator_type
;
137 typedef typename
_Traits::iterator_category iterator_category
;
138 typedef typename
_Traits::value_type value_type
;
139 typedef typename
_Traits::difference_type difference_type
;
140 typedef typename
_Traits::reference reference
;
141 typedef typename
_Traits::pointer pointer
;
143 #if __cplusplus > 201703L && __cpp_lib_concepts
144 using iterator_concept
= std::__detail::__iter_concept
<_Iterator
>;
147 /// @post the iterator is singular and unattached
148 _Safe_iterator() _GLIBCXX_NOEXCEPT
: _Iter_base() { }
151 * @brief Safe iterator construction from an unsafe iterator and
154 * @pre @p seq is not NULL
155 * @post this is not singular
157 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
159 : _Iter_base(__i
), _Safe_base(__seq
, _S_constant())
161 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
162 _M_message(__msg_init_singular
)
163 ._M_iterator(*this, "this"));
167 * @brief Copy construction.
169 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
170 : _Iter_base(__x
.base())
172 // _GLIBCXX_RESOLVE_LIB_DEFECTS
173 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
174 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
175 || __x
.base() == _Iterator(),
176 _M_message(__msg_init_copy_singular
)
177 ._M_iterator(*this, "this")
178 ._M_iterator(__x
, "other"));
179 _M_attach(__x
._M_sequence
);
182 #if __cplusplus >= 201103L
184 * @brief Move construction.
185 * @post __x is singular and unattached
187 _Safe_iterator(_Safe_iterator
&& __x
) noexcept
190 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
191 || __x
.base() == _Iterator(),
192 _M_message(__msg_init_copy_singular
)
193 ._M_iterator(*this, "this")
194 ._M_iterator(__x
, "other"));
195 _Safe_sequence_base
* __seq
= __x
._M_sequence
;
197 std::swap(base(), __x
.base());
203 * @brief Converting constructor from a mutable iterator to a
206 template<typename _MutableIterator
>
208 const _Safe_iterator
<_MutableIterator
, _Sequence
,
209 typename
__gnu_cxx::__enable_if
<_IsConstant::__value
&&
210 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
211 _Category
>::__type
>& __x
)
213 : _Iter_base(__x
.base())
215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
216 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
217 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
218 || __x
.base() == _MutableIterator(),
219 _M_message(__msg_init_const_singular
)
220 ._M_iterator(*this, "this")
221 ._M_iterator(__x
, "other"));
222 _M_attach(__x
._M_sequence
);
226 * @brief Copy assignment.
229 operator=(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
232 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
233 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
234 || __x
.base() == _Iterator(),
235 _M_message(__msg_copy_singular
)
236 ._M_iterator(*this, "this")
237 ._M_iterator(__x
, "other"));
239 if (this->_M_sequence
&& this->_M_sequence
== __x
._M_sequence
)
241 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
243 _M_version
= __x
._M_sequence
->_M_version
;
249 _M_attach(__x
._M_sequence
);
255 #if __cplusplus >= 201103L
257 * @brief Move assignment.
258 * @post __x is singular and unattached
261 operator=(_Safe_iterator
&& __x
) noexcept
263 _GLIBCXX_DEBUG_VERIFY(this != &__x
,
264 _M_message(__msg_self_move_assign
)
265 ._M_iterator(*this, "this"));
266 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
267 || __x
.base() == _Iterator(),
268 _M_message(__msg_copy_singular
)
269 ._M_iterator(*this, "this")
270 ._M_iterator(__x
, "other"));
272 if (this->_M_sequence
&& this->_M_sequence
== __x
._M_sequence
)
274 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
276 _M_version
= __x
._M_sequence
->_M_version
;
282 _M_attach(__x
._M_sequence
);
286 __x
.base() = _Iterator();
292 * @brief Iterator dereference.
293 * @pre iterator is dereferenceable
296 operator*() const _GLIBCXX_NOEXCEPT
298 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
299 _M_message(__msg_bad_deref
)
300 ._M_iterator(*this, "this"));
305 * @brief Iterator dereference.
306 * @pre iterator is dereferenceable
309 operator->() const _GLIBCXX_NOEXCEPT
311 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
312 _M_message(__msg_bad_deref
)
313 ._M_iterator(*this, "this"));
314 return base().operator->();
317 // ------ Input iterator requirements ------
319 * @brief Iterator preincrement
320 * @pre iterator is incrementable
323 operator++() _GLIBCXX_NOEXCEPT
325 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
326 _M_message(__msg_bad_inc
)
327 ._M_iterator(*this, "this"));
328 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
334 * @brief Iterator postincrement
335 * @pre iterator is incrementable
338 operator++(int) _GLIBCXX_NOEXCEPT
340 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
341 _M_message(__msg_bad_inc
)
342 ._M_iterator(*this, "this"));
343 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
344 return _Safe_iterator(base()++, this->_M_sequence
, _Attach_single());
347 // ------ Utilities ------
349 /// Determine if this is a constant iterator.
350 static _GLIBCXX_CONSTEXPR
bool
352 { return _IsConstant::__value
; }
355 * @brief Return the underlying iterator
358 base() _GLIBCXX_NOEXCEPT
{ return *this; }
361 base() const _GLIBCXX_NOEXCEPT
{ return *this; }
364 * @brief Conversion to underlying non-debug iterator to allow
365 * better interaction with non-debug containers.
367 operator _Iterator() const _GLIBCXX_NOEXCEPT
{ return *this; }
369 /** Attach iterator to the given sequence. */
371 _M_attach(_Safe_sequence_base
* __seq
)
372 { _Safe_base::_M_attach(__seq
, _S_constant()); }
374 /** Likewise, but not thread-safe. */
376 _M_attach_single(_Safe_sequence_base
* __seq
)
377 { _Safe_base::_M_attach_single(__seq
, _S_constant()); }
379 /// Is the iterator dereferenceable?
381 _M_dereferenceable() const
382 { return !this->_M_singular() && !_M_is_end() && !_M_is_before_begin(); }
384 /// Is the iterator before a dereferenceable one?
386 _M_before_dereferenceable() const
388 if (this->_M_incrementable())
390 _Iterator __base
= base();
391 return ++__base
!= _M_get_sequence()->_M_base().end();
396 /// Is the iterator incrementable?
398 _M_incrementable() const
399 { return !this->_M_singular() && !_M_is_end(); }
401 // Can we advance the iterator @p __n steps (@p __n may be negative)
403 _M_can_advance(difference_type __n
, bool __strict
= false) const;
405 // Is the iterator range [*this, __rhs) valid?
407 _M_valid_range(const _Safe_iterator
& __rhs
,
408 std::pair
<difference_type
, _Distance_precision
>& __dist
,
409 bool __check_dereferenceable
= true) const;
411 // The sequence this iterator references.
412 typename
__gnu_cxx::__conditional_type
<
413 _IsConstant::__value
, const _Sequence
*, _Sequence
*>::__type
414 _M_get_sequence() const
415 { return static_cast<_Sequence
*>(_M_sequence
); }
417 // Get distance to __rhs.
418 typename _Distance_traits
<_Iterator
>::__type
419 _M_get_distance_to(const _Safe_iterator
& __rhs
) const;
421 // Get distance from sequence begin up to *this.
422 typename _Distance_traits
<_Iterator
>::__type
423 _M_get_distance_from_begin() const;
425 // Get distance from *this to sequence end.
426 typename _Distance_traits
<_Iterator
>::__type
427 _M_get_distance_to_end() const;
429 /// Is this iterator equal to the sequence's begin() iterator?
432 { return base() == _M_get_sequence()->_M_base().begin(); }
434 /// Is this iterator equal to the sequence's end() iterator?
437 { return base() == _M_get_sequence()->_M_base().end(); }
439 /// Is this iterator equal to the sequence's before_begin() iterator if
442 _M_is_before_begin() const
443 { return _BeforeBeginHelper
<_Sequence
>::_S_Is(*this); }
445 /// Is this iterator equal to the sequence's before_begin() iterator if
446 /// any or begin() otherwise?
448 _M_is_beginnest() const
449 { return _BeforeBeginHelper
<_Sequence
>::_S_Is_Beginnest(*this); }
451 // ------ Operators ------
453 typedef _Safe_iterator
<_Iterator
, _Sequence
, iterator_category
> _Self
;
456 operator==(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
458 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
459 return __lhs
.base() == __rhs
.base();
462 template<typename _IteR
>
464 operator==(const _Self
& __lhs
,
465 const _Safe_iterator
<_IteR
, _Sequence
, iterator_category
>& __rhs
)
468 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
469 return __lhs
.base() == __rhs
.base();
473 operator!=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
475 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
476 return __lhs
.base() != __rhs
.base();
479 template<typename _IteR
>
481 operator!=(const _Self
& __lhs
,
482 const _Safe_iterator
<_IteR
, _Sequence
, iterator_category
>& __rhs
)
485 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
486 return __lhs
.base() != __rhs
.base();
490 template<typename _Iterator
, typename _Sequence
>
491 class _Safe_iterator
<_Iterator
, _Sequence
, std::bidirectional_iterator_tag
>
492 : public _Safe_iterator
<_Iterator
, _Sequence
, std::forward_iterator_tag
>
494 typedef _Safe_iterator
<_Iterator
, _Sequence
,
495 std::forward_iterator_tag
> _Safe_base
;
498 typedef typename
_Safe_base::_OtherIterator _OtherIterator
;
499 typedef typename
_Safe_base::_Attach_single _Attach_single
;
501 _Safe_iterator(_Iterator __i
, _Safe_sequence_base
* __seq
, _Attach_single
)
503 : _Safe_base(__i
, __seq
, _Attach_single())
507 /// @post the iterator is singular and unattached
508 _Safe_iterator() _GLIBCXX_NOEXCEPT
{ }
511 * @brief Safe iterator construction from an unsafe iterator and
514 * @pre @p seq is not NULL
515 * @post this is not singular
517 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
519 : _Safe_base(__i
, __seq
)
523 * @brief Copy construction.
525 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
529 #if __cplusplus >= 201103L
530 /** @brief Move construction. */
531 _Safe_iterator(_Safe_iterator
&&) = default;
535 * @brief Converting constructor from a mutable iterator to a
538 template<typename _MutableIterator
>
540 const _Safe_iterator
<_MutableIterator
, _Sequence
,
541 typename
__gnu_cxx::__enable_if
<_Safe_base::_IsConstant::__value
&&
542 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
543 std::bidirectional_iterator_tag
>::__type
>& __x
)
548 #if __cplusplus >= 201103L
549 /** @brief Copy assignment. */
551 operator=(const _Safe_iterator
&) = default;
553 /** @brief Move assignment. */
555 operator=(_Safe_iterator
&&) = default;
557 /** @brief Copy assignment. */
559 operator=(const _Safe_iterator
& __x
)
561 _Safe_base::operator=(__x
);
566 // ------ Input iterator requirements ------
568 * @brief Iterator preincrement
569 * @pre iterator is incrementable
572 operator++() _GLIBCXX_NOEXCEPT
574 _Safe_base::operator++();
579 * @brief Iterator postincrement
580 * @pre iterator is incrementable
583 operator++(int) _GLIBCXX_NOEXCEPT
585 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
586 _M_message(__msg_bad_inc
)
587 ._M_iterator(*this, "this"));
588 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
589 return _Safe_iterator(this->base()++, this->_M_sequence
,
593 // ------ Bidirectional iterator requirements ------
595 * @brief Iterator predecrement
596 * @pre iterator is decrementable
599 operator--() _GLIBCXX_NOEXCEPT
601 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
602 _M_message(__msg_bad_dec
)
603 ._M_iterator(*this, "this"));
604 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
610 * @brief Iterator postdecrement
611 * @pre iterator is decrementable
614 operator--(int) _GLIBCXX_NOEXCEPT
616 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
617 _M_message(__msg_bad_dec
)
618 ._M_iterator(*this, "this"));
619 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
620 return _Safe_iterator(this->base()--, this->_M_sequence
,
624 // ------ Utilities ------
626 // Is the iterator decrementable?
628 _M_decrementable() const
629 { return !this->_M_singular() && !this->_M_is_begin(); }
632 template<typename _Iterator
, typename _Sequence
>
633 class _Safe_iterator
<_Iterator
, _Sequence
, std::random_access_iterator_tag
>
634 : public _Safe_iterator
<_Iterator
, _Sequence
,
635 std::bidirectional_iterator_tag
>
637 typedef _Safe_iterator
<_Iterator
, _Sequence
,
638 std::bidirectional_iterator_tag
> _Safe_base
;
639 typedef typename
_Safe_base::_OtherIterator _OtherIterator
;
641 typedef typename
_Safe_base::_Self _Self
;
642 typedef _Safe_iterator
<_OtherIterator
, _Sequence
,
643 std::random_access_iterator_tag
> _OtherSelf
;
645 typedef typename
_Safe_base::_Attach_single _Attach_single
;
647 _Safe_iterator(_Iterator __i
, _Safe_sequence_base
* __seq
, _Attach_single
)
649 : _Safe_base(__i
, __seq
, _Attach_single())
653 typedef typename
_Safe_base::difference_type difference_type
;
654 typedef typename
_Safe_base::reference reference
;
656 /// @post the iterator is singular and unattached
657 _Safe_iterator() _GLIBCXX_NOEXCEPT
{ }
660 * @brief Safe iterator construction from an unsafe iterator and
663 * @pre @p seq is not NULL
664 * @post this is not singular
666 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
668 : _Safe_base(__i
, __seq
)
672 * @brief Copy construction.
674 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
678 #if __cplusplus >= 201103L
679 /** @brief Move construction. */
680 _Safe_iterator(_Safe_iterator
&&) = default;
684 * @brief Converting constructor from a mutable iterator to a
687 template<typename _MutableIterator
>
689 const _Safe_iterator
<_MutableIterator
, _Sequence
,
690 typename
__gnu_cxx::__enable_if
<_Safe_base::_IsConstant::__value
&&
691 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
692 std::random_access_iterator_tag
>::__type
>& __x
)
697 #if __cplusplus >= 201103L
698 /** @brief Copy assignment. */
700 operator=(const _Safe_iterator
&) = default;
702 /** @brief Move assignment. */
704 operator=(_Safe_iterator
&&) = default;
706 /** @brief Copy assignment. */
708 operator=(const _Safe_iterator
& __x
)
710 _Safe_base::operator=(__x
);
715 // Is the iterator range [*this, __rhs) valid?
717 _M_valid_range(const _Safe_iterator
& __rhs
,
718 std::pair
<difference_type
,
719 _Distance_precision
>& __dist
) const;
721 // ------ Input iterator requirements ------
723 * @brief Iterator preincrement
724 * @pre iterator is incrementable
727 operator++() _GLIBCXX_NOEXCEPT
729 _Safe_base::operator++();
734 * @brief Iterator postincrement
735 * @pre iterator is incrementable
738 operator++(int) _GLIBCXX_NOEXCEPT
740 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
741 _M_message(__msg_bad_inc
)
742 ._M_iterator(*this, "this"));
743 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
744 return _Safe_iterator(this->base()++, this->_M_sequence
,
748 // ------ Bidirectional iterator requirements ------
750 * @brief Iterator predecrement
751 * @pre iterator is decrementable
754 operator--() _GLIBCXX_NOEXCEPT
756 _Safe_base::operator--();
761 * @brief Iterator postdecrement
762 * @pre iterator is decrementable
765 operator--(int) _GLIBCXX_NOEXCEPT
767 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
768 _M_message(__msg_bad_dec
)
769 ._M_iterator(*this, "this"));
770 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
771 return _Safe_iterator(this->base()--, this->_M_sequence
,
775 // ------ Random access iterator requirements ------
777 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
779 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n
)
780 && this->_M_can_advance(__n
+ 1),
781 _M_message(__msg_iter_subscript_oob
)
782 ._M_iterator(*this)._M_integer(__n
));
783 return this->base()[__n
];
787 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
789 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n
),
790 _M_message(__msg_advance_oob
)
791 ._M_iterator(*this)._M_integer(__n
));
792 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
798 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
800 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n
),
801 _M_message(__msg_retreat_oob
)
802 ._M_iterator(*this)._M_integer(__n
));
803 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
809 operator<(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
811 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
812 return __lhs
.base() < __rhs
.base();
816 operator<(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
818 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
819 return __lhs
.base() < __rhs
.base();
823 operator<=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
825 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
826 return __lhs
.base() <= __rhs
.base();
830 operator<=(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
832 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
833 return __lhs
.base() <= __rhs
.base();
837 operator>(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
839 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
840 return __lhs
.base() > __rhs
.base();
844 operator>(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
846 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
847 return __lhs
.base() > __rhs
.base();
851 operator>=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
853 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
854 return __lhs
.base() >= __rhs
.base();
858 operator>=(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
860 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
861 return __lhs
.base() >= __rhs
.base();
864 // _GLIBCXX_RESOLVE_LIB_DEFECTS
865 // According to the resolution of DR179 not only the various comparison
866 // operators but also operator- must accept mixed iterator/const_iterator
868 friend difference_type
869 operator-(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
871 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs
, __rhs
);
872 return __lhs
.base() - __rhs
.base();
875 friend difference_type
876 operator-(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
878 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs
, __rhs
);
879 return __lhs
.base() - __rhs
.base();
883 operator+(const _Self
& __x
, difference_type __n
) _GLIBCXX_NOEXCEPT
885 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(__n
),
886 _M_message(__msg_advance_oob
)
887 ._M_iterator(__x
)._M_integer(__n
));
888 return _Safe_iterator(__x
.base() + __n
, __x
._M_sequence
);
892 operator+(difference_type __n
, const _Self
& __x
) _GLIBCXX_NOEXCEPT
894 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(__n
),
895 _M_message(__msg_advance_oob
)
896 ._M_iterator(__x
)._M_integer(__n
));
897 return _Safe_iterator(__n
+ __x
.base(), __x
._M_sequence
);
901 operator-(const _Self
& __x
, difference_type __n
) _GLIBCXX_NOEXCEPT
903 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(-__n
),
904 _M_message(__msg_retreat_oob
)
905 ._M_iterator(__x
)._M_integer(__n
));
906 return _Safe_iterator(__x
.base() - __n
, __x
._M_sequence
);
910 /** Safe iterators know how to check if they form a valid range. */
911 template<typename _Iterator
, typename _Sequence
, typename _Category
>
913 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
,
915 const _Safe_iterator
<_Iterator
, _Sequence
,
917 typename _Distance_traits
<_Iterator
>::__type
& __dist
)
918 { return __first
._M_valid_range(__last
, __dist
); }
920 template<typename _Iterator
, typename _Sequence
, typename _Category
>
922 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
,
924 const _Safe_iterator
<_Iterator
, _Sequence
,
927 typename _Distance_traits
<_Iterator
>::__type __dist
;
928 return __first
._M_valid_range(__last
, __dist
);
931 template<typename _Iterator
, typename _Sequence
, typename _Category
,
934 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>& __it
,
936 { return __it
._M_can_advance(__n
); }
938 template<typename _Iterator
, typename _Sequence
>
940 __base(const _Safe_iterator
<_Iterator
, _Sequence
,
941 std::random_access_iterator_tag
>& __it
)
942 { return __it
.base(); }
944 #if __cplusplus < 201103L
945 template<typename _Iterator
, typename _Sequence
>
946 struct _Unsafe_type
<_Safe_iterator
<_Iterator
, _Sequence
> >
947 { typedef _Iterator _Type
; };
950 template<typename _Iterator
, typename _Sequence
>
952 __unsafe(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
)
953 { return __it
.base(); }
955 } // namespace __gnu_debug
957 #undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
958 #undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
959 #undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
960 #undef _GLIBCXX_DEBUG_VERIFY_OPERANDS
962 #include <debug/safe_iterator.tcc>