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