1 // Safe iterator implementation -*- C++ -*-
3 // Copyright (C) 2003-2023 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>
38 #if __cplusplus > 201703L
42 #define _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, _BadMsgId, _DiffMsgId) \
43 _GLIBCXX_DEBUG_VERIFY((!_Lhs._M_singular() && !_Rhs._M_singular()) \
44 || (_Lhs._M_value_initialized() \
45 && _Rhs._M_value_initialized()), \
46 _M_message(_BadMsgId) \
47 ._M_iterator(_Lhs, #_Lhs) \
48 ._M_iterator(_Rhs, #_Rhs)); \
49 _GLIBCXX_DEBUG_VERIFY(_Lhs._M_can_compare(_Rhs), \
50 _M_message(_DiffMsgId) \
51 ._M_iterator(_Lhs, #_Lhs) \
52 ._M_iterator(_Rhs, #_Rhs))
54 #define _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(_Lhs, _Rhs) \
55 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_compare_bad, \
56 __msg_compare_different)
58 #define _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(_Lhs, _Rhs) \
59 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_order_bad, \
60 __msg_order_different)
62 #define _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(_Lhs, _Rhs) \
63 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_distance_bad, \
64 __msg_distance_different)
68 /** Helper struct to deal with sequence offering a before_begin
71 template<typename _Sequence
>
72 struct _BeforeBeginHelper
74 template<typename _Iterator
, typename _Category
>
76 _S_Is(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&)
79 template<typename _Iterator
, typename _Category
>
81 _S_Is_Beginnest(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>& __it
)
82 { return __it
.base() == __it
._M_get_sequence()->_M_base().begin(); }
85 /** Sequence traits giving the size of a container if possible. */
86 template<typename _Sequence
>
87 struct _Sequence_traits
89 typedef _Distance_traits
<typename
_Sequence::iterator
> _DistTraits
;
91 static typename
_DistTraits::__type
92 _S_size(const _Sequence
& __seq
)
93 { return std::make_pair(__seq
.size(), __dp_exact
); }
96 /** \brief Safe iterator wrapper.
98 * The class template %_Safe_iterator is a wrapper around an
99 * iterator that tracks the iterator's movement among sequences and
100 * checks that operations performed on the "safe" iterator are
101 * legal. In additional to the basic iterator operations (which are
102 * validated, and then passed to the underlying iterator),
103 * %_Safe_iterator has member functions for iterator invalidation,
104 * attaching/detaching the iterator from sequences, and querying
105 * the iterator's state.
107 * Note that _Iterator must be the first base class so that it gets
108 * initialized before the iterator is being attached to the container's list
109 * of iterators and it is being detached before _Iterator get
110 * destroyed. Otherwise it would result in a data race.
112 template<typename _Iterator
, typename _Sequence
, typename _Category
113 = typename
std::iterator_traits
<_Iterator
>::iterator_category
>
116 public _Safe_iterator_base
118 typedef _Iterator _Iter_base
;
119 typedef _Safe_iterator_base _Safe_base
;
121 typedef std::iterator_traits
<_Iterator
> _Traits
;
124 typedef std::__are_same
<typename
_Sequence::_Base::const_iterator
,
125 _Iterator
> _IsConstant
;
127 typedef typename
__gnu_cxx::__conditional_type
<
128 _IsConstant::__value
,
129 typename
_Sequence::_Base::iterator
,
130 typename
_Sequence::_Base::const_iterator
>::__type _OtherIterator
;
133 typedef _Iterator iterator_type
;
134 typedef typename
_Traits::iterator_category iterator_category
;
135 typedef typename
_Traits::value_type value_type
;
136 typedef typename
_Traits::difference_type difference_type
;
137 typedef typename
_Traits::reference reference
;
138 typedef typename
_Traits::pointer pointer
;
140 #if __cplusplus > 201703L && __cpp_lib_concepts
141 using iterator_concept
= std::__detail::__iter_concept
<_Iterator
>;
144 /// @post the iterator is singular and unattached
145 _Safe_iterator() _GLIBCXX_NOEXCEPT
: _Iter_base() { }
148 * @brief Safe iterator construction from an unsafe iterator and
151 * @pre @p seq is not NULL
152 * @post this is not singular
154 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
156 : _Iter_base(__i
), _Safe_base(__seq
, _S_constant())
158 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
159 _M_message(__msg_init_singular
)
160 ._M_iterator(*this, "this"));
164 * @brief Copy construction.
166 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
167 : _Iter_base(__x
.base()), _Safe_base()
169 // _GLIBCXX_RESOLVE_LIB_DEFECTS
170 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
171 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
172 || __x
._M_value_initialized(),
173 _M_message(__msg_init_copy_singular
)
174 ._M_iterator(*this, "this")
175 ._M_iterator(__x
, "other"));
176 _M_attach(__x
._M_sequence
);
179 #if __cplusplus >= 201103L
181 * @brief Move construction.
182 * @post __x is singular and unattached
184 _Safe_iterator(_Safe_iterator
&& __x
) noexcept
187 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
188 || __x
._M_value_initialized(),
189 _M_message(__msg_init_copy_singular
)
190 ._M_iterator(*this, "this")
191 ._M_iterator(__x
, "other"));
192 _Safe_sequence_base
* __seq
= __x
._M_sequence
;
194 std::swap(base(), __x
.base());
200 * @brief Converting constructor from a mutable iterator to a
203 template<typename _MutableIterator
>
205 const _Safe_iterator
<_MutableIterator
, _Sequence
,
206 typename
__gnu_cxx::__enable_if
<_IsConstant::__value
&&
207 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
208 _Category
>::__type
>& __x
)
210 : _Iter_base(__x
.base())
212 // _GLIBCXX_RESOLVE_LIB_DEFECTS
213 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
214 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
215 || __x
._M_value_initialized(),
216 _M_message(__msg_init_const_singular
)
217 ._M_iterator(*this, "this")
218 ._M_iterator(__x
, "other"));
219 _M_attach(__x
._M_sequence
);
223 * @brief Copy assignment.
226 operator=(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
230 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
231 || __x
._M_value_initialized(),
232 _M_message(__msg_copy_singular
)
233 ._M_iterator(*this, "this")
234 ._M_iterator(__x
, "other"));
236 if (this->_M_sequence
&& this->_M_sequence
== __x
._M_sequence
)
238 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
240 _M_version
= __x
._M_sequence
->_M_version
;
246 _M_attach(__x
._M_sequence
);
252 #if __cplusplus >= 201103L
254 * @brief Move assignment.
255 * @post __x is singular and unattached
258 operator=(_Safe_iterator
&& __x
) noexcept
260 _GLIBCXX_DEBUG_VERIFY(!__x
._M_singular()
261 || __x
._M_value_initialized(),
262 _M_message(__msg_copy_singular
)
263 ._M_iterator(*this, "this")
264 ._M_iterator(__x
, "other"));
266 if (std::__addressof(__x
) == this)
269 if (this->_M_sequence
&& this->_M_sequence
== __x
._M_sequence
)
271 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
273 _M_version
= __x
._M_sequence
->_M_version
;
279 _M_attach(__x
._M_sequence
);
283 __x
.base() = _Iterator();
289 * @brief Iterator dereference.
290 * @pre iterator is dereferenceable
294 operator*() const _GLIBCXX_NOEXCEPT
296 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
297 _M_message(__msg_bad_deref
)
298 ._M_iterator(*this, "this"));
303 * @brief Iterator dereference.
304 * @pre iterator is dereferenceable
308 operator->() const _GLIBCXX_NOEXCEPT
310 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
311 _M_message(__msg_bad_deref
)
312 ._M_iterator(*this, "this"));
313 return base().operator->();
316 // ------ Input iterator requirements ------
318 * @brief Iterator preincrement
319 * @pre iterator is incrementable
322 operator++() _GLIBCXX_NOEXCEPT
324 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
325 _M_message(__msg_bad_inc
)
326 ._M_iterator(*this, "this"));
327 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
333 * @brief Iterator postincrement
334 * @pre iterator is incrementable
337 operator++(int) _GLIBCXX_NOEXCEPT
339 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
340 _M_message(__msg_bad_inc
)
341 ._M_iterator(*this, "this"));
342 _Safe_iterator __ret
= *this;
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 /// Is the iterator value-initialized?
403 _M_value_initialized() const
404 { return _M_version
== 0 && base() == _Iter_base(); }
406 // Can we advance the iterator @p __n steps (@p __n may be negative)
408 _M_can_advance(difference_type __n
, bool __strict
= false) const;
410 // Can we advance the iterator using @p __dist in @p __way direction.
411 template<typename _Diff
>
413 _M_can_advance(const std::pair
<_Diff
, _Distance_precision
>& __dist
,
416 // Is the iterator range [*this, __rhs) valid?
418 _M_valid_range(const _Safe_iterator
& __rhs
,
419 std::pair
<difference_type
, _Distance_precision
>& __dist
,
420 bool __check_dereferenceable
= true) const;
422 // The sequence this iterator references.
423 typename
__gnu_cxx::__conditional_type
<
424 _IsConstant::__value
, const _Sequence
*, _Sequence
*>::__type
425 _M_get_sequence() const
426 { return static_cast<_Sequence
*>(_M_sequence
); }
428 // Get distance to __rhs.
429 typename _Distance_traits
<_Iterator
>::__type
430 _M_get_distance_to(const _Safe_iterator
& __rhs
) const;
432 // Get distance from sequence begin up to *this.
433 typename _Distance_traits
<_Iterator
>::__type
434 _M_get_distance_from_begin() const;
436 // Get distance from *this to sequence end.
437 typename _Distance_traits
<_Iterator
>::__type
438 _M_get_distance_to_end() const;
440 /// Is this iterator equal to the sequence's begin() iterator?
443 { return base() == _M_get_sequence()->_M_base().begin(); }
445 /// Is this iterator equal to the sequence's end() iterator?
448 { return base() == _M_get_sequence()->_M_base().end(); }
450 /// Is this iterator equal to the sequence's before_begin() iterator if
453 _M_is_before_begin() const
454 { return _BeforeBeginHelper
<_Sequence
>::_S_Is(*this); }
456 /// Is this iterator equal to the sequence's before_begin() iterator if
457 /// any or begin() otherwise?
459 _M_is_beginnest() const
460 { return _BeforeBeginHelper
<_Sequence
>::_S_Is_Beginnest(*this); }
462 // ------ Operators ------
464 typedef _Safe_iterator
<_Iterator
, _Sequence
, iterator_category
> _Self
;
468 operator==(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
470 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
471 return __lhs
.base() == __rhs
.base();
474 template<typename _IteR
>
477 operator==(const _Self
& __lhs
,
478 const _Safe_iterator
<_IteR
, _Sequence
, iterator_category
>& __rhs
)
481 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
482 return __lhs
.base() == __rhs
.base();
485 #if ! __cpp_lib_three_way_comparison
488 operator!=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
490 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
491 return __lhs
.base() != __rhs
.base();
494 template<typename _IteR
>
497 operator!=(const _Self
& __lhs
,
498 const _Safe_iterator
<_IteR
, _Sequence
, iterator_category
>& __rhs
)
501 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs
, __rhs
);
502 return __lhs
.base() != __rhs
.base();
504 #endif // three-way comparison
507 template<typename _Iterator
, typename _Sequence
>
508 class _Safe_iterator
<_Iterator
, _Sequence
, std::bidirectional_iterator_tag
>
509 : public _Safe_iterator
<_Iterator
, _Sequence
, std::forward_iterator_tag
>
511 typedef _Safe_iterator
<_Iterator
, _Sequence
,
512 std::forward_iterator_tag
> _Safe_base
;
515 typedef typename
_Safe_base::_OtherIterator _OtherIterator
;
518 /// @post the iterator is singular and unattached
519 _Safe_iterator() _GLIBCXX_NOEXCEPT
{ }
522 * @brief Safe iterator construction from an unsafe iterator and
525 * @pre @p seq is not NULL
526 * @post this is not singular
528 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
530 : _Safe_base(__i
, __seq
)
534 * @brief Copy construction.
536 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
540 #if __cplusplus >= 201103L
541 /** @brief Move construction. */
542 _Safe_iterator(_Safe_iterator
&&) = default;
546 * @brief Converting constructor from a mutable iterator to a
549 template<typename _MutableIterator
>
551 const _Safe_iterator
<_MutableIterator
, _Sequence
,
552 typename
__gnu_cxx::__enable_if
<_Safe_base::_IsConstant::__value
&&
553 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
554 std::bidirectional_iterator_tag
>::__type
>& __x
)
559 #if __cplusplus >= 201103L
560 /** @brief Copy assignment. */
562 operator=(const _Safe_iterator
&) = default;
564 /** @brief Move assignment. */
566 operator=(_Safe_iterator
&&) = default;
568 /** @brief Copy assignment. */
570 operator=(const _Safe_iterator
& __x
)
572 _Safe_base::operator=(__x
);
577 // ------ Input iterator requirements ------
579 * @brief Iterator preincrement
580 * @pre iterator is incrementable
583 operator++() _GLIBCXX_NOEXCEPT
585 _Safe_base::operator++();
590 * @brief Iterator postincrement
591 * @pre iterator is incrementable
594 operator++(int) _GLIBCXX_NOEXCEPT
596 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
597 _M_message(__msg_bad_inc
)
598 ._M_iterator(*this, "this"));
599 _Safe_iterator __ret
= *this;
604 // ------ Bidirectional iterator requirements ------
606 * @brief Iterator predecrement
607 * @pre iterator is decrementable
610 operator--() _GLIBCXX_NOEXCEPT
612 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
613 _M_message(__msg_bad_dec
)
614 ._M_iterator(*this, "this"));
615 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
621 * @brief Iterator postdecrement
622 * @pre iterator is decrementable
625 operator--(int) _GLIBCXX_NOEXCEPT
627 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
628 _M_message(__msg_bad_dec
)
629 ._M_iterator(*this, "this"));
630 _Safe_iterator __ret
= *this;
635 // ------ Utilities ------
637 // Is the iterator decrementable?
639 _M_decrementable() const
640 { return !this->_M_singular() && !this->_M_is_begin(); }
643 template<typename _Iterator
, typename _Sequence
>
644 class _Safe_iterator
<_Iterator
, _Sequence
, std::random_access_iterator_tag
>
645 : public _Safe_iterator
<_Iterator
, _Sequence
,
646 std::bidirectional_iterator_tag
>
648 typedef _Safe_iterator
<_Iterator
, _Sequence
,
649 std::bidirectional_iterator_tag
> _Safe_base
;
650 typedef typename
_Safe_base::_OtherIterator _OtherIterator
;
652 typedef typename
_Safe_base::_Self _Self
;
653 typedef _Safe_iterator
<_OtherIterator
, _Sequence
,
654 std::random_access_iterator_tag
> _OtherSelf
;
657 typedef typename
_Safe_base::difference_type difference_type
;
658 typedef typename
_Safe_base::reference reference
;
660 /// @post the iterator is singular and unattached
661 _Safe_iterator() _GLIBCXX_NOEXCEPT
{ }
664 * @brief Safe iterator construction from an unsafe iterator and
667 * @pre @p seq is not NULL
668 * @post this is not singular
670 _Safe_iterator(_Iterator __i
, const _Safe_sequence_base
* __seq
)
672 : _Safe_base(__i
, __seq
)
676 * @brief Copy construction.
678 _Safe_iterator(const _Safe_iterator
& __x
) _GLIBCXX_NOEXCEPT
682 #if __cplusplus >= 201103L
683 /** @brief Move construction. */
684 _Safe_iterator(_Safe_iterator
&&) = default;
688 * @brief Converting constructor from a mutable iterator to a
691 template<typename _MutableIterator
>
693 const _Safe_iterator
<_MutableIterator
, _Sequence
,
694 typename
__gnu_cxx::__enable_if
<_Safe_base::_IsConstant::__value
&&
695 std::__are_same
<_MutableIterator
, _OtherIterator
>::__value
,
696 std::random_access_iterator_tag
>::__type
>& __x
)
701 #if __cplusplus >= 201103L
702 /** @brief Copy assignment. */
704 operator=(const _Safe_iterator
&) = default;
706 /** @brief Move assignment. */
708 operator=(_Safe_iterator
&&) = default;
710 /** @brief Copy assignment. */
712 operator=(const _Safe_iterator
& __x
)
714 _Safe_base::operator=(__x
);
719 // Is the iterator range [*this, __rhs) valid?
721 _M_valid_range(const _Safe_iterator
& __rhs
,
722 std::pair
<difference_type
,
723 _Distance_precision
>& __dist
) const;
725 // ------ Input iterator requirements ------
727 * @brief Iterator preincrement
728 * @pre iterator is incrementable
731 operator++() _GLIBCXX_NOEXCEPT
733 _Safe_base::operator++();
738 * @brief Iterator postincrement
739 * @pre iterator is incrementable
742 operator++(int) _GLIBCXX_NOEXCEPT
744 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
745 _M_message(__msg_bad_inc
)
746 ._M_iterator(*this, "this"));
747 _Safe_iterator __ret
= *this;
752 // ------ Bidirectional iterator requirements ------
754 * @brief Iterator predecrement
755 * @pre iterator is decrementable
758 operator--() _GLIBCXX_NOEXCEPT
760 _Safe_base::operator--();
765 * @brief Iterator postdecrement
766 * @pre iterator is decrementable
769 operator--(int) _GLIBCXX_NOEXCEPT
771 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
772 _M_message(__msg_bad_dec
)
773 ._M_iterator(*this, "this"));
774 _Safe_iterator __ret
= *this;
779 // ------ Random access iterator requirements ------
782 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
784 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n
)
785 && this->_M_can_advance(__n
+ 1),
786 _M_message(__msg_iter_subscript_oob
)
787 ._M_iterator(*this)._M_integer(__n
));
788 return this->base()[__n
];
792 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
794 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n
),
795 _M_message(__msg_advance_oob
)
796 ._M_iterator(*this)._M_integer(__n
));
797 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
803 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
805 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n
),
806 _M_message(__msg_retreat_oob
)
807 ._M_iterator(*this)._M_integer(__n
));
808 __gnu_cxx::__scoped_lock
__l(this->_M_get_mutex());
813 #if __cpp_lib_three_way_comparison
816 operator<=>(const _Self
& __lhs
, const _Self
& __rhs
) noexcept
818 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
819 return __lhs
.base() <=> __rhs
.base();
824 operator<=>(const _Self
& __lhs
, const _OtherSelf
& __rhs
) noexcept
826 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
827 return __lhs
.base() <=> __rhs
.base();
832 operator<(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
834 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
835 return __lhs
.base() < __rhs
.base();
840 operator<(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
842 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
843 return __lhs
.base() < __rhs
.base();
848 operator<=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
850 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
851 return __lhs
.base() <= __rhs
.base();
856 operator<=(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
858 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
859 return __lhs
.base() <= __rhs
.base();
864 operator>(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
866 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
867 return __lhs
.base() > __rhs
.base();
872 operator>(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
874 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
875 return __lhs
.base() > __rhs
.base();
880 operator>=(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
882 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
883 return __lhs
.base() >= __rhs
.base();
888 operator>=(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
890 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs
, __rhs
);
891 return __lhs
.base() >= __rhs
.base();
893 #endif // three-way comparison
895 // _GLIBCXX_RESOLVE_LIB_DEFECTS
896 // According to the resolution of DR179 not only the various comparison
897 // operators but also operator- must accept mixed iterator/const_iterator
900 friend difference_type
901 operator-(const _Self
& __lhs
, const _OtherSelf
& __rhs
) _GLIBCXX_NOEXCEPT
903 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs
, __rhs
);
904 return __lhs
.base() - __rhs
.base();
908 friend difference_type
909 operator-(const _Self
& __lhs
, const _Self
& __rhs
) _GLIBCXX_NOEXCEPT
911 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs
, __rhs
);
912 return __lhs
.base() - __rhs
.base();
917 operator+(const _Self
& __x
, difference_type __n
) _GLIBCXX_NOEXCEPT
919 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(__n
),
920 _M_message(__msg_advance_oob
)
921 ._M_iterator(__x
)._M_integer(__n
));
922 return _Safe_iterator(__x
.base() + __n
, __x
._M_sequence
);
927 operator+(difference_type __n
, const _Self
& __x
) _GLIBCXX_NOEXCEPT
929 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(__n
),
930 _M_message(__msg_advance_oob
)
931 ._M_iterator(__x
)._M_integer(__n
));
932 return _Safe_iterator(__n
+ __x
.base(), __x
._M_sequence
);
937 operator-(const _Self
& __x
, difference_type __n
) _GLIBCXX_NOEXCEPT
939 _GLIBCXX_DEBUG_VERIFY(__x
._M_can_advance(-__n
),
940 _M_message(__msg_retreat_oob
)
941 ._M_iterator(__x
)._M_integer(__n
));
942 return _Safe_iterator(__x
.base() - __n
, __x
._M_sequence
);
946 /** Safe iterators know how to check if they form a valid range. */
947 template<typename _Iterator
, typename _Sequence
, typename _Category
>
949 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
,
951 const _Safe_iterator
<_Iterator
, _Sequence
,
953 typename _Distance_traits
<_Iterator
>::__type
& __dist
)
954 { return __first
._M_valid_range(__last
, __dist
); }
956 template<typename _Iterator
, typename _Sequence
, typename _Category
>
958 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
,
960 const _Safe_iterator
<_Iterator
, _Sequence
,
963 typename _Distance_traits
<_Iterator
>::__type __dist
;
964 return __first
._M_valid_range(__last
, __dist
);
967 template<typename _Iterator
, typename _Sequence
, typename _Category
,
970 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>& __it
,
972 { return __it
._M_can_advance(__n
); }
974 template<typename _Iterator
, typename _Sequence
, typename _Category
,
977 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>& __it
,
978 const std::pair
<_Diff
, _Distance_precision
>& __dist
,
980 { return __it
._M_can_advance(__dist
, __way
); }
982 template<typename _Iterator
, typename _Sequence
>
984 __base(const _Safe_iterator
<_Iterator
, _Sequence
,
985 std::random_access_iterator_tag
>& __it
)
986 { return __it
.base(); }
988 #if __cplusplus < 201103L
989 template<typename _Iterator
, typename _Sequence
>
990 struct _Unsafe_type
<_Safe_iterator
<_Iterator
, _Sequence
> >
991 { typedef _Iterator _Type
; };
994 template<typename _Iterator
, typename _Sequence
>
996 __unsafe(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
)
997 { return __it
.base(); }
999 } // namespace __gnu_debug
1001 #if __cplusplus >= 201103L && __cplusplus <= 201703L
1002 namespace std
_GLIBCXX_VISIBILITY(default)
1004 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1006 template<typename _Iterator
, typename _Container
, typename _Sequence
>
1008 __to_address(const __gnu_debug::_Safe_iterator
<
1009 __gnu_cxx::__normal_iterator
<_Iterator
, _Container
>,
1010 _Sequence
>& __it
) noexcept
1011 -> decltype(std::__to_address(__it
.base().base()))
1012 { return std::__to_address(__it
.base().base()); }
1014 _GLIBCXX_END_NAMESPACE_VERSION
1018 #undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
1019 #undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
1020 #undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
1021 #undef _GLIBCXX_DEBUG_VERIFY_OPERANDS
1023 #include <debug/safe_iterator.tcc>