30 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
31 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
46 template <
typename _Sequence>
49 typedef typename _Sequence::const_iterator _It;
50 typedef typename _It::iterator_type _BaseIt;
53 _S_Is(_BaseIt,
const _Sequence*)
57 _S_Is_Beginnest(_BaseIt __it,
const _Sequence* __seq)
58 {
return __it == __seq->_M_base().begin(); }
82 template<
typename _Iterator1,
typename _Iterator2>
89 template<
typename _Iterator1,
typename _Iterator2>
96 template<
typename _Iterator1,
typename _Iterator2>
101 typedef typename std::iterator_traits<_Iterator1>::iterator_category
117 template<
typename _Iterator,
typename _Sequence>
118 class _Safe_iterator :
public _Safe_iterator_base
123 _Iterator _M_current;
129 typedef typename _Sequence::const_iterator const_iterator;
130 return std::__are_same<const_iterator, _Safe_iterator>::__value;
133 typedef std::iterator_traits<_Iterator> _Traits;
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;
157 _M_message(__msg_init_singular)
158 ._M_iterator(*
this,
"this"));
169 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
170 || __x._M_current == _Iterator(),
171 _M_message(__msg_init_copy_singular)
172 ._M_iterator(*
this,
"this")
173 ._M_iterator(__x,
"other"));
176 #ifdef __GXX_EXPERIMENTAL_CXX0X__
183 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
184 || __x._M_current == _Iterator(),
185 _M_message(__msg_init_copy_singular)
186 ._M_iterator(*
this,
"this")
187 ._M_iterator(__x,
"other"));
188 std::swap(_M_current, __x._M_current);
198 template<
typename _MutableIterator>
201 typename __gnu_cxx::__enable_if<(std::__are_same<_MutableIterator,
202 typename _Sequence::iterator::iterator_type>::__value),
203 _Sequence>::__type>& __x)
208 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
209 || __x.base() == _Iterator(),
210 _M_message(__msg_init_const_singular)
211 ._M_iterator(*
this,
"this")
212 ._M_iterator(__x,
"other"));
223 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
224 || __x._M_current == _Iterator(),
225 _M_message(__msg_copy_singular)
226 ._M_iterator(*
this,
"this")
227 ._M_iterator(__x,
"other"));
228 _M_current = __x._M_current;
233 #ifdef __GXX_EXPERIMENTAL_CXX0X__
241 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
242 || __x._M_current == _Iterator(),
243 _M_message(__msg_copy_singular)
244 ._M_iterator(*
this,
"this")
245 ._M_iterator(__x,
"other"));
246 _M_current = __x._M_current;
249 __x._M_current = _Iterator();
262 _M_message(__msg_bad_deref)
263 ._M_iterator(*
this,
"this"));
277 _M_message(__msg_bad_deref)
278 ._M_iterator(*
this,
"this"));
291 _M_message(__msg_bad_inc)
292 ._M_iterator(*
this,
"this"));
305 _M_message(__msg_bad_inc)
306 ._M_iterator(*
this,
"this"));
320 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
321 _M_message(__msg_bad_dec)
322 ._M_iterator(*
this,
"this"));
334 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
335 _M_message(__msg_bad_dec)
336 ._M_iterator(*
this,
"this"));
344 operator[](
const difference_type& __n)
const
346 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
347 && this->_M_can_advance(__n+1),
348 _M_message(__msg_iter_subscript_oob)
349 ._M_iterator(*this)._M_integer(__n));
351 return _M_current[__n];
355 operator+=(
const difference_type& __n)
357 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
358 _M_message(__msg_advance_oob)
359 ._M_iterator(*this)._M_integer(__n));
365 operator+(
const difference_type& __n)
const
373 operator-=(
const difference_type& __n)
375 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
376 _M_message(__msg_retreat_oob)
377 ._M_iterator(*this)._M_integer(__n));
383 operator-(
const difference_type& __n)
const
395 base()
const {
return _M_current; }
401 operator _Iterator()
const {
return _M_current; }
429 return ++__base != _M_get_sequence()->_M_base().end();
445 _M_can_advance(
const difference_type& __n)
const;
448 template<
typename _Other>
450 _M_valid_range(
const _Safe_iterator<_Other, _Sequence>& __rhs)
const;
454 _M_get_sequence()
const
455 {
return static_cast<const _Sequence*
>(
_M_sequence); }
459 {
return base() == _M_get_sequence()->_M_base().begin(); }
463 {
return base() == _M_get_sequence()->_M_base().end(); }
481 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
483 operator==(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
484 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)
502 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
503 _M_message(__msg_iter_compare_bad)
504 ._M_iterator(__lhs,
"lhs")
505 ._M_iterator(__rhs,
"rhs"));
506 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
507 _M_message(__msg_compare_different)
508 ._M_iterator(__lhs,
"lhs")
509 ._M_iterator(__rhs,
"rhs"));
510 return __lhs.base() == __rhs.base();
513 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
515 operator!=(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
516 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
518 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
519 _M_message(__msg_iter_compare_bad)
520 ._M_iterator(__lhs,
"lhs")
521 ._M_iterator(__rhs,
"rhs"));
522 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
523 _M_message(__msg_compare_different)
524 ._M_iterator(__lhs,
"lhs")
525 ._M_iterator(__rhs,
"rhs"));
526 return __lhs.base() != __rhs.base();
529 template<
typename _Iterator,
typename _Sequence>
531 operator!=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
532 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
534 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
535 _M_message(__msg_iter_compare_bad)
536 ._M_iterator(__lhs,
"lhs")
537 ._M_iterator(__rhs,
"rhs"));
538 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
539 _M_message(__msg_compare_different)
540 ._M_iterator(__lhs,
"lhs")
541 ._M_iterator(__rhs,
"rhs"));
542 return __lhs.base() != __rhs.base();
545 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
547 operator<(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
548 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
550 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
551 _M_message(__msg_iter_order_bad)
552 ._M_iterator(__lhs,
"lhs")
553 ._M_iterator(__rhs,
"rhs"));
554 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
555 _M_message(__msg_order_different)
556 ._M_iterator(__lhs,
"lhs")
557 ._M_iterator(__rhs,
"rhs"));
558 return __lhs.base() < __rhs.base();
561 template<
typename _Iterator,
typename _Sequence>
563 operator<(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
564 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
566 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
567 _M_message(__msg_iter_order_bad)
568 ._M_iterator(__lhs,
"lhs")
569 ._M_iterator(__rhs,
"rhs"));
570 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
571 _M_message(__msg_order_different)
572 ._M_iterator(__lhs,
"lhs")
573 ._M_iterator(__rhs,
"rhs"));
574 return __lhs.base() < __rhs.base();
577 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
579 operator<=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
580 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
582 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
583 _M_message(__msg_iter_order_bad)
584 ._M_iterator(__lhs,
"lhs")
585 ._M_iterator(__rhs,
"rhs"));
586 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
587 _M_message(__msg_order_different)
588 ._M_iterator(__lhs,
"lhs")
589 ._M_iterator(__rhs,
"rhs"));
590 return __lhs.base() <= __rhs.base();
593 template<
typename _Iterator,
typename _Sequence>
595 operator<=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
596 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
598 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
599 _M_message(__msg_iter_order_bad)
600 ._M_iterator(__lhs,
"lhs")
601 ._M_iterator(__rhs,
"rhs"));
602 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
603 _M_message(__msg_order_different)
604 ._M_iterator(__lhs,
"lhs")
605 ._M_iterator(__rhs,
"rhs"));
606 return __lhs.base() <= __rhs.base();
609 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
611 operator>(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
612 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
614 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
615 _M_message(__msg_iter_order_bad)
616 ._M_iterator(__lhs,
"lhs")
617 ._M_iterator(__rhs,
"rhs"));
618 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
619 _M_message(__msg_order_different)
620 ._M_iterator(__lhs,
"lhs")
621 ._M_iterator(__rhs,
"rhs"));
622 return __lhs.base() > __rhs.base();
625 template<
typename _Iterator,
typename _Sequence>
627 operator>(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
628 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
630 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
631 _M_message(__msg_iter_order_bad)
632 ._M_iterator(__lhs,
"lhs")
633 ._M_iterator(__rhs,
"rhs"));
634 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
635 _M_message(__msg_order_different)
636 ._M_iterator(__lhs,
"lhs")
637 ._M_iterator(__rhs,
"rhs"));
638 return __lhs.base() > __rhs.base();
641 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
643 operator>=(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
644 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
646 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
647 _M_message(__msg_iter_order_bad)
648 ._M_iterator(__lhs,
"lhs")
649 ._M_iterator(__rhs,
"rhs"));
650 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
651 _M_message(__msg_order_different)
652 ._M_iterator(__lhs,
"lhs")
653 ._M_iterator(__rhs,
"rhs"));
654 return __lhs.base() >= __rhs.base();
657 template<
typename _Iterator,
typename _Sequence>
659 operator>=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
660 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
662 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
663 _M_message(__msg_iter_order_bad)
664 ._M_iterator(__lhs,
"lhs")
665 ._M_iterator(__rhs,
"rhs"));
666 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
667 _M_message(__msg_order_different)
668 ._M_iterator(__lhs,
"lhs")
669 ._M_iterator(__rhs,
"rhs"));
670 return __lhs.base() >= __rhs.base();
677 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
678 inline typename _Safe_iterator<_IteratorL, _Sequence>::difference_type
679 operator-(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
680 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
682 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
683 _M_message(__msg_distance_bad)
684 ._M_iterator(__lhs,
"lhs")
685 ._M_iterator(__rhs,
"rhs"));
686 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
687 _M_message(__msg_distance_different)
688 ._M_iterator(__lhs,
"lhs")
689 ._M_iterator(__rhs,
"rhs"));
690 return __lhs.base() - __rhs.base();
693 template<
typename _Iterator,
typename _Sequence>
694 inline typename _Safe_iterator<_Iterator, _Sequence>::difference_type
695 operator-(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
696 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
698 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
699 _M_message(__msg_distance_bad)
700 ._M_iterator(__lhs,
"lhs")
701 ._M_iterator(__rhs,
"rhs"));
702 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
703 _M_message(__msg_distance_different)
704 ._M_iterator(__lhs,
"lhs")
705 ._M_iterator(__rhs,
"rhs"));
706 return __lhs.base() - __rhs.base();
709 template<
typename _Iterator,
typename _Sequence>
710 inline _Safe_iterator<_Iterator, _Sequence>
711 operator+(
typename _Safe_iterator<_Iterator,_Sequence>::difference_type __n,
712 const _Safe_iterator<_Iterator, _Sequence>& __i)
713 {
return __i + __n; }
716 template<
typename _Iterator>
717 struct __is_safe_random_iterator
719 enum { __value = 0 };
720 typedef std::__false_type __type;
723 template<
typename _Iterator,
typename _Sequence>
724 struct __is_safe_random_iterator<_Safe_iterator<_Iterator, _Sequence> >
725 : std::__are_same<std::random_access_iterator_tag,
726 typename std::iterator_traits<_Iterator>::
730 template<
typename _Iterator>
732 : std::_Iter_base<_Iterator, __is_safe_random_iterator<_Iterator>::__value>
741 template<
typename _Iterator>
742 inline typename _Siter_base<_Iterator>::iterator_type
744 {
return _Siter_base<_Iterator>::_S_base(__it); }
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
_Safe_iterator operator++(int)
Iterator postincrement.
bool _M_is_before_begin() const
Is this iterator equal to the sequence's before_begin() iterator if any?
_Safe_iterator operator--(int)
Iterator postdecrement.
void _M_attach(_Safe_sequence_base *__seq, bool __constant)
pointer operator->() const
Iterator dereference.
_Iterator base() const
Return the underlying iterator.
std::pair< typename std::iterator_traits< _Iterator1 >::difference_type, _Distance_precision > __get_distance(const _Iterator1 &__lhs, const _Iterator2 &__rhs, std::random_access_iterator_tag)
bool _M_before_dereferenceable() const
Is the iterator before a dereferenceable one?
Base class that supports tracking of iterators that reference a sequence.
void _M_attach_single(_Safe_sequence_base *__seq)
bool _M_is_end() const
Is this iterator equal to the sequence's end() iterator?
bool _M_is_beginnest() const
Is this iterator equal to the sequence's before_begin() iterator if any or begin() otherwise...
_Safe_iterator(const _Iterator &__i, const _Sequence *__seq)
Safe iterator construction from an unsafe iterator and its sequence.
_Safe_iterator & operator--()
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)
Converting constructor from a mutable iterator to a constant iterator.
void _M_attach(_Safe_sequence_base *__seq)
_Safe_sequence_base * _M_sequence
Struct holding two objects of arbitrary type.
Basic functionality for a safe iterator.
void _M_attach_single(_Safe_sequence_base *__seq, bool __constant)
Forward iterators support a superset of input iterator operations.
bool _M_dereferenceable() const
Is the iterator dereferenceable?
_Safe_iterator & operator=(_Safe_iterator &&__x)
Move assignment.
reference operator*() const
Iterator dereference.
_Safe_iterator & operator++()
Iterator preincrement.
bool _M_incrementable() const
Is the iterator incrementable?
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.
Random-access iterators support a superset of bidirectional iterator operations.
_Safe_iterator(_Safe_iterator &&__x)
Move construction.
_Safe_iterator(const _Safe_iterator &__x)
Copy construction.
bool _M_is_begin() const
Is this iterator equal to the sequence's begin() iterator?
_Safe_iterator & operator=(const _Safe_iterator &__x)
Copy assignment.