libstdc++
helper_functions.h
Go to the documentation of this file.
1// Debugging support implementation -*- C++ -*-
2
3// Copyright (C) 2003-2024 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/helper_functions.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H
30#define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1
31
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
36
37#include <bits/stl_pair.h> // for pair
38
39namespace __gnu_debug
40{
41 template<typename _Iterator, typename _Sequence, typename _Category>
42 class _Safe_iterator;
43
44#if __cplusplus >= 201103L
45 template<typename _Iterator, typename _Sequence>
46 class _Safe_local_iterator;
47#endif
48
49 /** The precision to which we can calculate the distance between
50 * two iterators.
51 */
53 {
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
59 };
60
61 template<typename _Iterator,
62 typename = typename std::__is_integer<_Iterator>::__type>
63 struct _Distance_traits
64 {
65 private:
66 typedef
68
69 template<typename _DiffType, typename = _DiffType> // PR c++/85282
70 struct _DiffTraits
71 { typedef _DiffType __type; };
72
73 template<typename _DiffType>
74 struct _DiffTraits<_DiffType, void>
75 { typedef std::ptrdiff_t __type; };
76
77 typedef typename _DiffTraits<_ItDiffType>::__type _DiffType;
78
79 public:
81 };
82
83 template<typename _Integral>
84 struct _Distance_traits<_Integral, std::__true_type>
86
87 /** Determine the distance between two iterators with some known
88 * precision.
89 */
90 template<typename _Iterator>
91 _GLIBCXX_CONSTEXPR
92 inline typename _Distance_traits<_Iterator>::__type
93 __get_distance(_Iterator __lhs, _Iterator __rhs,
95 { return std::make_pair(__rhs - __lhs, __dp_exact); }
96
97 template<typename _Iterator>
98 _GLIBCXX14_CONSTEXPR
99 inline typename _Distance_traits<_Iterator>::__type
100 __get_distance(_Iterator __lhs, _Iterator __rhs,
102 {
103 if (__lhs == __rhs)
104 return std::make_pair(0, __dp_exact);
105
106 return std::make_pair(1, __dp_equality);
107 }
108
109 template<typename _Iterator>
110 _GLIBCXX_CONSTEXPR
111 inline typename _Distance_traits<_Iterator>::__type
112 __get_distance(_Iterator __lhs, _Iterator __rhs)
113 {
114 return __gnu_debug::__get_distance(__lhs, __rhs,
116 }
117
118 // An arbitrary iterator pointer is not singular.
119 inline bool
120 __check_singular_aux(const void*) { return false; }
121
122 // Defined in <debug/safe_base.h>
123 bool
124 __check_singular_aux(const class _Safe_iterator_base*);
125
126 // We may have an iterator that derives from _Safe_iterator_base but isn't
127 // a _Safe_iterator.
128 template<typename _Iterator>
129 _GLIBCXX_CONSTEXPR
130 inline bool
131 __check_singular(_Iterator const& __x)
132 {
133 return ! std::__is_constant_evaluated()
134 && __gnu_debug::__check_singular_aux(std::__addressof(__x));
135 }
136
137 /** Non-NULL pointers are nonsingular. */
138 template<typename _Tp>
139 _GLIBCXX_CONSTEXPR
140 inline bool
141 __check_singular(_Tp* const& __ptr)
142 { return __ptr == 0; }
143
144 /** We say that integral types for a valid range, and defer to other
145 * routines to realize what to do with integral types instead of
146 * iterators.
147 */
148 template<typename _Integral>
149 _GLIBCXX_CONSTEXPR
150 inline bool
151 __valid_range_aux(_Integral, _Integral, std::__true_type)
152 { return true; }
153
154 template<typename _Integral>
155 _GLIBCXX20_CONSTEXPR
156 inline bool
157 __valid_range_aux(_Integral, _Integral,
158 typename _Distance_traits<_Integral>::__type& __dist,
159 std::__true_type)
160 {
161 __dist = std::make_pair(0, __dp_none);
162 return true;
163 }
164
165 template<typename _InputIterator>
166 _GLIBCXX_CONSTEXPR
167 inline bool
168 __valid_range_aux(_InputIterator __first, _InputIterator __last,
170 {
171 return __first == __last
172 || (!__gnu_debug::__check_singular(__first)
173 && !__gnu_debug::__check_singular(__last));
174 }
175
176 template<typename _InputIterator>
177 _GLIBCXX_CONSTEXPR
178 inline bool
179 __valid_range_aux(_InputIterator __first, _InputIterator __last,
181 {
182 return __gnu_debug::__valid_range_aux(__first, __last,
184 && __first <= __last;
185 }
186
187 /** We have iterators, so figure out what kind of iterators they are
188 * to see if we can check the range ahead of time.
189 */
190 template<typename _InputIterator>
191 _GLIBCXX_CONSTEXPR
192 inline bool
193 __valid_range_aux(_InputIterator __first, _InputIterator __last,
194 std::__false_type)
195 {
196 return __gnu_debug::__valid_range_aux(__first, __last,
197 std::__iterator_category(__first));
198 }
199
200 template<typename _InputIterator>
201 _GLIBCXX20_CONSTEXPR
202 inline bool
203 __valid_range_aux(_InputIterator __first, _InputIterator __last,
204 typename _Distance_traits<_InputIterator>::__type& __dist,
205 std::__false_type)
206 {
207 if (!__gnu_debug::__valid_range_aux(__first, __last,
209 return false;
210
211 __dist = __gnu_debug::__get_distance(__first, __last);
212 switch (__dist.second)
213 {
214 case __dp_none:
215 break;
216 case __dp_equality:
217 if (__dist.first == 0)
218 return true;
219 break;
220 case __dp_sign:
221 case __dp_sign_max_size:
222 case __dp_exact:
223 return __dist.first >= 0;
224 }
225
226 // Can't tell so assume it is fine.
227 return true;
228 }
229
230 /** Don't know what these iterators are, or if they are even
231 * iterators (we may get an integral type for InputIterator), so
232 * see if they are integral and pass them on to the next phase
233 * otherwise.
234 */
235 template<typename _InputIterator>
236 _GLIBCXX20_CONSTEXPR
237 inline bool
238 __valid_range(_InputIterator __first, _InputIterator __last,
240 {
241 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
242 return __gnu_debug::__valid_range_aux(__first, __last, __dist,
243 _Integral());
244 }
245
246 template<typename _Iterator, typename _Sequence, typename _Category>
247 bool
248 __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
249 const _Safe_iterator<_Iterator, _Sequence, _Category>&,
250 typename _Distance_traits<_Iterator>::__type&);
251
252#if __cplusplus >= 201103L
253 template<typename _Iterator,typename _Sequence>
254 bool
255 __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
256 const _Safe_local_iterator<_Iterator, _Sequence>&,
257 typename _Distance_traits<_Iterator>::__type&);
258#endif
259
260 template<typename _InputIterator>
261 _GLIBCXX14_CONSTEXPR
262 inline bool
263 __valid_range(_InputIterator __first, _InputIterator __last)
264 {
265 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
266 return __gnu_debug::__valid_range_aux(__first, __last, _Integral());
267 }
268
269 template<typename _Iterator, typename _Sequence, typename _Category>
270 bool
271 __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
272 const _Safe_iterator<_Iterator, _Sequence, _Category>&);
273
274#if __cplusplus >= 201103L
275 template<typename _Iterator, typename _Sequence>
276 bool
277 __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
278 const _Safe_local_iterator<_Iterator, _Sequence>&);
279#endif
280
281 // Fallback method, always ok.
282 template<typename _InputIterator, typename _Size>
283 _GLIBCXX_CONSTEXPR
284 inline bool
285 __can_advance(_InputIterator, _Size)
286 { return true; }
287
288 template<typename _Iterator, typename _Sequence, typename _Category,
289 typename _Size>
290 bool
291 __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
292 _Size);
293
294 template<typename _InputIterator, typename _Diff>
295 _GLIBCXX_CONSTEXPR
296 inline bool
297 __can_advance(_InputIterator, const std::pair<_Diff, _Distance_precision>&, int)
298 { return true; }
299
300 template<typename _Iterator, typename _Sequence, typename _Category,
301 typename _Diff>
302 bool
303 __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
305
306 /** Helper function to extract base iterator of random access safe iterator
307 * in order to reduce performance impact of debug mode. Limited to random
308 * access iterator because it is the only category for which it is possible
309 * to check for correct iterators order in the __valid_range function
310 * thanks to the < operator.
311 */
312 template<typename _Iterator>
313 _GLIBCXX_CONSTEXPR
314 inline _Iterator
315 __base(_Iterator __it)
316 { return __it; }
317
318#if __cplusplus < 201103L
319 template<typename _Iterator>
320 struct _Unsafe_type
321 { typedef _Iterator _Type; };
322#endif
323
324 /* Remove debug mode safe iterator layer, if any. */
325 template<typename _Iterator>
326 _GLIBCXX_CONSTEXPR
327 inline _Iterator
328 __unsafe(_Iterator __it)
329 { return __it; }
330}
331
332#endif
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1146
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
GNU debug classes for public use.
constexpr bool __valid_range_aux(_Integral, _Integral, std::__true_type)
constexpr _Distance_traits< _Iterator >::__type __get_distance(_Iterator __lhs, _Iterator __rhs, std::random_access_iterator_tag)
constexpr bool __valid_range(_InputIterator __first, _InputIterator __last, typename _Distance_traits< _InputIterator >::__type &__dist)
constexpr _Iterator __base(_Iterator __it)
Struct holding two objects of arbitrary type.
Definition stl_pair.h:286
Marking input iterators.
Random-access iterators support a superset of bidirectional iterator operations.
Traits class for iterators.