1 // Debugging support implementation -*- C++ -*-
3 // Copyright (C) 2003-2022 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file debug/helper_functions.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H
30 #define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1
32 #include <bits/move.h> // for __addressof
33 #include <bits/stl_iterator_base_types.h> // for iterator_traits,
34 // categories and _Iter_base
35 #include <bits/cpp_type_traits.h> // for __is_integer
37 #include <bits/stl_pair.h> // for pair
41 template<typename _Iterator
, typename _Sequence
, typename _Category
>
44 #if __cplusplus >= 201103L
45 template<typename _Iterator
, typename _Sequence
>
46 class _Safe_local_iterator
;
49 /** The precision to which we can calculate the distance between
52 enum _Distance_precision
54 __dp_none
, // Not even an iterator type
55 __dp_equality
, //< Can compare iterator equality, only
56 __dp_sign
, //< Can determine equality and ordering
57 __dp_sign_max_size
, //< __dp_sign and gives max range size
58 __dp_exact
//< Can determine distance precisely
61 template<typename _Iterator
,
62 typename
= typename
std::__is_integer
<_Iterator
>::__type
>
63 struct _Distance_traits
67 typename
std::iterator_traits
<_Iterator
>::difference_type _ItDiffType
;
69 template<typename _DiffType
,
70 typename
= typename
std::__is_void
<_DiffType
>::__type
>
72 { typedef _DiffType __type
; };
74 template<typename _DiffType
>
75 struct _DiffTraits
<_DiffType
, std::__true_type
>
76 { typedef std::ptrdiff_t __type
; };
78 typedef typename _DiffTraits
<_ItDiffType
>::__type _DiffType
;
81 typedef std::pair
<_DiffType
, _Distance_precision
> __type
;
84 template<typename _Integral
>
85 struct _Distance_traits
<_Integral
, std::__true_type
>
86 { typedef std::pair
<std::ptrdiff_t, _Distance_precision
> __type
; };
88 /** Determine the distance between two iterators with some known
91 template<typename _Iterator
>
93 inline typename _Distance_traits
<_Iterator
>::__type
94 __get_distance(_Iterator __lhs
, _Iterator __rhs
,
95 std::random_access_iterator_tag
)
96 { return std::make_pair(__rhs
- __lhs
, __dp_exact
); }
98 template<typename _Iterator
>
100 inline typename _Distance_traits
<_Iterator
>::__type
101 __get_distance(_Iterator __lhs
, _Iterator __rhs
,
102 std::input_iterator_tag
)
105 return std::make_pair(0, __dp_exact
);
107 return std::make_pair(1, __dp_equality
);
110 template<typename _Iterator
>
112 inline typename _Distance_traits
<_Iterator
>::__type
113 __get_distance(_Iterator __lhs
, _Iterator __rhs
)
114 { return __get_distance(__lhs
, __rhs
, std::__iterator_category(__lhs
)); }
116 // An arbitrary iterator pointer is not singular.
118 __check_singular_aux(const void*) { return false; }
120 // We may have an iterator that derives from _Safe_iterator_base but isn't
122 template<typename _Iterator
>
125 __check_singular(_Iterator
const& __x
)
127 return ! std::__is_constant_evaluated()
128 && __check_singular_aux(std::__addressof(__x
));
131 /** Non-NULL pointers are nonsingular. */
132 template<typename _Tp
>
135 __check_singular(_Tp
* const& __ptr
)
136 { return __ptr
== 0; }
138 /** We say that integral types for a valid range, and defer to other
139 * routines to realize what to do with integral types instead of
142 template<typename _Integral
>
145 __valid_range_aux(_Integral
, _Integral
, std::__true_type
)
148 template<typename _Integral
>
151 __valid_range_aux(_Integral
, _Integral
,
152 typename _Distance_traits
<_Integral
>::__type
& __dist
,
155 __dist
= std::make_pair(0, __dp_none
);
159 template<typename _InputIterator
>
162 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
163 std::input_iterator_tag
)
165 return __first
== __last
166 || (!__check_singular(__first
) && !__check_singular(__last
));
169 template<typename _InputIterator
>
172 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
173 std::random_access_iterator_tag
)
176 __valid_range_aux(__first
, __last
, std::input_iterator_tag())
177 && __first
<= __last
;
180 /** We have iterators, so figure out what kind of iterators they are
181 * to see if we can check the range ahead of time.
183 template<typename _InputIterator
>
186 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
189 return __valid_range_aux(__first
, __last
,
190 std::__iterator_category(__first
));
193 template<typename _InputIterator
>
196 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
197 typename _Distance_traits
<_InputIterator
>::__type
& __dist
,
200 if (!__valid_range_aux(__first
, __last
, std::input_iterator_tag()))
203 __dist
= __get_distance(__first
, __last
);
204 switch (__dist
.second
)
209 if (__dist
.first
== 0)
213 case __dp_sign_max_size
:
215 return __dist
.first
>= 0;
218 // Can't tell so assume it is fine.
222 /** Don't know what these iterators are, or if they are even
223 * iterators (we may get an integral type for InputIterator), so
224 * see if they are integral and pass them on to the next phase
227 template<typename _InputIterator
>
230 __valid_range(_InputIterator __first
, _InputIterator __last
,
231 typename _Distance_traits
<_InputIterator
>::__type
& __dist
)
233 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
234 return __valid_range_aux(__first
, __last
, __dist
, _Integral());
237 template<typename _Iterator
, typename _Sequence
, typename _Category
>
239 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
240 const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
241 typename _Distance_traits
<_Iterator
>::__type
&);
243 #if __cplusplus >= 201103L
244 template<typename _Iterator
,typename _Sequence
>
246 __valid_range(const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
247 const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
248 typename _Distance_traits
<_Iterator
>::__type
&);
251 template<typename _InputIterator
>
254 __valid_range(_InputIterator __first
, _InputIterator __last
)
256 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
257 return __valid_range_aux(__first
, __last
, _Integral());
260 template<typename _Iterator
, typename _Sequence
, typename _Category
>
262 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
263 const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&);
265 #if __cplusplus >= 201103L
266 template<typename _Iterator
, typename _Sequence
>
268 __valid_range(const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
269 const _Safe_local_iterator
<_Iterator
, _Sequence
>&);
272 // Fallback method, always ok.
273 template<typename _InputIterator
, typename _Size
>
276 __can_advance(_InputIterator
, _Size
)
279 template<typename _Iterator
, typename _Sequence
, typename _Category
,
282 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
285 template<typename _InputIterator
, typename _Diff
>
288 __can_advance(_InputIterator
, const std::pair
<_Diff
, _Distance_precision
>&, int)
291 template<typename _Iterator
, typename _Sequence
, typename _Category
,
294 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
295 const std::pair
<_Diff
, _Distance_precision
>&, int);
297 /** Helper function to extract base iterator of random access safe iterator
298 * in order to reduce performance impact of debug mode. Limited to random
299 * access iterator because it is the only category for which it is possible
300 * to check for correct iterators order in the __valid_range function
301 * thanks to the < operator.
303 template<typename _Iterator
>
306 __base(_Iterator __it
)
309 #if __cplusplus < 201103L
310 template<typename _Iterator
>
312 { typedef _Iterator _Type
; };
315 /* Remove debug mode safe iterator layer, if any. */
316 template<typename _Iterator
>
318 __unsafe(_Iterator __it
)