29 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
30 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
44 template <
typename _Sequence>
47 template<
typename _Iterator>
52 template<
typename _Iterator>
55 {
return __it.
base() == __it._M_get_sequence()->_M_base().begin(); }
78 template<
typename _Iterator>
85 template<
typename _Iterator>
92 template<
typename _Iterator>
97 typedef typename std::iterator_traits<_Iterator>::iterator_category
113 template<
typename _Iterator,
typename _Sequence>
114 class _Safe_iterator :
public _Safe_iterator_base
117 typedef typename _Sequence::const_iterator _Const_iterator;
120 _Iterator _M_current;
125 {
return std::__are_same<_Const_iterator, _Safe_iterator>::__value; }
127 typedef std::iterator_traits<_Iterator> _Traits;
130 typedef _Iterator iterator_type;
131 typedef typename _Traits::iterator_category iterator_category;
132 typedef typename _Traits::value_type value_type;
133 typedef typename _Traits::difference_type difference_type;
134 typedef typename _Traits::reference reference;
135 typedef typename _Traits::pointer pointer;
152 _M_message(__msg_init_singular)
153 ._M_iterator(*
this,
"this"));
164 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
165 || __x._M_current == _Iterator(),
166 _M_message(__msg_init_copy_singular)
167 ._M_iterator(*
this,
"this")
168 ._M_iterator(__x,
"other"));
171 #if __cplusplus >= 201103L
178 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
179 || __x._M_current == _Iterator(),
180 _M_message(__msg_init_copy_singular)
181 ._M_iterator(*
this,
"this")
182 ._M_iterator(__x,
"other"));
193 template<
typename _MutableIterator>
196 typename __gnu_cxx::__enable_if<(std::__are_same<_MutableIterator,
197 typename _Sequence::iterator::iterator_type>::__value),
198 _Sequence>::__type>& __x) _GLIBCXX_NOEXCEPT
203 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
204 || __x.base() == _Iterator(),
205 _M_message(__msg_init_const_singular)
206 ._M_iterator(*
this,
"this")
207 ._M_iterator(__x,
"other"));
218 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
219 || __x._M_current == _Iterator(),
220 _M_message(__msg_copy_singular)
221 ._M_iterator(*
this,
"this")
222 ._M_iterator(__x,
"other"));
223 _M_current = __x._M_current;
228 #if __cplusplus >= 201103L
236 _GLIBCXX_DEBUG_VERIFY(
this != &__x,
237 _M_message(__msg_self_move_assign)
238 ._M_iterator(*
this,
"this"));
239 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
240 || __x._M_current == _Iterator(),
241 _M_message(__msg_copy_singular)
242 ._M_iterator(*
this,
"this")
243 ._M_iterator(__x,
"other"));
244 _M_current = __x._M_current;
247 __x._M_current = _Iterator();
260 _M_message(__msg_bad_deref)
261 ._M_iterator(*
this,
"this"));
274 _M_message(__msg_bad_deref)
275 ._M_iterator(*
this,
"this"));
288 _M_message(__msg_bad_inc)
289 ._M_iterator(*
this,
"this"));
302 _M_message(__msg_bad_inc)
303 ._M_iterator(*
this,
"this"));
317 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
318 _M_message(__msg_bad_dec)
319 ._M_iterator(*
this,
"this"));
331 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
332 _M_message(__msg_bad_dec)
333 ._M_iterator(*
this,
"this"));
341 operator[](
const difference_type& __n)
const _GLIBCXX_NOEXCEPT
343 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
344 && this->_M_can_advance(__n+1),
345 _M_message(__msg_iter_subscript_oob)
346 ._M_iterator(*this)._M_integer(__n));
348 return _M_current[__n];
352 operator+=(
const difference_type& __n) _GLIBCXX_NOEXCEPT
354 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
355 _M_message(__msg_advance_oob)
356 ._M_iterator(*this)._M_integer(__n));
362 operator+(
const difference_type& __n)
const _GLIBCXX_NOEXCEPT
370 operator-=(
const difference_type& __n) _GLIBCXX_NOEXCEPT
372 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
373 _M_message(__msg_retreat_oob)
374 ._M_iterator(*this)._M_integer(__n));
380 operator-(
const difference_type& __n)
const _GLIBCXX_NOEXCEPT
392 base() const _GLIBCXX_NOEXCEPT {
return _M_current; }
398 operator _Iterator() const _GLIBCXX_NOEXCEPT {
return _M_current; }
426 return ++__base != _M_get_sequence()->_M_base().end();
442 _M_can_advance(
const difference_type& __n)
const;
450 __gnu_cxx::__conditional_type<std::__are_same<_Const_iterator,
454 _M_get_sequence()
const
460 {
return base() == _M_get_sequence()->_M_base().begin(); }
465 {
return base() == _M_get_sequence()->_M_base().end(); }
480 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
482 operator==(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
483 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
486 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
487 _M_message(__msg_iter_compare_bad)
488 ._M_iterator(__lhs,
"lhs")
489 ._M_iterator(__rhs,
"rhs"));
490 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
491 _M_message(__msg_compare_different)
492 ._M_iterator(__lhs,
"lhs")
493 ._M_iterator(__rhs,
"rhs"));
494 return __lhs.base() == __rhs.base();
497 template<
typename _Iterator,
typename _Sequence>
499 operator==(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
500 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
503 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
504 _M_message(__msg_iter_compare_bad)
505 ._M_iterator(__lhs,
"lhs")
506 ._M_iterator(__rhs,
"rhs"));
507 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
508 _M_message(__msg_compare_different)
509 ._M_iterator(__lhs,
"lhs")
510 ._M_iterator(__rhs,
"rhs"));
511 return __lhs.base() == __rhs.base();
514 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
516 operator!=(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
517 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
520 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
521 _M_message(__msg_iter_compare_bad)
522 ._M_iterator(__lhs,
"lhs")
523 ._M_iterator(__rhs,
"rhs"));
524 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
525 _M_message(__msg_compare_different)
526 ._M_iterator(__lhs,
"lhs")
527 ._M_iterator(__rhs,
"rhs"));
528 return __lhs.base() != __rhs.base();
531 template<
typename _Iterator,
typename _Sequence>
533 operator!=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
534 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
537 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
538 _M_message(__msg_iter_compare_bad)
539 ._M_iterator(__lhs,
"lhs")
540 ._M_iterator(__rhs,
"rhs"));
541 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
542 _M_message(__msg_compare_different)
543 ._M_iterator(__lhs,
"lhs")
544 ._M_iterator(__rhs,
"rhs"));
545 return __lhs.base() != __rhs.base();
548 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
550 operator<(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
551 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
554 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
555 _M_message(__msg_iter_order_bad)
556 ._M_iterator(__lhs,
"lhs")
557 ._M_iterator(__rhs,
"rhs"));
558 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
559 _M_message(__msg_order_different)
560 ._M_iterator(__lhs,
"lhs")
561 ._M_iterator(__rhs,
"rhs"));
562 return __lhs.base() < __rhs.base();
565 template<
typename _Iterator,
typename _Sequence>
567 operator<(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
568 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
571 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
572 _M_message(__msg_iter_order_bad)
573 ._M_iterator(__lhs,
"lhs")
574 ._M_iterator(__rhs,
"rhs"));
575 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
576 _M_message(__msg_order_different)
577 ._M_iterator(__lhs,
"lhs")
578 ._M_iterator(__rhs,
"rhs"));
579 return __lhs.base() < __rhs.base();
582 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
584 operator<=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
585 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
588 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
589 _M_message(__msg_iter_order_bad)
590 ._M_iterator(__lhs,
"lhs")
591 ._M_iterator(__rhs,
"rhs"));
592 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
593 _M_message(__msg_order_different)
594 ._M_iterator(__lhs,
"lhs")
595 ._M_iterator(__rhs,
"rhs"));
596 return __lhs.base() <= __rhs.base();
599 template<
typename _Iterator,
typename _Sequence>
601 operator<=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
602 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
605 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
606 _M_message(__msg_iter_order_bad)
607 ._M_iterator(__lhs,
"lhs")
608 ._M_iterator(__rhs,
"rhs"));
609 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
610 _M_message(__msg_order_different)
611 ._M_iterator(__lhs,
"lhs")
612 ._M_iterator(__rhs,
"rhs"));
613 return __lhs.base() <= __rhs.base();
616 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
618 operator>(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
619 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
622 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
623 _M_message(__msg_iter_order_bad)
624 ._M_iterator(__lhs,
"lhs")
625 ._M_iterator(__rhs,
"rhs"));
626 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
627 _M_message(__msg_order_different)
628 ._M_iterator(__lhs,
"lhs")
629 ._M_iterator(__rhs,
"rhs"));
630 return __lhs.base() > __rhs.base();
633 template<
typename _Iterator,
typename _Sequence>
635 operator>(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
636 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
639 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
640 _M_message(__msg_iter_order_bad)
641 ._M_iterator(__lhs,
"lhs")
642 ._M_iterator(__rhs,
"rhs"));
643 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
644 _M_message(__msg_order_different)
645 ._M_iterator(__lhs,
"lhs")
646 ._M_iterator(__rhs,
"rhs"));
647 return __lhs.base() > __rhs.base();
650 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
652 operator>=(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
653 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
656 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
657 _M_message(__msg_iter_order_bad)
658 ._M_iterator(__lhs,
"lhs")
659 ._M_iterator(__rhs,
"rhs"));
660 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
661 _M_message(__msg_order_different)
662 ._M_iterator(__lhs,
"lhs")
663 ._M_iterator(__rhs,
"rhs"));
664 return __lhs.base() >= __rhs.base();
667 template<
typename _Iterator,
typename _Sequence>
669 operator>=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
670 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
673 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
674 _M_message(__msg_iter_order_bad)
675 ._M_iterator(__lhs,
"lhs")
676 ._M_iterator(__rhs,
"rhs"));
677 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
678 _M_message(__msg_order_different)
679 ._M_iterator(__lhs,
"lhs")
680 ._M_iterator(__rhs,
"rhs"));
681 return __lhs.base() >= __rhs.base();
688 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
689 inline typename _Safe_iterator<_IteratorL, _Sequence>::difference_type
690 operator-(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
691 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
694 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
695 _M_message(__msg_distance_bad)
696 ._M_iterator(__lhs,
"lhs")
697 ._M_iterator(__rhs,
"rhs"));
698 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
699 _M_message(__msg_distance_different)
700 ._M_iterator(__lhs,
"lhs")
701 ._M_iterator(__rhs,
"rhs"));
702 return __lhs.base() - __rhs.base();
705 template<
typename _Iterator,
typename _Sequence>
706 inline typename _Safe_iterator<_Iterator, _Sequence>::difference_type
707 operator-(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
708 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
711 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
712 _M_message(__msg_distance_bad)
713 ._M_iterator(__lhs,
"lhs")
714 ._M_iterator(__rhs,
"rhs"));
715 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
716 _M_message(__msg_distance_different)
717 ._M_iterator(__lhs,
"lhs")
718 ._M_iterator(__rhs,
"rhs"));
719 return __lhs.base() - __rhs.base();
722 template<
typename _Iterator,
typename _Sequence>
723 inline _Safe_iterator<_Iterator, _Sequence>
724 operator+(
typename _Safe_iterator<_Iterator,_Sequence>::difference_type __n,
725 const _Safe_iterator<_Iterator, _Sequence>& __i) _GLIBCXX_NOEXCEPT
726 {
return __i + __n; }
729 #include <debug/safe_iterator.tcc>
_Safe_iterator & operator++() noexcept
Iterator preincrement.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
_Safe_iterator & operator=(_Safe_iterator &&__x) noexcept
Move assignment.
std::pair< typename std::iterator_traits< _Iterator >::difference_type, _Distance_precision > __get_distance(const _Iterator &__lhs, const _Iterator &__rhs, std::random_access_iterator_tag)
bool _M_is_end() const
Is this iterator equal to the sequence's end() iterator?
_Safe_iterator operator--(int) noexcept
Iterator postdecrement.
void _M_attach(_Safe_sequence_base *__seq, bool __constant)
Forward iterators support a superset of input iterator operations.
void _M_attach(_Safe_sequence_base *__seq)
Basic functionality for a safe iterator.
pointer operator->() const noexcept
Iterator dereference.
_Safe_iterator() noexcept
bool _M_incrementable() const
Is the iterator incrementable?
reference operator*() const noexcept
Iterator dereference.
bool _M_is_beginnest() const
Is this iterator equal to the sequence's before_begin() iterator if any or begin() otherwise...
_Safe_iterator(const _Safe_iterator &__x) noexcept
Copy construction.
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
bool _M_is_begin() const
Is this iterator equal to the sequence's begin() iterator?
_Safe_sequence_base * _M_sequence
_Safe_iterator(_Safe_iterator &&__x) noexcept
Move construction.
_Safe_iterator & operator=(const _Safe_iterator &__x) noexcept
Copy assignment.
void _M_attach_single(_Safe_sequence_base *__seq, bool __constant)
bool _M_before_dereferenceable() const
Is the iterator before a dereferenceable one?
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.
GNU debug classes for public use.
_Safe_iterator(const _Iterator &__i, const _Sequence *__seq) noexcept
Safe iterator construction from an unsafe iterator and its sequence.
bool _M_is_before_begin() const
Is this iterator equal to the sequence's before_begin() iterator if any?
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
basic_string< _CharT, _Traits, _Alloc > operator+(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Concatenate two strings.
_Safe_iterator & operator--() noexcept
Iterator predecrement.
_Safe_iterator(const _Safe_iterator< _MutableIterator, typename __gnu_cxx::__enable_if<(std::__are_same< _MutableIterator, typename _Sequence::iterator::iterator_type >::__value), _Sequence >::__type > &__x) noexcept
Converting constructor from a mutable iterator to a constant iterator.
Random-access iterators support a superset of bidirectional iterator operations.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
_Safe_iterator operator++(int) noexcept
Iterator postincrement.
Struct holding two objects of arbitrary type.
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
_Iterator base() const noexcept
Return the underlying iterator.
bool _M_dereferenceable() const
Is the iterator dereferenceable?
void _M_attach_single(_Safe_sequence_base *__seq)
Base class that supports tracking of iterators that reference a sequence.