libstdc++
safe_iterator.tcc
Go to the documentation of this file.
1// Debugging iterator implementation (out of line) -*- C++ -*-
2
3// Copyright (C) 2003-2023 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.tcc
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
30#define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
31
32#include <bits/stl_algobase.h>
33
34namespace __gnu_debug
35{
36 template<typename _Iterator, typename _Sequence, typename _Category>
37 typename _Distance_traits<_Iterator>::__type
38 _Safe_iterator<_Iterator, _Sequence, _Category>::
39 _M_get_distance_from_begin() const
40 {
41 typedef _Sequence_traits<_Sequence> _SeqTraits;
42
43 // No need to consider before_begin as this function is only used in
44 // _M_can_advance which won't be used for forward_list iterators.
45 if (_M_is_begin())
46 return std::make_pair(0, __dp_exact);
47
48 if (_M_is_end())
49 return _SeqTraits::_S_size(*_M_get_sequence());
50
51 typename _Distance_traits<_Iterator>::__type __res
52 = __get_distance(_M_get_sequence()->_M_base().begin(), base());
53
54 if (__res.second == __dp_equality)
55 return std::make_pair(1, __dp_sign);
56
57 return __res;
58 }
59
60 template<typename _Iterator, typename _Sequence, typename _Category>
61 typename _Distance_traits<_Iterator>::__type
62 _Safe_iterator<_Iterator, _Sequence, _Category>::
63 _M_get_distance_to_end() const
64 {
65 typedef _Sequence_traits<_Sequence> _SeqTraits;
66
67 // No need to consider before_begin as this function is only used in
68 // _M_can_advance which won't be used for forward_list iterators.
69 if (_M_is_begin())
70 return _SeqTraits::_S_size(*_M_get_sequence());
71
72 if (_M_is_end())
73 return std::make_pair(0, __dp_exact);
74
75 typename _Distance_traits<_Iterator>::__type __res
76 = __get_distance(base(), _M_get_sequence()->_M_base().end());
77
78 if (__res.second == __dp_equality)
79 return std::make_pair(1, __dp_sign);
80
81 return __res;
82 }
83
84 template<typename _Iterator, typename _Sequence, typename _Category>
85 bool
86 _Safe_iterator<_Iterator, _Sequence, _Category>::
87 _M_can_advance(difference_type __n, bool __strict) const
88 {
89 if (this->_M_value_initialized() && __n == 0)
90 return true;
91
92 if (this->_M_singular())
93 return false;
94
95 if (__n == 0)
96 return true;
97
99 ? _M_get_distance_from_begin()
100 : _M_get_distance_to_end();
101
102 if (__n < 0)
103 __n = -__n;
104
105 return __dist.second > __dp_sign
106 ? __dist.first >= __n
107 : !__strict && __dist.first > 0;
108 }
109
110 template<typename _Iterator, typename _Sequence, typename _Category>
111 template<typename _Diff>
112 bool
113 _Safe_iterator<_Iterator, _Sequence, _Category>::
114 _M_can_advance(const std::pair<_Diff, _Distance_precision>& __dist,
115 int __way) const
116 {
117 return __dist.second == __dp_exact
118 ? _M_can_advance(__way * __dist.first)
119 : _M_can_advance(__way * (__dist.first == 0
120 ? 0
121 : __dist.first < 0 ? -1 : 1));
122 }
123
124 template<typename _Iterator, typename _Sequence, typename _Category>
125 typename _Distance_traits<_Iterator>::__type
126 _Safe_iterator<_Iterator, _Sequence, _Category>::
127 _M_get_distance_to(const _Safe_iterator& __rhs) const
128 {
129 typedef typename _Distance_traits<_Iterator>::__type _Dist;
130 typedef _Sequence_traits<_Sequence> _SeqTraits;
131
132 _Dist __base_dist = __get_distance(this->base(), __rhs.base());
133 if (__base_dist.second == __dp_exact)
134 return __base_dist;
135
136 _Dist __seq_dist = _SeqTraits::_S_size(*this->_M_get_sequence());
137 if (this->_M_is_before_begin())
138 {
139 if (__rhs._M_is_begin())
140 return std::make_pair(1, __dp_exact);
141
142 return __seq_dist.second == __dp_exact
143 ? std::make_pair(__seq_dist.first + 1, __dp_exact)
144 : __seq_dist;
145 }
146
147 if (this->_M_is_begin())
148 {
149 if (__rhs._M_is_before_begin())
150 return std::make_pair(-1, __dp_exact);
151
152 if (__rhs._M_is_end())
153 return __seq_dist;
154
155 return std::make_pair(__seq_dist.first,
156 __seq_dist.second == __dp_exact
157 ? __dp_sign_max_size : __seq_dist.second);
158 }
159
160 if (this->_M_is_end())
161 {
162 if (__rhs._M_is_before_begin())
163 return __seq_dist.second == __dp_exact
164 ? std::make_pair(-__seq_dist.first - 1, __dp_exact)
165 : std::make_pair(-__seq_dist.first, __dp_sign);
166
167 if (__rhs._M_is_begin())
168 return std::make_pair(-__seq_dist.first, __seq_dist.second);
169
170 return std::make_pair(-__seq_dist.first,
171 __seq_dist.second == __dp_exact
172 ? __dp_sign_max_size : __seq_dist.second);
173 }
174
175 if (__rhs._M_is_before_begin())
176 return __seq_dist.second == __dp_exact
177 ? std::make_pair(__seq_dist.first - 1, __dp_exact)
178 : std::make_pair(-__seq_dist.first, __dp_sign);
179
180 if (__rhs._M_is_begin())
181 return std::make_pair(-__seq_dist.first,
182 __seq_dist.second == __dp_exact
183 ? __dp_sign_max_size : __seq_dist.second);
184
185 if (__rhs._M_is_end())
186 return std::make_pair(__seq_dist.first,
187 __seq_dist.second == __dp_exact
188 ? __dp_sign_max_size : __seq_dist.second);
189
190 return std::make_pair(1, __dp_equality);
191 }
192
193 template<typename _Iterator, typename _Sequence, typename _Category>
194 bool
195 _Safe_iterator<_Iterator, _Sequence, _Category>::
196 _M_valid_range(const _Safe_iterator& __rhs,
198 bool __check_dereferenceable) const
199 {
200 if (_M_value_initialized() && __rhs._M_value_initialized())
201 {
202 __dist = std::make_pair(0, __dp_exact);
203 return true;
204 }
205
206 if (_M_singular() || __rhs._M_singular() || !_M_can_compare(__rhs))
207 return false;
208
209 /* Determine iterators order */
210 __dist = _M_get_distance_to(__rhs);
211 if (__dist.second != __dp_equality)
212 {
213 // If range is not empty first iterator must be dereferenceable.
214 return __dist.first == 0
215 || (__dist.first > 0
216 && (!__check_dereferenceable || _M_dereferenceable()));
217 }
218
219 // Assume that this is a valid range; we can't check anything else.
220 return true;
221 }
222
223 template<typename _Iterator, typename _Sequence>
224 bool
225 _Safe_iterator<_Iterator, _Sequence, std::random_access_iterator_tag>::
226 _M_valid_range(const _Safe_iterator& __rhs,
227 std::pair<difference_type,
228 _Distance_precision>& __dist) const
229 {
230 if (this->_M_value_initialized() && __rhs._M_value_initialized())
231 {
232 __dist = std::make_pair(0, __dp_exact);
233 return true;
234 }
235
236 if (this->_M_singular() || __rhs._M_singular()
237 || !this->_M_can_compare(__rhs))
238 return false;
239
240 /* Determine iterators order */
241 __dist = std::make_pair(__rhs.base() - this->base(), __dp_exact);
242
243 // If range is not empty first iterator must be dereferenceable.
244 return __dist.first == 0
245 || (__dist.first > 0 && this->_M_dereferenceable());
246 }
247} // namespace __gnu_debug
248
249namespace std _GLIBCXX_VISIBILITY(default)
250{
251_GLIBCXX_BEGIN_NAMESPACE_VERSION
252
253 template<typename _Ite, typename _Seq>
254 _Ite
255 __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
257 { return __it.base(); }
258
259 template<bool _IsMove,
260 typename _Ite, typename _Seq, typename _Cat, typename _OI>
261 _OI
262 __copy_move_a(
263 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
264 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __last,
265 _OI __result)
266 {
267 typename ::__gnu_debug::_Distance_traits<_Ite>::__type __dist;
268 __glibcxx_check_valid_range2(__first, __last, __dist);
269 __glibcxx_check_can_increment_dist(__result, __dist, 1);
270
271 if (__dist.second > ::__gnu_debug::__dp_equality)
272 return std::__copy_move_a<_IsMove>(__first.base(), __last.base(),
273 __result);
274
275 return std::__copy_move_a1<_IsMove>(__first, __last, __result);
276 }
277
278 template<bool _IsMove,
279 typename _II, typename _Ite, typename _Seq, typename _Cat>
281 __copy_move_a(_II __first, _II __last,
282 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __result)
283 {
284 typename ::__gnu_debug::_Distance_traits<_II>::__type __dist;
285 __glibcxx_check_valid_range2(__first, __last, __dist);
286 __glibcxx_check_can_increment_dist(__result, __dist, 1);
287
288 if (__dist.second > ::__gnu_debug::__dp_sign
289 && __result._M_can_advance(__dist.first, true))
290 return ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>(
291 std::__copy_move_a<_IsMove>(__first, __last, __result.base()),
292 __result._M_sequence);
293
294 return std::__copy_move_a1<_IsMove>(__first, __last, __result);
295 }
296
297 template<bool _IsMove,
298 typename _IIte, typename _ISeq, typename _ICat,
299 typename _OIte, typename _OSeq, typename _OCat>
301 __copy_move_a(
302 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>& __first,
303 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>& __last,
304 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>& __result)
305 {
306 typename ::__gnu_debug::_Distance_traits<_IIte>::__type __dist;
307 __glibcxx_check_valid_range2(__first, __last, __dist);
308 __glibcxx_check_can_increment_dist(__result, __dist, 1);
309
310 if (__dist.second > ::__gnu_debug::__dp_equality)
311 {
312 if (__dist.second > ::__gnu_debug::__dp_sign
313 && __result._M_can_advance(__dist.first, true))
314 return ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>(
315 std::__copy_move_a<_IsMove>(__first.base(), __last.base(),
316 __result.base()),
317 __result._M_sequence);
318
319 return std::__copy_move_a<_IsMove>(__first.base(), __last.base(),
320 __result);
321 }
322
323 return std::__copy_move_a1<_IsMove>(__first, __last, __result);
324 }
325
326 template<bool _IsMove,
327 typename _Ite, typename _Seq, typename _Cat, typename _OI>
328 _OI
329 __copy_move_backward_a(
330 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
331 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __last,
332 _OI __result)
333 {
334 typename ::__gnu_debug::_Distance_traits<_Ite>::__type __dist;
335 __glibcxx_check_valid_range2(__first, __last, __dist);
336 __glibcxx_check_can_increment_dist(__result, __dist, -1);
337
338 if (__dist.second > ::__gnu_debug::__dp_equality)
340 __first.base(), __last.base(), __result);
341
342 return std::__copy_move_backward_a1<_IsMove>(__first, __last, __result);
343 }
344
345 template<bool _IsMove,
346 typename _II, typename _Ite, typename _Seq, typename _Cat>
348 __copy_move_backward_a(_II __first, _II __last,
349 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __result)
350 {
351 typename ::__gnu_debug::_Distance_traits<_II>::__type __dist;
352 __glibcxx_check_valid_range2(__first, __last, __dist);
353 __glibcxx_check_can_increment_dist(__result, __dist, -1);
354
355 if (__dist.second > ::__gnu_debug::__dp_sign
356 && __result._M_can_advance(-__dist.first, true))
357 return ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>(
359 __result.base()),
360 __result._M_sequence);
361
362 return std::__copy_move_backward_a1<_IsMove>(__first, __last, __result);
363 }
364
365 template<bool _IsMove,
366 typename _IIte, typename _ISeq, typename _ICat,
367 typename _OIte, typename _OSeq, typename _OCat>
369 __copy_move_backward_a(
370 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>& __first,
371 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>& __last,
372 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>& __result)
373 {
374 typename ::__gnu_debug::_Distance_traits<_IIte>::__type __dist;
375 __glibcxx_check_valid_range2(__first, __last, __dist);
376 __glibcxx_check_can_increment_dist(__result, __dist, -1);
377
378 if (__dist.second > ::__gnu_debug::__dp_equality)
379 {
380 if (__dist.second > ::__gnu_debug::__dp_sign
381 && __result._M_can_advance(-__dist.first, true))
382 return ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>(
383 std::__copy_move_backward_a<_IsMove>(__first.base(), __last.base(),
384 __result.base()),
385 __result._M_sequence);
386
388 __first.base(), __last.base(), __result);
389 }
390
391 return std::__copy_move_backward_a1<_IsMove>(__first, __last, __result);
392 }
393
394 template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
395 void
396 __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
397 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __last,
398 const _Tp& __value)
399 {
400 typename ::__gnu_debug::_Distance_traits<_Ite>::__type __dist;
401 __glibcxx_check_valid_range2(__first, __last, __dist);
402
403 if (__dist.second > ::__gnu_debug::__dp_equality)
404 std::__fill_a(__first.base(), __last.base(), __value);
405
406 std::__fill_a1(__first, __last, __value);
407 }
408
409 template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
410 typename _Tp>
412 __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
413 _Size __n, const _Tp& __value,
415 {
416#if __cplusplus >= 201103L
417 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
418#endif
419
420 if (__n <= 0)
421 return __first;
422
423 __glibcxx_check_can_increment(__first, __n);
424 if (__first._M_can_advance(__n, true))
425 return ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>(
426 std::__fill_n_a(__first.base(), __n, __value, _Cat()),
427 __first._M_sequence);
428
429 return std::__fill_n_a1(__first, __n, __value);
430 }
431
432 template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
433 bool
434 __equal_aux(
435 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>& __first1,
436 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>& __last1,
437 _II2 __first2)
438 {
439 typename ::__gnu_debug::_Distance_traits<_II1>::__type __dist;
440 __glibcxx_check_valid_range2(__first1, __last1, __dist);
441 __glibcxx_check_can_increment_dist(__first2, __dist, 1);
442
443 if (__dist.second > ::__gnu_debug::__dp_equality)
444 return std::__equal_aux(__first1.base(), __last1.base(), __first2);
445
446 return std::__equal_aux1(__first1, __last1, __first2);
447 }
448
449 template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
450 bool
451 __equal_aux(_II1 __first1, _II1 __last1,
452 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>& __first2)
453 {
454 typename ::__gnu_debug::_Distance_traits<_II1>::__type __dist;
455 __glibcxx_check_valid_range2(__first1, __last1, __dist);
456 __glibcxx_check_can_increment_dist(__first2, __dist, 1);
457
458 if (__dist.second > ::__gnu_debug::__dp_sign
459 && __first2._M_can_advance(__dist.first, true))
460 return std::__equal_aux(__first1, __last1, __first2.base());
461
462 return std::__equal_aux1(__first1, __last1, __first2);
463 }
464
465 template<typename _II1, typename _Seq1, typename _Cat1,
466 typename _II2, typename _Seq2, typename _Cat2>
467 bool
468 __equal_aux(
469 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>& __first1,
470 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>& __last1,
471 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>& __first2)
472 {
473 typename ::__gnu_debug::_Distance_traits<_II1>::__type __dist;
474 __glibcxx_check_valid_range2(__first1, __last1, __dist);
475 __glibcxx_check_can_increment_dist(__first2, __dist, 1);
476
477 if (__dist.second > ::__gnu_debug::__dp_equality)
478 {
479 if (__dist.second > ::__gnu_debug::__dp_sign &&
480 __first2._M_can_advance(__dist.first, true))
481 return std::__equal_aux(__first1.base(), __last1.base(),
482 __first2.base());
483 return std::__equal_aux(__first1.base(), __last1.base(), __first2);
484 }
485
486 return __equal_aux1(__first1, __last1, __first2);
487 }
488
489 template<typename _Ite1, typename _Seq1, typename _Cat1,
490 typename _II2>
491 bool
492 __lexicographical_compare_aux(
493 const ::__gnu_debug::_Safe_iterator<_Ite1, _Seq1, _Cat1>& __first1,
494 const ::__gnu_debug::_Safe_iterator<_Ite1, _Seq1, _Cat1>& __last1,
495 _II2 __first2, _II2 __last2)
496 {
497 typename ::__gnu_debug::_Distance_traits<_Ite1>::__type __dist1;
498 __glibcxx_check_valid_range2(__first1, __last1, __dist1);
499 __glibcxx_check_valid_range(__first2, __last2);
500
501 if (__dist1.second > ::__gnu_debug::__dp_equality)
502 return std::__lexicographical_compare_aux(__first1.base(),
503 __last1.base(),
504 __first2, __last2);
505 return std::__lexicographical_compare_aux1(__first1, __last1,
506 __first2, __last2);
507 }
508
509 template<typename _II1,
510 typename _Ite2, typename _Seq2, typename _Cat2>
511 bool
512 __lexicographical_compare_aux(
513 _II1 __first1, _II1 __last1,
514 const ::__gnu_debug::_Safe_iterator<_Ite2, _Seq2, _Cat2>& __first2,
515 const ::__gnu_debug::_Safe_iterator<_Ite2, _Seq2, _Cat2>& __last2)
516 {
517 __glibcxx_check_valid_range(__first1, __last1);
518 typename ::__gnu_debug::_Distance_traits<_II1>::__type __dist2;
519 __glibcxx_check_valid_range2(__first2, __last2, __dist2);
520
521 if (__dist2.second > ::__gnu_debug::__dp_equality)
522 return std::__lexicographical_compare_aux(__first1, __last1,
523 __first2.base(),
524 __last2.base());
525 return std::__lexicographical_compare_aux1(__first1, __last1,
526 __first2, __last2);
527 }
528
529 template<typename _Ite1, typename _Seq1, typename _Cat1,
530 typename _Ite2, typename _Seq2, typename _Cat2>
531 bool
532 __lexicographical_compare_aux(
533 const ::__gnu_debug::_Safe_iterator<_Ite1, _Seq1, _Cat1>& __first1,
534 const ::__gnu_debug::_Safe_iterator<_Ite1, _Seq1, _Cat1>& __last1,
535 const ::__gnu_debug::_Safe_iterator<_Ite2, _Seq2, _Cat2>& __first2,
536 const ::__gnu_debug::_Safe_iterator<_Ite2, _Seq2, _Cat2>& __last2)
537 {
538 typename ::__gnu_debug::_Distance_traits<_Ite1>::__type __dist1;
539 __glibcxx_check_valid_range2(__first1, __last1, __dist1);
540 typename ::__gnu_debug::_Distance_traits<_Ite2>::__type __dist2;
541 __glibcxx_check_valid_range2(__first2, __last2, __dist2);
542
543 if (__dist1.second > ::__gnu_debug::__dp_equality)
544 {
545 if (__dist2.second > ::__gnu_debug::__dp_equality)
546 return std::__lexicographical_compare_aux(__first1.base(),
547 __last1.base(),
548 __first2.base(),
549 __last2.base());
550 return std::__lexicographical_compare_aux(__first1.base(),
551 __last1.base(),
552 __first2, __last2);
553 }
554
555 if (__dist2.second > ::__gnu_debug::__dp_equality)
556 return std::__lexicographical_compare_aux(__first1, __last1,
557 __first2.base(),
558 __last2.base());
559 return std::__lexicographical_compare_aux1(__first1, __last1,
560 __first2, __last2);
561 }
562
563_GLIBCXX_END_NAMESPACE_VERSION
564} // namespace std
565
566#endif
ISO C++ entities toplevel namespace is std.
GNU debug classes for public use.
constexpr _Distance_traits< _Iterator >::__type __get_distance(_Iterator __lhs, _Iterator __rhs, std::random_access_iterator_tag)
Safe iterator wrapper.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:189
Marking input iterators.
Random-access iterators support a superset of bidirectional iterator operations.