29 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
30 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
44 template <
typename _Sequence>
47 typedef typename _Sequence::const_iterator _It;
48 typedef typename _It::iterator_type _BaseIt;
51 _S_Is(_BaseIt,
const _Sequence*)
55 _S_Is_Beginnest(_BaseIt __it,
const _Sequence* __seq)
56 {
return __it == __seq->_M_base().begin(); }
80 template<
typename _Iterator1,
typename _Iterator2>
87 template<
typename _Iterator1,
typename _Iterator2>
94 template<
typename _Iterator1,
typename _Iterator2>
99 typedef typename std::iterator_traits<_Iterator1>::iterator_category
115 template<
typename _Iterator,
typename _Sequence>
116 class _Safe_iterator :
public _Safe_iterator_base
121 _Iterator _M_current;
127 typedef typename _Sequence::const_iterator const_iterator;
128 return std::__are_same<const_iterator, _Safe_iterator>::__value;
131 typedef std::iterator_traits<_Iterator> _Traits;
134 typedef _Iterator iterator_type;
135 typedef typename _Traits::iterator_category iterator_category;
136 typedef typename _Traits::value_type value_type;
137 typedef typename _Traits::difference_type difference_type;
138 typedef typename _Traits::reference reference;
139 typedef typename _Traits::pointer pointer;
155 _M_message(__msg_init_singular)
156 ._M_iterator(*
this,
"this"));
167 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
168 || __x._M_current == _Iterator(),
169 _M_message(__msg_init_copy_singular)
170 ._M_iterator(*
this,
"this")
171 ._M_iterator(__x,
"other"));
174 #if __cplusplus >= 201103L
181 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
182 || __x._M_current == _Iterator(),
183 _M_message(__msg_init_copy_singular)
184 ._M_iterator(*
this,
"this")
185 ._M_iterator(__x,
"other"));
186 std::swap(_M_current, __x._M_current);
196 template<
typename _MutableIterator>
199 typename __gnu_cxx::__enable_if<(std::__are_same<_MutableIterator,
200 typename _Sequence::iterator::iterator_type>::__value),
201 _Sequence>::__type>& __x)
206 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
207 || __x.base() == _Iterator(),
208 _M_message(__msg_init_const_singular)
209 ._M_iterator(*
this,
"this")
210 ._M_iterator(__x,
"other"));
221 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
222 || __x._M_current == _Iterator(),
223 _M_message(__msg_copy_singular)
224 ._M_iterator(*
this,
"this")
225 ._M_iterator(__x,
"other"));
226 _M_current = __x._M_current;
231 #if __cplusplus >= 201103L
239 _GLIBCXX_DEBUG_VERIFY(
this != &__x,
240 _M_message(__msg_self_move_assign)
241 ._M_iterator(*
this,
"this"));
242 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
243 || __x._M_current == _Iterator(),
244 _M_message(__msg_copy_singular)
245 ._M_iterator(*
this,
"this")
246 ._M_iterator(__x,
"other"));
247 _M_current = __x._M_current;
250 __x._M_current = _Iterator();
263 _M_message(__msg_bad_deref)
264 ._M_iterator(*
this,
"this"));
278 _M_message(__msg_bad_deref)
279 ._M_iterator(*
this,
"this"));
292 _M_message(__msg_bad_inc)
293 ._M_iterator(*
this,
"this"));
306 _M_message(__msg_bad_inc)
307 ._M_iterator(*
this,
"this"));
321 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
322 _M_message(__msg_bad_dec)
323 ._M_iterator(*
this,
"this"));
335 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
336 _M_message(__msg_bad_dec)
337 ._M_iterator(*
this,
"this"));
345 operator[](
const difference_type& __n)
const
347 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
348 && this->_M_can_advance(__n+1),
349 _M_message(__msg_iter_subscript_oob)
350 ._M_iterator(*this)._M_integer(__n));
352 return _M_current[__n];
356 operator+=(
const difference_type& __n)
358 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
359 _M_message(__msg_advance_oob)
360 ._M_iterator(*this)._M_integer(__n));
366 operator+(
const difference_type& __n)
const
374 operator-=(
const difference_type& __n)
376 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
377 _M_message(__msg_retreat_oob)
378 ._M_iterator(*this)._M_integer(__n));
384 operator-(
const difference_type& __n)
const
396 base()
const {
return _M_current; }
402 operator _Iterator()
const {
return _M_current; }
430 return ++__base != _M_get_sequence()->_M_base().end();
446 _M_can_advance(
const difference_type& __n)
const;
449 template<
typename _Other>
451 _M_valid_range(
const _Safe_iterator<_Other, _Sequence>& __rhs)
const;
455 _M_get_sequence()
const
456 {
return static_cast<const _Sequence*
>(
_M_sequence); }
460 {
return base() == _M_get_sequence()->_M_base().begin(); }
464 {
return base() == _M_get_sequence()->_M_base().end(); }
482 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
484 operator==(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
485 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
487 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
488 _M_message(__msg_iter_compare_bad)
489 ._M_iterator(__lhs,
"lhs")
490 ._M_iterator(__rhs,
"rhs"));
491 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
492 _M_message(__msg_compare_different)
493 ._M_iterator(__lhs,
"lhs")
494 ._M_iterator(__rhs,
"rhs"));
495 return __lhs.base() == __rhs.base();
498 template<
typename _Iterator,
typename _Sequence>
500 operator==(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
501 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)
519 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
520 _M_message(__msg_iter_compare_bad)
521 ._M_iterator(__lhs,
"lhs")
522 ._M_iterator(__rhs,
"rhs"));
523 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
524 _M_message(__msg_compare_different)
525 ._M_iterator(__lhs,
"lhs")
526 ._M_iterator(__rhs,
"rhs"));
527 return __lhs.base() != __rhs.base();
530 template<
typename _Iterator,
typename _Sequence>
532 operator!=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
533 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
535 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
536 _M_message(__msg_iter_compare_bad)
537 ._M_iterator(__lhs,
"lhs")
538 ._M_iterator(__rhs,
"rhs"));
539 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
540 _M_message(__msg_compare_different)
541 ._M_iterator(__lhs,
"lhs")
542 ._M_iterator(__rhs,
"rhs"));
543 return __lhs.base() != __rhs.base();
546 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
548 operator<(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
549 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
551 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
552 _M_message(__msg_iter_order_bad)
553 ._M_iterator(__lhs,
"lhs")
554 ._M_iterator(__rhs,
"rhs"));
555 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
556 _M_message(__msg_order_different)
557 ._M_iterator(__lhs,
"lhs")
558 ._M_iterator(__rhs,
"rhs"));
559 return __lhs.base() < __rhs.base();
562 template<
typename _Iterator,
typename _Sequence>
564 operator<(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
565 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
567 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
568 _M_message(__msg_iter_order_bad)
569 ._M_iterator(__lhs,
"lhs")
570 ._M_iterator(__rhs,
"rhs"));
571 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
572 _M_message(__msg_order_different)
573 ._M_iterator(__lhs,
"lhs")
574 ._M_iterator(__rhs,
"rhs"));
575 return __lhs.base() < __rhs.base();
578 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
580 operator<=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
581 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
583 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
584 _M_message(__msg_iter_order_bad)
585 ._M_iterator(__lhs,
"lhs")
586 ._M_iterator(__rhs,
"rhs"));
587 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
588 _M_message(__msg_order_different)
589 ._M_iterator(__lhs,
"lhs")
590 ._M_iterator(__rhs,
"rhs"));
591 return __lhs.base() <= __rhs.base();
594 template<
typename _Iterator,
typename _Sequence>
596 operator<=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
597 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
599 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
600 _M_message(__msg_iter_order_bad)
601 ._M_iterator(__lhs,
"lhs")
602 ._M_iterator(__rhs,
"rhs"));
603 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
604 _M_message(__msg_order_different)
605 ._M_iterator(__lhs,
"lhs")
606 ._M_iterator(__rhs,
"rhs"));
607 return __lhs.base() <= __rhs.base();
610 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
612 operator>(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
613 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
615 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
616 _M_message(__msg_iter_order_bad)
617 ._M_iterator(__lhs,
"lhs")
618 ._M_iterator(__rhs,
"rhs"));
619 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
620 _M_message(__msg_order_different)
621 ._M_iterator(__lhs,
"lhs")
622 ._M_iterator(__rhs,
"rhs"));
623 return __lhs.base() > __rhs.base();
626 template<
typename _Iterator,
typename _Sequence>
628 operator>(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
629 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
631 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
632 _M_message(__msg_iter_order_bad)
633 ._M_iterator(__lhs,
"lhs")
634 ._M_iterator(__rhs,
"rhs"));
635 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
636 _M_message(__msg_order_different)
637 ._M_iterator(__lhs,
"lhs")
638 ._M_iterator(__rhs,
"rhs"));
639 return __lhs.base() > __rhs.base();
642 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
644 operator>=(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
645 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
647 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
648 _M_message(__msg_iter_order_bad)
649 ._M_iterator(__lhs,
"lhs")
650 ._M_iterator(__rhs,
"rhs"));
651 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
652 _M_message(__msg_order_different)
653 ._M_iterator(__lhs,
"lhs")
654 ._M_iterator(__rhs,
"rhs"));
655 return __lhs.base() >= __rhs.base();
658 template<
typename _Iterator,
typename _Sequence>
660 operator>=(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
661 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
663 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
664 _M_message(__msg_iter_order_bad)
665 ._M_iterator(__lhs,
"lhs")
666 ._M_iterator(__rhs,
"rhs"));
667 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
668 _M_message(__msg_order_different)
669 ._M_iterator(__lhs,
"lhs")
670 ._M_iterator(__rhs,
"rhs"));
671 return __lhs.base() >= __rhs.base();
678 template<
typename _IteratorL,
typename _IteratorR,
typename _Sequence>
679 inline typename _Safe_iterator<_IteratorL, _Sequence>::difference_type
680 operator-(
const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
681 const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
683 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
684 _M_message(__msg_distance_bad)
685 ._M_iterator(__lhs,
"lhs")
686 ._M_iterator(__rhs,
"rhs"));
687 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
688 _M_message(__msg_distance_different)
689 ._M_iterator(__lhs,
"lhs")
690 ._M_iterator(__rhs,
"rhs"));
691 return __lhs.base() - __rhs.base();
694 template<
typename _Iterator,
typename _Sequence>
695 inline typename _Safe_iterator<_Iterator, _Sequence>::difference_type
696 operator-(
const _Safe_iterator<_Iterator, _Sequence>& __lhs,
697 const _Safe_iterator<_Iterator, _Sequence>& __rhs)
699 _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
700 _M_message(__msg_distance_bad)
701 ._M_iterator(__lhs,
"lhs")
702 ._M_iterator(__rhs,
"rhs"));
703 _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
704 _M_message(__msg_distance_different)
705 ._M_iterator(__lhs,
"lhs")
706 ._M_iterator(__rhs,
"rhs"));
707 return __lhs.base() - __rhs.base();
710 template<
typename _Iterator,
typename _Sequence>
711 inline _Safe_iterator<_Iterator, _Sequence>
712 operator+(
typename _Safe_iterator<_Iterator,_Sequence>::difference_type __n,
713 const _Safe_iterator<_Iterator, _Sequence>& __i)
714 {
return __i + __n; }