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._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)
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._M_value_initialized(),
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._M_value_initialized(),
197 _M_message(__msg_init_copy_singular)
198 ._M_iterator(*
this,
"this")
199 ._M_iterator(__x,
"other"));
202 std::swap(
base(), __x.base());
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._M_value_initialized(),
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._M_value_initialized(),
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._M_value_initialized(),
270 _M_message(__msg_copy_singular)
271 ._M_iterator(*
this,
"this")
272 ._M_iterator(__x,
"other"));
291 __x.base() = _Iterator();
305 _M_message(__msg_bad_deref)
306 ._M_iterator(*
this,
"this"));
319 _M_message(__msg_bad_deref)
320 ._M_iterator(*
this,
"this"));
321 return base().operator->();
333 _M_message(__msg_bad_inc)
334 ._M_iterator(*
this,
"this"));
348 _M_message(__msg_bad_inc)
349 ._M_iterator(*
this,
"this"));
357 static _GLIBCXX_CONSTEXPR
bool
359 {
return _IsConstant::__value; }
365 base() _GLIBCXX_NOEXCEPT {
return *
this; }
368 base() const _GLIBCXX_NOEXCEPT {
return *
this; }
374 operator _Iterator() const _GLIBCXX_NOEXCEPT {
return *
this; }
398 return ++
__base != _M_get_sequence()->_M_base().end();
415 _M_can_advance(difference_type __n,
bool __strict =
false)
const;
418 template<
typename _Diff>
427 bool __check_dereferenceable =
true)
const;
430 typename __gnu_cxx::__conditional_type<
431 _IsConstant::__value,
const _Sequence*, _Sequence*>::__type
432 _M_get_sequence()
const
436 typename _Distance_traits<_Iterator>::__type
440 typename _Distance_traits<_Iterator>::__type
441 _M_get_distance_from_begin()
const;
444 typename _Distance_traits<_Iterator>::__type
445 _M_get_distance_to_end()
const;
450 {
return base() == _M_get_sequence()->_M_base().begin(); }
455 {
return base() == _M_get_sequence()->_M_base().end(); }
475 operator==(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
477 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
478 return __lhs.base() == __rhs.base();
481 template<
typename _IteR>
484 operator==(
const _Self& __lhs,
485 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
488 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
489 return __lhs.base() == __rhs.base();
492#if ! __cpp_lib_three_way_comparison
495 operator!=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
497 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
498 return __lhs.base() != __rhs.base();
501 template<
typename _IteR>
504 operator!=(
const _Self& __lhs,
505 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
508 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
509 return __lhs.base() != __rhs.base();
514 template<
typename _Iterator,
typename _Sequence>
515 class _Safe_iterator<_Iterator, _Sequence,
std::bidirectional_iterator_tag>
516 :
public _Safe_iterator<_Iterator, _Sequence, std::forward_iterator_tag>
522 typedef typename _Safe_base::_OtherIterator _OtherIterator;
523 typedef typename _Safe_base::_Attach_single _Attach_single;
525 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
527 : _Safe_base(__i, __seq, _Attach_single())
543 : _Safe_base(__i, __seq)
553#if __cplusplus >= 201103L
562 template<
typename _MutableIterator>
565 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
566 std::__are_same<_MutableIterator, _OtherIterator>::__value,
572#if __cplusplus >= 201103L
585 _Safe_base::operator=(__x);
598 _Safe_base::operator++();
610 _M_message(__msg_bad_inc)
611 ._M_iterator(*
this,
"this"));
623 operator--() _GLIBCXX_NOEXCEPT
625 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
626 _M_message(__msg_bad_dec)
627 ._M_iterator(*
this,
"this"));
638 operator--(
int) _GLIBCXX_NOEXCEPT
640 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
641 _M_message(__msg_bad_dec)
642 ._M_iterator(*
this,
"this"));
652 _M_decrementable()
const
656 template<
typename _Iterator,
typename _Sequence>
657 class _Safe_iterator<_Iterator, _Sequence,
std::random_access_iterator_tag>
658 :
public _Safe_iterator<_Iterator, _Sequence,
659 std::bidirectional_iterator_tag>
663 typedef typename _Safe_base::_OtherIterator _OtherIterator;
665 typedef typename _Safe_base::_Self _Self;
669 typedef typename _Safe_base::_Attach_single _Attach_single;
671 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
673 : _Safe_base(__i, __seq, _Attach_single())
677 typedef typename _Safe_base::difference_type difference_type;
678 typedef typename _Safe_base::reference reference;
692 : _Safe_base(__i, __seq)
702#if __cplusplus >= 201103L
711 template<
typename _MutableIterator>
714 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
715 std::__are_same<_MutableIterator, _OtherIterator>::__value,
721#if __cplusplus >= 201103L
734 _Safe_base::operator=(__x);
753 _Safe_base::operator++();
765 _M_message(__msg_bad_inc)
766 ._M_iterator(*
this,
"this"));
778 operator--() _GLIBCXX_NOEXCEPT
780 _Safe_base::operator--();
789 operator--(
int) _GLIBCXX_NOEXCEPT
791 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
792 _M_message(__msg_bad_dec)
793 ._M_iterator(*
this,
"this"));
802 operator[](difference_type __n)
const _GLIBCXX_NOEXCEPT
804 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
805 && this->_M_can_advance(__n + 1),
806 _M_message(__msg_iter_subscript_oob)
807 ._M_iterator(*this)._M_integer(__n));
808 return this->
base()[__n];
812 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
814 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
815 _M_message(__msg_advance_oob)
816 ._M_iterator(*this)._M_integer(__n));
823 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
825 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
826 _M_message(__msg_retreat_oob)
827 ._M_iterator(*this)._M_integer(__n));
833#if __cpp_lib_three_way_comparison
836 operator<=>(
const _Self& __lhs,
const _Self& __rhs)
noexcept
838 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
839 return __lhs.base() <=> __rhs.base();
844 operator<=>(
const _Self& __lhs,
const _OtherSelf& __rhs)
noexcept
846 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
847 return __lhs.base() <=> __rhs.base();
852 operator<(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
854 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
855 return __lhs.base() < __rhs.base();
860 operator<(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
862 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
863 return __lhs.base() < __rhs.base();
868 operator<=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
870 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
871 return __lhs.base() <= __rhs.base();
876 operator<=(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
878 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
879 return __lhs.base() <= __rhs.base();
884 operator>(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
886 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
887 return __lhs.base() > __rhs.base();
892 operator>(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
894 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
895 return __lhs.base() > __rhs.base();
900 operator>=(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
902 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
903 return __lhs.base() >= __rhs.base();
908 operator>=(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
910 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
911 return __lhs.base() >= __rhs.base();
920 friend difference_type
921 operator-(
const _Self& __lhs,
const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
923 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
924 return __lhs.base() - __rhs.base();
928 friend difference_type
929 operator-(
const _Self& __lhs,
const _Self& __rhs) _GLIBCXX_NOEXCEPT
931 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
932 return __lhs.base() - __rhs.base();
937 operator+(
const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
939 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
940 _M_message(__msg_advance_oob)
941 ._M_iterator(__x)._M_integer(__n));
947 operator+(difference_type __n,
const _Self& __x) _GLIBCXX_NOEXCEPT
949 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
950 _M_message(__msg_advance_oob)
951 ._M_iterator(__x)._M_integer(__n));
957 operator-(
const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
959 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(-__n),
960 _M_message(__msg_retreat_oob)
961 ._M_iterator(__x)._M_integer(__n));
967 template<
typename _Iterator,
typename _Sequence,
typename _Category>
974 {
return __first._M_valid_range(__last, __dist); }
976 template<
typename _Iterator,
typename _Sequence,
typename _Category>
980 const _Safe_iterator<_Iterator, _Sequence,
983 typename _Distance_traits<_Iterator>::__type __dist;
984 return __first._M_valid_range(__last, __dist);
987 template<
typename _Iterator,
typename _Sequence,
typename _Category,
990 __can_advance(
const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
992 {
return __it._M_can_advance(__n); }
994 template<
typename _Iterator,
typename _Sequence,
typename _Category,
997 __can_advance(
const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
1000 {
return __it._M_can_advance(__dist, __way); }
1002 template<
typename _Iterator,
typename _Sequence>
1004 __base(
const _Safe_iterator<_Iterator, _Sequence,
1006 {
return __it.base(); }
1008#if __cplusplus < 201103L
1009 template<
typename _Iterator,
typename _Sequence>
1010 struct _Unsafe_type<_Safe_iterator<_Iterator, _Sequence> >
1011 {
typedef _Iterator _Type; };
1014 template<
typename _Iterator,
typename _Sequence>
1016 __unsafe(
const _Safe_iterator<_Iterator, _Sequence>& __it)
1017 {
return __it.base(); }
1021#if __cplusplus >= 201103L && __cplusplus <= 201703L
1022namespace std _GLIBCXX_VISIBILITY(default)
1024_GLIBCXX_BEGIN_NAMESPACE_VERSION
1026 template<
typename _Iterator,
typename _Container,
typename _Sequence>
1029 __gnu_cxx::__normal_iterator<_Iterator, _Container>,
1030 _Sequence>& __it)
noexcept
1031 ->
decltype(std::__to_address(__it.base().base()))
1032 {
return std::__to_address(__it.base().base()); }
1034_GLIBCXX_END_NAMESPACE_VERSION
1038#undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
1039#undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
1040#undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
1041#undef _GLIBCXX_DEBUG_VERIFY_OPERANDS
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.
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)
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)
bool _M_before_dereferenceable() const
Is the iterator before a dereferenceable one?
_Safe_iterator & operator=(const _Safe_iterator &__x) noexcept
Copy assignment.
_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++() noexcept
Iterator preincrement.
_Safe_iterator & operator=(_Safe_iterator &&__x) noexcept
Move assignment.
bool _M_is_begin() const
Is this iterator equal to the sequence's begin() iterator?
bool _M_value_initialized() const
Is the iterator value-initialized?
_Iterator & base() noexcept
Return the underlying 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)
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.
pointer operator->() const noexcept
Iterator dereference.
Traits class for iterators.
Struct holding two objects of arbitrary type.
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.
Basic functionality for a safe iterator.
_Safe_sequence_base * _M_sequence
__gnu_cxx::__mutex & _M_get_mutex()
void _M_attach_single(_Safe_sequence_base *__seq, bool __constant)
void _M_attach(_Safe_sequence_base *__seq, bool __constant)
Base class that supports tracking of iterators that reference a sequence.