libstdc++
safe_iterator.h
Go to the documentation of this file.
1// Safe iterator implementation -*- C++ -*-
2
3// Copyright (C) 2003-2022 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file debug/safe_iterator.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
30#define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
31
32#include <debug/assertions.h>
33#include <debug/macros.h>
34#include <debug/functions.h>
35#include <debug/safe_base.h>
36#include <bits/stl_pair.h>
37#include <ext/type_traits.h>
38#if __cplusplus > 201703L
39# include <compare>
40#endif
41
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))
53
54#define _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(_Lhs, _Rhs) \
55 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_compare_bad, \
56 __msg_compare_different)
57
58#define _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(_Lhs, _Rhs) \
59 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_iter_order_bad, \
60 __msg_order_different)
61
62#define _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(_Lhs, _Rhs) \
63 _GLIBCXX_DEBUG_VERIFY_OPERANDS(_Lhs, _Rhs, __msg_distance_bad, \
64 __msg_distance_different)
65
66namespace __gnu_debug
67{
68 /** Helper struct to deal with sequence offering a before_begin
69 * iterator.
70 **/
71 template<typename _Sequence>
73 {
74 template<typename _Iterator, typename _Category>
75 static bool
77 { return false; }
78
79 template<typename _Iterator, typename _Category>
80 static bool
81 _S_Is_Beginnest(const _Safe_iterator<_Iterator, _Sequence, _Category>& __it)
82 { return __it.base() == __it._M_get_sequence()->_M_base().begin(); }
83 };
84
85 /** Sequence traits giving the size of a container if possible. */
86 template<typename _Sequence>
88 {
89 typedef _Distance_traits<typename _Sequence::iterator> _DistTraits;
90
91 static typename _DistTraits::__type
92 _S_size(const _Sequence& __seq)
93 { return std::make_pair(__seq.size(), __dp_exact); }
94 };
95
96 /** \brief Safe iterator wrapper.
97 *
98 * The class template %_Safe_iterator is a wrapper around an
99 * iterator that tracks the iterator's movement among sequences and
100 * checks that operations performed on the "safe" iterator are
101 * legal. In additional to the basic iterator operations (which are
102 * validated, and then passed to the underlying iterator),
103 * %_Safe_iterator has member functions for iterator invalidation,
104 * attaching/detaching the iterator from sequences, and querying
105 * the iterator's state.
106 *
107 * Note that _Iterator must be the first base class so that it gets
108 * initialized before the iterator is being attached to the container's list
109 * of iterators and it is being detached before _Iterator get
110 * destroyed. Otherwise it would result in a data race.
111 */
112 template<typename _Iterator, typename _Sequence, typename _Category
115 : private _Iterator,
117 {
118 typedef _Iterator _Iter_base;
120
122
123 protected:
124 typedef std::__are_same<typename _Sequence::_Base::const_iterator,
125 _Iterator> _IsConstant;
126
127 typedef typename __gnu_cxx::__conditional_type<
128 _IsConstant::__value,
129 typename _Sequence::_Base::iterator,
130 typename _Sequence::_Base::const_iterator>::__type _OtherIterator;
131
132 struct _Attach_single
133 { };
134
135 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
136 _GLIBCXX_NOEXCEPT
137 : _Iter_base(__i)
138 { _M_attach_single(__seq); }
139
140 public:
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;
147
148#if __cplusplus > 201703L && __cpp_lib_concepts
149 using iterator_concept = std::__detail::__iter_concept<_Iterator>;
150#endif
151
152 /// @post the iterator is singular and unattached
153 _Safe_iterator() _GLIBCXX_NOEXCEPT : _Iter_base() { }
154
155 /**
156 * @brief Safe iterator construction from an unsafe iterator and
157 * its sequence.
158 *
159 * @pre @p seq is not NULL
160 * @post this is not singular
161 */
162 _Safe_iterator(_Iterator __i, const _Safe_sequence_base* __seq)
163 _GLIBCXX_NOEXCEPT
164 : _Iter_base(__i), _Safe_base(__seq, _S_constant())
165 {
166 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
167 _M_message(__msg_init_singular)
168 ._M_iterator(*this, "this"));
169 }
170
171 /**
172 * @brief Copy construction.
173 */
174 _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
175 : _Iter_base(__x.base()), _Safe_base()
176 {
177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
178 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
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"));
184 _M_attach(__x._M_sequence);
185 }
186
187#if __cplusplus >= 201103L
188 /**
189 * @brief Move construction.
190 * @post __x is singular and unattached
191 */
193 : _Iter_base()
194 {
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"));
200 _Safe_sequence_base* __seq = __x._M_sequence;
201 __x._M_detach();
202 std::swap(base(), __x.base());
203 _M_attach(__seq);
204 }
205#endif
206
207 /**
208 * @brief Converting constructor from a mutable iterator to a
209 * constant iterator.
210 */
211 template<typename _MutableIterator>
213 const _Safe_iterator<_MutableIterator, _Sequence,
214 typename __gnu_cxx::__enable_if<_IsConstant::__value &&
215 std::__are_same<_MutableIterator, _OtherIterator>::__value,
216 _Category>::__type>& __x)
217 _GLIBCXX_NOEXCEPT
218 : _Iter_base(__x.base())
219 {
220 // _GLIBCXX_RESOLVE_LIB_DEFECTS
221 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
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"));
227 _M_attach(__x._M_sequence);
228 }
229
230 /**
231 * @brief Copy assignment.
232 */
234 operator=(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
235 {
236 // _GLIBCXX_RESOLVE_LIB_DEFECTS
237 // DR 408. Is vector<reverse_iterator<char*> > forbidden?
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"));
243
244 if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
245 {
247 base() = __x.base();
248 _M_version = __x._M_sequence->_M_version;
249 }
250 else
251 {
252 _M_detach();
253 base() = __x.base();
254 _M_attach(__x._M_sequence);
255 }
256
257 return *this;
258 }
259
260#if __cplusplus >= 201103L
261 /**
262 * @brief Move assignment.
263 * @post __x is singular and unattached
264 */
266 operator=(_Safe_iterator&& __x) noexcept
267 {
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"));
273
274 if (std::__addressof(__x) == this)
275 return *this;
276
277 if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
278 {
280 base() = __x.base();
281 _M_version = __x._M_sequence->_M_version;
282 }
283 else
284 {
285 _M_detach();
286 base() = __x.base();
287 _M_attach(__x._M_sequence);
288 }
289
290 __x._M_detach();
291 __x.base() = _Iterator();
292 return *this;
293 }
294#endif
295
296 /**
297 * @brief Iterator dereference.
298 * @pre iterator is dereferenceable
299 */
300 _GLIBCXX_NODISCARD
301 reference
302 operator*() const _GLIBCXX_NOEXCEPT
303 {
304 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
305 _M_message(__msg_bad_deref)
306 ._M_iterator(*this, "this"));
307 return *base();
308 }
309
310 /**
311 * @brief Iterator dereference.
312 * @pre iterator is dereferenceable
313 */
314 _GLIBCXX_NODISCARD
315 pointer
316 operator->() const _GLIBCXX_NOEXCEPT
317 {
318 _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
319 _M_message(__msg_bad_deref)
320 ._M_iterator(*this, "this"));
321 return base().operator->();
322 }
323
324 // ------ Input iterator requirements ------
325 /**
326 * @brief Iterator preincrement
327 * @pre iterator is incrementable
328 */
330 operator++() _GLIBCXX_NOEXCEPT
331 {
332 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
333 _M_message(__msg_bad_inc)
334 ._M_iterator(*this, "this"));
336 ++base();
337 return *this;
338 }
339
340 /**
341 * @brief Iterator postincrement
342 * @pre iterator is incrementable
343 */
345 operator++(int) _GLIBCXX_NOEXCEPT
346 {
347 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
348 _M_message(__msg_bad_inc)
349 ._M_iterator(*this, "this"));
351 return _Safe_iterator(base()++, this->_M_sequence, _Attach_single());
352 }
353
354 // ------ Utilities ------
355
356 /// Determine if this is a constant iterator.
357 static _GLIBCXX_CONSTEXPR bool
359 { return _IsConstant::__value; }
360
361 /**
362 * @brief Return the underlying iterator
363 */
364 _Iterator&
365 base() _GLIBCXX_NOEXCEPT { return *this; }
366
367 const _Iterator&
368 base() const _GLIBCXX_NOEXCEPT { return *this; }
369
370 /**
371 * @brief Conversion to underlying non-debug iterator to allow
372 * better interaction with non-debug containers.
373 */
374 operator _Iterator() const _GLIBCXX_NOEXCEPT { return *this; }
375
376 /** Attach iterator to the given sequence. */
377 void
380
381 /** Likewise, but not thread-safe. */
382 void
385
386 /// Is the iterator dereferenceable?
387 bool
389 { return !this->_M_singular() && !_M_is_end() && !_M_is_before_begin(); }
390
391 /// Is the iterator before a dereferenceable one?
392 bool
394 {
395 if (this->_M_incrementable())
396 {
397 _Iterator __base = base();
398 return ++__base != _M_get_sequence()->_M_base().end();
399 }
400 return false;
401 }
402
403 /// Is the iterator incrementable?
404 bool
406 { return !this->_M_singular() && !_M_is_end(); }
407
408 /// Is the iterator value-initialized?
409 bool
411 { return _M_version == 0 && base() == _Iter_base(); }
412
413 // Can we advance the iterator @p __n steps (@p __n may be negative)
414 bool
415 _M_can_advance(difference_type __n, bool __strict = false) const;
416
417 // Can we advance the iterator using @p __dist in @p __way direction.
418 template<typename _Diff>
419 bool
420 _M_can_advance(const std::pair<_Diff, _Distance_precision>& __dist,
421 int __way) const;
422
423 // Is the iterator range [*this, __rhs) valid?
424 bool
425 _M_valid_range(const _Safe_iterator& __rhs,
427 bool __check_dereferenceable = true) const;
428
429 // The sequence this iterator references.
430 typename __gnu_cxx::__conditional_type<
431 _IsConstant::__value, const _Sequence*, _Sequence*>::__type
432 _M_get_sequence() const
433 { return static_cast<_Sequence*>(_M_sequence); }
434
435 // Get distance to __rhs.
436 typename _Distance_traits<_Iterator>::__type
437 _M_get_distance_to(const _Safe_iterator& __rhs) const;
438
439 // Get distance from sequence begin up to *this.
440 typename _Distance_traits<_Iterator>::__type
441 _M_get_distance_from_begin() const;
442
443 // Get distance from *this to sequence end.
444 typename _Distance_traits<_Iterator>::__type
445 _M_get_distance_to_end() const;
446
447 /// Is this iterator equal to the sequence's begin() iterator?
448 bool
450 { return base() == _M_get_sequence()->_M_base().begin(); }
451
452 /// Is this iterator equal to the sequence's end() iterator?
453 bool
454 _M_is_end() const
455 { return base() == _M_get_sequence()->_M_base().end(); }
456
457 /// Is this iterator equal to the sequence's before_begin() iterator if
458 /// any?
459 bool
461 { return _BeforeBeginHelper<_Sequence>::_S_Is(*this); }
462
463 /// Is this iterator equal to the sequence's before_begin() iterator if
464 /// any or begin() otherwise?
465 bool
468
469 // ------ Operators ------
470
472
473 _GLIBCXX_NODISCARD
474 friend bool
475 operator==(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
476 {
477 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
478 return __lhs.base() == __rhs.base();
479 }
480
481 template<typename _IteR>
482 _GLIBCXX_NODISCARD
483 friend bool
484 operator==(const _Self& __lhs,
485 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
486 _GLIBCXX_NOEXCEPT
487 {
488 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
489 return __lhs.base() == __rhs.base();
490 }
491
492#if ! __cpp_lib_three_way_comparison
493 _GLIBCXX_NODISCARD
494 friend bool
495 operator!=(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
496 {
497 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
498 return __lhs.base() != __rhs.base();
499 }
500
501 template<typename _IteR>
502 _GLIBCXX_NODISCARD
503 friend bool
504 operator!=(const _Self& __lhs,
505 const _Safe_iterator<_IteR, _Sequence, iterator_category>& __rhs)
506 _GLIBCXX_NOEXCEPT
507 {
508 _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS(__lhs, __rhs);
509 return __lhs.base() != __rhs.base();
510 }
511#endif // three-way comparison
512 };
513
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>
517 {
518 typedef _Safe_iterator<_Iterator, _Sequence,
519 std::forward_iterator_tag> _Safe_base;
520
521 protected:
522 typedef typename _Safe_base::_OtherIterator _OtherIterator;
523 typedef typename _Safe_base::_Attach_single _Attach_single;
524
525 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
526 _GLIBCXX_NOEXCEPT
527 : _Safe_base(__i, __seq, _Attach_single())
528 { }
529
530 public:
531 /// @post the iterator is singular and unattached
532 _Safe_iterator() _GLIBCXX_NOEXCEPT { }
533
534 /**
535 * @brief Safe iterator construction from an unsafe iterator and
536 * its sequence.
537 *
538 * @pre @p seq is not NULL
539 * @post this is not singular
540 */
541 _Safe_iterator(_Iterator __i, const _Safe_sequence_base* __seq)
542 _GLIBCXX_NOEXCEPT
543 : _Safe_base(__i, __seq)
544 { }
545
546 /**
547 * @brief Copy construction.
548 */
549 _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
550 : _Safe_base(__x)
551 { }
552
553#if __cplusplus >= 201103L
554 /** @brief Move construction. */
555 _Safe_iterator(_Safe_iterator&&) = default;
556#endif
557
558 /**
559 * @brief Converting constructor from a mutable iterator to a
560 * constant iterator.
561 */
562 template<typename _MutableIterator>
564 const _Safe_iterator<_MutableIterator, _Sequence,
565 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
566 std::__are_same<_MutableIterator, _OtherIterator>::__value,
567 std::bidirectional_iterator_tag>::__type>& __x)
568 _GLIBCXX_NOEXCEPT
569 : _Safe_base(__x)
570 { }
571
572#if __cplusplus >= 201103L
573 /** @brief Copy assignment. */
575 operator=(const _Safe_iterator&) = default;
576
577 /** @brief Move assignment. */
579 operator=(_Safe_iterator&&) = default;
580#else
581 /** @brief Copy assignment. */
583 operator=(const _Safe_iterator& __x)
584 {
585 _Safe_base::operator=(__x);
586 return *this;
587 }
588#endif
589
590 // ------ Input iterator requirements ------
591 /**
592 * @brief Iterator preincrement
593 * @pre iterator is incrementable
594 */
596 operator++() _GLIBCXX_NOEXCEPT
597 {
598 _Safe_base::operator++();
599 return *this;
600 }
601
602 /**
603 * @brief Iterator postincrement
604 * @pre iterator is incrementable
605 */
607 operator++(int) _GLIBCXX_NOEXCEPT
608 {
609 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
610 _M_message(__msg_bad_inc)
611 ._M_iterator(*this, "this"));
613 return _Safe_iterator(this->base()++, this->_M_sequence,
614 _Attach_single());
615 }
616
617 // ------ Bidirectional iterator requirements ------
618 /**
619 * @brief Iterator predecrement
620 * @pre iterator is decrementable
621 */
623 operator--() _GLIBCXX_NOEXCEPT
624 {
625 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
626 _M_message(__msg_bad_dec)
627 ._M_iterator(*this, "this"));
629 --this->base();
630 return *this;
631 }
632
633 /**
634 * @brief Iterator postdecrement
635 * @pre iterator is decrementable
636 */
638 operator--(int) _GLIBCXX_NOEXCEPT
639 {
640 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
641 _M_message(__msg_bad_dec)
642 ._M_iterator(*this, "this"));
644 return _Safe_iterator(this->base()--, this->_M_sequence,
645 _Attach_single());
646 }
647
648 // ------ Utilities ------
649
650 // Is the iterator decrementable?
651 bool
652 _M_decrementable() const
653 { return !this->_M_singular() && !this->_M_is_begin(); }
654 };
655
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>
660 {
661 typedef _Safe_iterator<_Iterator, _Sequence,
663 typedef typename _Safe_base::_OtherIterator _OtherIterator;
664
665 typedef typename _Safe_base::_Self _Self;
666 typedef _Safe_iterator<_OtherIterator, _Sequence,
668
669 typedef typename _Safe_base::_Attach_single _Attach_single;
670
671 _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single)
672 _GLIBCXX_NOEXCEPT
673 : _Safe_base(__i, __seq, _Attach_single())
674 { }
675
676 public:
677 typedef typename _Safe_base::difference_type difference_type;
678 typedef typename _Safe_base::reference reference;
679
680 /// @post the iterator is singular and unattached
681 _Safe_iterator() _GLIBCXX_NOEXCEPT { }
682
683 /**
684 * @brief Safe iterator construction from an unsafe iterator and
685 * its sequence.
686 *
687 * @pre @p seq is not NULL
688 * @post this is not singular
689 */
690 _Safe_iterator(_Iterator __i, const _Safe_sequence_base* __seq)
691 _GLIBCXX_NOEXCEPT
692 : _Safe_base(__i, __seq)
693 { }
694
695 /**
696 * @brief Copy construction.
697 */
698 _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
699 : _Safe_base(__x)
700 { }
701
702#if __cplusplus >= 201103L
703 /** @brief Move construction. */
704 _Safe_iterator(_Safe_iterator&&) = default;
705#endif
706
707 /**
708 * @brief Converting constructor from a mutable iterator to a
709 * constant iterator.
710 */
711 template<typename _MutableIterator>
713 const _Safe_iterator<_MutableIterator, _Sequence,
714 typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
715 std::__are_same<_MutableIterator, _OtherIterator>::__value,
716 std::random_access_iterator_tag>::__type>& __x)
717 _GLIBCXX_NOEXCEPT
718 : _Safe_base(__x)
719 { }
720
721#if __cplusplus >= 201103L
722 /** @brief Copy assignment. */
724 operator=(const _Safe_iterator&) = default;
725
726 /** @brief Move assignment. */
728 operator=(_Safe_iterator&&) = default;
729#else
730 /** @brief Copy assignment. */
732 operator=(const _Safe_iterator& __x)
733 {
734 _Safe_base::operator=(__x);
735 return *this;
736 }
737#endif
738
739 // Is the iterator range [*this, __rhs) valid?
740 bool
741 _M_valid_range(const _Safe_iterator& __rhs,
742 std::pair<difference_type,
743 _Distance_precision>& __dist) const;
744
745 // ------ Input iterator requirements ------
746 /**
747 * @brief Iterator preincrement
748 * @pre iterator is incrementable
749 */
751 operator++() _GLIBCXX_NOEXCEPT
752 {
753 _Safe_base::operator++();
754 return *this;
755 }
756
757 /**
758 * @brief Iterator postincrement
759 * @pre iterator is incrementable
760 */
762 operator++(int) _GLIBCXX_NOEXCEPT
763 {
764 _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
765 _M_message(__msg_bad_inc)
766 ._M_iterator(*this, "this"));
768 return _Safe_iterator(this->base()++, this->_M_sequence,
769 _Attach_single());
770 }
771
772 // ------ Bidirectional iterator requirements ------
773 /**
774 * @brief Iterator predecrement
775 * @pre iterator is decrementable
776 */
778 operator--() _GLIBCXX_NOEXCEPT
779 {
780 _Safe_base::operator--();
781 return *this;
782 }
783
784 /**
785 * @brief Iterator postdecrement
786 * @pre iterator is decrementable
787 */
789 operator--(int) _GLIBCXX_NOEXCEPT
790 {
791 _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
792 _M_message(__msg_bad_dec)
793 ._M_iterator(*this, "this"));
795 return _Safe_iterator(this->base()--, this->_M_sequence,
796 _Attach_single());
797 }
798
799 // ------ Random access iterator requirements ------
800 _GLIBCXX_NODISCARD
801 reference
802 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
803 {
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];
809 }
810
812 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
813 {
814 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
815 _M_message(__msg_advance_oob)
816 ._M_iterator(*this)._M_integer(__n));
818 this->base() += __n;
819 return *this;
820 }
821
823 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
824 {
825 _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
826 _M_message(__msg_retreat_oob)
827 ._M_iterator(*this)._M_integer(__n));
829 this->base() -= __n;
830 return *this;
831 }
832
833#if __cpp_lib_three_way_comparison
834 [[nodiscard]]
835 friend auto
836 operator<=>(const _Self& __lhs, const _Self& __rhs) noexcept
837 {
838 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
839 return __lhs.base() <=> __rhs.base();
840 }
841
842 [[nodiscard]]
843 friend auto
844 operator<=>(const _Self& __lhs, const _OtherSelf& __rhs) noexcept
845 {
846 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
847 return __lhs.base() <=> __rhs.base();
848 }
849#else
850 _GLIBCXX_NODISCARD
851 friend bool
852 operator<(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
853 {
854 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
855 return __lhs.base() < __rhs.base();
856 }
857
858 _GLIBCXX_NODISCARD
859 friend bool
860 operator<(const _Self& __lhs, const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
861 {
862 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
863 return __lhs.base() < __rhs.base();
864 }
865
866 _GLIBCXX_NODISCARD
867 friend bool
868 operator<=(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
869 {
870 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
871 return __lhs.base() <= __rhs.base();
872 }
873
874 _GLIBCXX_NODISCARD
875 friend bool
876 operator<=(const _Self& __lhs, const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
877 {
878 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
879 return __lhs.base() <= __rhs.base();
880 }
881
882 _GLIBCXX_NODISCARD
883 friend bool
884 operator>(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
885 {
886 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
887 return __lhs.base() > __rhs.base();
888 }
889
890 _GLIBCXX_NODISCARD
891 friend bool
892 operator>(const _Self& __lhs, const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
893 {
894 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
895 return __lhs.base() > __rhs.base();
896 }
897
898 _GLIBCXX_NODISCARD
899 friend bool
900 operator>=(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
901 {
902 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
903 return __lhs.base() >= __rhs.base();
904 }
905
906 _GLIBCXX_NODISCARD
907 friend bool
908 operator>=(const _Self& __lhs, const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
909 {
910 _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS(__lhs, __rhs);
911 return __lhs.base() >= __rhs.base();
912 }
913#endif // three-way comparison
914
915 // _GLIBCXX_RESOLVE_LIB_DEFECTS
916 // According to the resolution of DR179 not only the various comparison
917 // operators but also operator- must accept mixed iterator/const_iterator
918 // parameters.
919 _GLIBCXX_NODISCARD
920 friend difference_type
921 operator-(const _Self& __lhs, const _OtherSelf& __rhs) _GLIBCXX_NOEXCEPT
922 {
923 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
924 return __lhs.base() - __rhs.base();
925 }
926
927 _GLIBCXX_NODISCARD
928 friend difference_type
929 operator-(const _Self& __lhs, const _Self& __rhs) _GLIBCXX_NOEXCEPT
930 {
931 _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS(__lhs, __rhs);
932 return __lhs.base() - __rhs.base();
933 }
934
935 _GLIBCXX_NODISCARD
936 friend _Self
937 operator+(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
938 {
939 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
940 _M_message(__msg_advance_oob)
941 ._M_iterator(__x)._M_integer(__n));
942 return _Safe_iterator(__x.base() + __n, __x._M_sequence);
943 }
944
945 _GLIBCXX_NODISCARD
946 friend _Self
947 operator+(difference_type __n, const _Self& __x) _GLIBCXX_NOEXCEPT
948 {
949 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(__n),
950 _M_message(__msg_advance_oob)
951 ._M_iterator(__x)._M_integer(__n));
952 return _Safe_iterator(__n + __x.base(), __x._M_sequence);
953 }
954
955 _GLIBCXX_NODISCARD
956 friend _Self
957 operator-(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
958 {
959 _GLIBCXX_DEBUG_VERIFY(__x._M_can_advance(-__n),
960 _M_message(__msg_retreat_oob)
961 ._M_iterator(__x)._M_integer(__n));
962 return _Safe_iterator(__x.base() - __n, __x._M_sequence);
963 }
964 };
965
966 /** Safe iterators know how to check if they form a valid range. */
967 template<typename _Iterator, typename _Sequence, typename _Category>
968 inline bool
969 __valid_range(const _Safe_iterator<_Iterator, _Sequence,
970 _Category>& __first,
971 const _Safe_iterator<_Iterator, _Sequence,
972 _Category>& __last,
974 { return __first._M_valid_range(__last, __dist); }
975
976 template<typename _Iterator, typename _Sequence, typename _Category>
977 inline bool
978 __valid_range(const _Safe_iterator<_Iterator, _Sequence,
979 _Category>& __first,
980 const _Safe_iterator<_Iterator, _Sequence,
981 _Category>& __last)
982 {
983 typename _Distance_traits<_Iterator>::__type __dist;
984 return __first._M_valid_range(__last, __dist);
985 }
986
987 template<typename _Iterator, typename _Sequence, typename _Category,
988 typename _Size>
989 inline bool
990 __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
991 _Size __n)
992 { return __it._M_can_advance(__n); }
993
994 template<typename _Iterator, typename _Sequence, typename _Category,
995 typename _Diff>
996 inline bool
997 __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
999 int __way)
1000 { return __it._M_can_advance(__dist, __way); }
1001
1002 template<typename _Iterator, typename _Sequence>
1003 _Iterator
1004 __base(const _Safe_iterator<_Iterator, _Sequence,
1006 { return __it.base(); }
1007
1008#if __cplusplus < 201103L
1009 template<typename _Iterator, typename _Sequence>
1010 struct _Unsafe_type<_Safe_iterator<_Iterator, _Sequence> >
1011 { typedef _Iterator _Type; };
1012#endif
1013
1014 template<typename _Iterator, typename _Sequence>
1015 inline _Iterator
1016 __unsafe(const _Safe_iterator<_Iterator, _Sequence>& __it)
1017 { return __it.base(); }
1018
1019} // namespace __gnu_debug
1020
1021#if __cplusplus >= 201103L && __cplusplus <= 201703L
1022namespace std _GLIBCXX_VISIBILITY(default)
1023{
1024_GLIBCXX_BEGIN_NAMESPACE_VERSION
1025
1026 template<typename _Iterator, typename _Container, typename _Sequence>
1027 constexpr auto
1028 __to_address(const __gnu_debug::_Safe_iterator<
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()); }
1033
1034_GLIBCXX_END_NAMESPACE_VERSION
1035}
1036#endif
1037
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
1042
1043#include <debug/safe_iterator.tcc>
1044
1045#endif
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition: complex:365
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:335
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
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)
Safe iterator wrapper.
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(_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.
Definition: stl_pair.h:189
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.
Definition: safe_base.h:51
_Safe_sequence_base * _M_sequence
Definition: safe_base.h:57
__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.
Definition: safe_base.h:189
Scoped lock idiom.
Definition: concurrence.h:229