29 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
30 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
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.base() == _Iterator() \
45 && _Rhs.base() == _Iterator()), \
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)
71 template<
typename _Sequence>
74 template<
typename _Iterator,
typename _Category>
79 template<
typename _Iterator,
typename _Category>
82 {
return __it.
base() == __it._M_get_sequence()->_M_base().begin(); }
86 template<
typename _Sequence>
89 typedef _Distance_traits<typename _Sequence::iterator> _DistTraits;
92 _S_size(
const _Sequence& __seq)
93 {
return std::make_pair(__seq.size(), __dp_exact); }
112 template<
typename _Iterator,
typename _Sequence,
typename _Category
118 typedef _Iterator _Iter_base;
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;
132 struct _Attach_single
141 typedef _Iterator iterator_type;
142 typedef typename _Traits::iterator_category iterator_category;
143 typedef typename _Traits::value_type value_type;
144 typedef typename _Traits::difference_type difference_type;
145 typedef typename _Traits::reference reference;
146 typedef typename _Traits::pointer pointer;
148 #if __cplusplus > 201703L && __cpp_lib_concepts
149 using iterator_concept = std::__detail::__iter_concept<_Iterator>;
167 _M_message(__msg_init_singular)
168 ._M_iterator(*
this,
"this"));
179 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
180 || __x.base() == _Iterator(),
181 _M_message(__msg_init_copy_singular)
182 ._M_iterator(*
this,
"this")
183 ._M_iterator(__x,
"other"));
187 #if __cplusplus >= 201103L
195 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
196 || __x.base() == _Iterator(),
197 _M_message(__msg_init_copy_singular)
198 ._M_iterator(*
this,
"this")
199 ._M_iterator(__x,
"other"));
211 template<
typename _MutableIterator>
214 typename __gnu_cxx::__enable_if<_IsConstant::__value &&
215 std::__are_same<_MutableIterator, _OtherIterator>::__value,
216 _Category>::__type>& __x)
218 : _Iter_base(__x.base())
222 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
223 || __x.base() == _MutableIterator(),
224 _M_message(__msg_init_const_singular)
225 ._M_iterator(*
this,
"this")
226 ._M_iterator(__x,
"other"));
238 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
239 || __x.base() == _Iterator(),
240 _M_message(__msg_copy_singular)
241 ._M_iterator(*
this,
"this")
242 ._M_iterator(__x,
"other"));
260 #if __cplusplus >= 201103L
268 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
269 || __x.base() == _Iterator(),
270 _M_message(__msg_copy_singular)
271 ._M_iterator(*
this,
"this")
272 ._M_iterator(__x,
"other"));
291 __x.base() = _Iterator();
304 _M_message(__msg_bad_deref)
305 ._M_iterator(*
this,
"this"));
317 _M_message(__msg_bad_deref)
318 ._M_iterator(*
this,
"this"));
319 return base().operator->();
331 _M_message(__msg_bad_inc)
332 ._M_iterator(*
this,
"this"));
346 _M_message(__msg_bad_inc)
347 ._M_iterator(*
this,
"this"));
355 static _GLIBCXX_CONSTEXPR
bool
357 {
return _IsConstant::__value; }
363 base() _GLIBCXX_NOEXCEPT {
return *
this; }
366 base() const _GLIBCXX_NOEXCEPT {
return *
this; }
372 operator _Iterator() const _GLIBCXX_NOEXCEPT {
return *
this; }
396 return ++
__base != _M_get_sequence()->_M_base().end();
408 _M_can_advance(difference_type __n,
bool __strict =
false)
const;
414 bool __check_dereferenceable =
true)
const;
417 typename __gnu_cxx::__conditional_type<
418 _IsConstant::__value,
const _Sequence*, _Sequence*>::__type
419 _M_get_sequence()
const
423 typename _Distance_traits<_Iterator>::__type
427 typename _Distance_traits<_Iterator>::__type
428 _M_get_distance_from_begin()
const;
431 typename _Distance_traits<_Iterator>::__type
432 _M_get_distance_to_end()
const;
437 {
return base() == _M_get_sequence()->_M_base().begin(); }
442 {
return base() == _M_get_sequence()->_M_base().end(); }
461 operator==(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
463 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
464 return __lhs.base() == __rhs.base();
467 template<
typename _IteR>
469 operator==(
const _Self& __lhs,
470 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
473 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
474 return __lhs.base() == __rhs.base();
477 #if ! __cpp_lib_three_way_comparison
479 operator!=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
481 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
482 return __lhs.base() != __rhs.base();
485 template<
typename _IteR>
487 operator!=(
const _Self& __lhs,
488 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
491 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
492 return __lhs.base() != __rhs.base();
497 template<
typename _Iterator,
typename _Sequence>
498 class _Safe_iterator<_Iterator, _Sequence,
std::bidirectional_iterator_tag>
499 :
public _Safe_iterator<_Iterator, _Sequence, std::forward_iterator_tag>
505 typedef typename _Safe_base::_OtherIterator _OtherIterator;
506 typedef typename _Safe_base::_Attach_single _Attach_single;
508 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
510 : _Safe_base(__i, __seq, _Attach_single())
526 : _Safe_base(__i, __seq)
536 #if __cplusplus >= 201103L
545 template<
typename _MutableIterator>
548 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
549 std::__are_same<_MutableIterator, _OtherIterator>::__value,
555 #if __cplusplus >= 201103L
581 _Safe_base::operator++();
593 _M_message(__msg_bad_inc)
594 ._M_iterator(*
this,
"this"));
606 operator--() _GLIBCXX_NOEXCEPT
608 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
609 _M_message(__msg_bad_dec)
610 ._M_iterator(*
this,
"this"));
621 operator--(
int) _GLIBCXX_NOEXCEPT
623 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
624 _M_message(__msg_bad_dec)
625 ._M_iterator(*
this,
"this"));
635 _M_decrementable()
const
639 template<
typename _Iterator,
typename _Sequence>
640 class _Safe_iterator<_Iterator, _Sequence,
std::random_access_iterator_tag>
641 :
public _Safe_iterator<_Iterator, _Sequence,
642 std::bidirectional_iterator_tag>
646 typedef typename _Safe_base::_OtherIterator _OtherIterator;
648 typedef typename _Safe_base::_Self _Self;
652 typedef typename _Safe_base::_Attach_single _Attach_single;
654 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
656 : _Safe_base(__i, __seq, _Attach_single())
660 typedef typename _Safe_base::difference_type difference_type;
661 typedef typename _Safe_base::reference reference;
675 : _Safe_base(__i, __seq)
685 #if __cplusplus >= 201103L
694 template<
typename _MutableIterator>
697 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
698 std::__are_same<_MutableIterator, _OtherIterator>::__value,
704 #if __cplusplus >= 201103L
736 _Safe_base::operator++();
748 _M_message(__msg_bad_inc)
749 ._M_iterator(*
this,
"this"));
761 operator--() _GLIBCXX_NOEXCEPT
763 _Safe_base::operator--();
772 operator--(
int) _GLIBCXX_NOEXCEPT
774 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
775 _M_message(__msg_bad_dec)
776 ._M_iterator(*
this,
"this"));
784 operator[](difference_type __n)
const _GLIBCXX_NOEXCEPT
786 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
787 && this->_M_can_advance(__n + 1),
788 _M_message(__msg_iter_subscript_oob)
789 ._M_iterator(*this)._M_integer(__n));
790 return this->
base()[__n];
794 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
796 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
797 _M_message(__msg_advance_oob)
798 ._M_iterator(*this)._M_integer(__n));
805 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
807 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
808 _M_message(__msg_retreat_oob)
809 ._M_iterator(*this)._M_integer(__n));
815 #if __cpp_lib_three_way_comparison
817 operator<=>(
const _Self& __lhs,
const _Self& __rhs) noexcept
819 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
820 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();
831 operator<(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
833 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
834 return __lhs.base() < __rhs.base();
838 operator<(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
840 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
841 return __lhs.base() < __rhs.base();
845 operator<=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
847 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
848 return __lhs.base() <= __rhs.base();
852 operator<=(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
854 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
855 return __lhs.base() <= __rhs.base();
859 operator>(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
861 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
862 return __lhs.base() > __rhs.base();
866 operator>(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
868 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
869 return __lhs.base() > __rhs.base();
873 operator>=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
875 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
876 return __lhs.base() >= __rhs.base();
880 operator>=(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
882 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
883 return __lhs.base() >= __rhs.base();
891 friend difference_type
892 operator-(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
894 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
895 return __lhs.base() - __rhs.base();
898 friend difference_type
899 operator-(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
901 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
902 return __lhs.base() - __rhs.base();
906 operator+(
const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
908 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
909 _M_message(__msg_advance_oob)
910 ._M_iterator(__x)._M_integer(__n));
915 operator+(difference_type __n,
const _Self& __x) _GLIBCXX_NOEXCEPT
917 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
918 _M_message(__msg_advance_oob)
919 ._M_iterator(__x)._M_integer(__n));
924 operator-(
const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
926 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(-__n),
927 _M_message(__msg_retreat_oob)
928 ._M_iterator(__x)._M_integer(__n));
934 template<
typename _Iterator,
typename _Sequence,
typename _Category>
941 {
return __first._M_valid_range(__last, __dist); }
943 template<
typename _Iterator,
typename _Sequence,
typename _Category>
947 const _Safe_iterator<_Iterator, _Sequence,
950 typename _Distance_traits<_Iterator>::__type __dist;
951 return __first._M_valid_range(__last, __dist);
954 template<
typename _Iterator,
typename _Sequence,
typename _Category,
957 __can_advance(
const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
959 {
return __it._M_can_advance(__n); }
961 template<
typename _Iterator,
typename _Sequence>
963 __base(
const _Safe_iterator<_Iterator, _Sequence,
965 {
return __it.base(); }
967 #if __cplusplus < 201103L
968 template<
typename _Iterator,
typename _Sequence>
969 struct _Unsafe_type<_Safe_iterator<_Iterator, _Sequence> >
970 {
typedef _Iterator _Type; };
973 template<
typename _Iterator,
typename _Sequence>
975 __unsafe(
const _Safe_iterator<_Iterator, _Sequence>& __it)
976 {
return __it.base(); }
980 #undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
981 #undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
982 #undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
983 #undef _GLIBCXX_DEBUG_VERIFY_OPERANDS
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
ISO C++ entities toplevel namespace is std.
GNU debug classes for public use.
constexpr bool __valid_range(_InputIterator __first, _InputIterator __last, typename _Distance_traits< _InputIterator >::__type &__dist)
constexpr _Iterator __base(_Iterator __it)
Traits class for iterators.
Forward iterators support a superset of input iterator operations.
Bidirectional iterators support a superset of forward iterator operations.
Random-access iterators support a superset of bidirectional iterator operations.
Struct holding two objects of arbitrary type.
bool _M_incrementable() const
Is the iterator incrementable?
reference operator*() const noexcept
Iterator dereference.
_Safe_iterator operator++(int) noexcept
Iterator postincrement.
_Safe_iterator(const _Safe_iterator &__x) noexcept
Copy construction.
_Safe_iterator(_Safe_iterator &&__x) noexcept
Move construction.
bool _M_dereferenceable() const
Is the iterator dereferenceable?
void _M_attach_single(_Safe_sequence_base *__seq)
_Iterator & base() noexcept
Return the underlying iterator.
bool _M_before_dereferenceable() const
Is the iterator before a dereferenceable one?
_Safe_iterator(const _Safe_iterator< _MutableIterator, _Sequence, typename __gnu_cxx::__enable_if< _IsConstant::__value &&std::__are_same< _MutableIterator, _OtherIterator >::__value, _Category >::__type > &__x) noexcept
Converting constructor from a mutable iterator to a constant iterator.
bool _M_is_beginnest() const
Is this iterator equal to the sequence's before_begin() iterator if any or begin() otherwise?
_Safe_iterator & operator=(_Safe_iterator &&__x) noexcept
Move assignment.
bool _M_is_begin() const
Is this iterator equal to the sequence's begin() iterator?
_Safe_iterator() noexcept
_Safe_iterator(_Iterator __i, const _Safe_sequence_base *__seq) noexcept
Safe iterator construction from an unsafe iterator and its sequence.
bool _M_is_end() const
Is this iterator equal to the sequence's end() iterator?
void _M_attach(_Safe_sequence_base *__seq)
_Safe_iterator & operator=(const _Safe_iterator &__x) noexcept
Copy assignment.
bool _M_is_before_begin() const
Is this iterator equal to the sequence's before_begin() iterator if any?
static constexpr bool _S_constant()
Determine if this is a constant iterator.
_Safe_iterator & operator++() noexcept
Iterator preincrement.
pointer operator->() const noexcept
Iterator dereference.
Basic functionality for a safe iterator.
_Safe_sequence_base * _M_sequence
void _M_attach_single(_Safe_sequence_base *__seq, bool __constant)
void _M_attach(_Safe_sequence_base *__seq, bool __constant)
__gnu_cxx::__mutex & _M_get_mutex()
Base class that supports tracking of iterators that reference a sequence.