helper_functions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/helper_functions.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H
  24. #define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1
  25. #include <bits/move.h> // for __addressof
  26. #include <bits/stl_iterator_base_types.h> // for iterator_traits,
  27. // categories and _Iter_base
  28. #include <bits/cpp_type_traits.h> // for __is_integer
  29. #include <bits/stl_pair.h> // for pair
  30. namespace __gnu_debug
  31. {
  32. template<typename _Iterator, typename _Sequence, typename _Category>
  33. class _Safe_iterator;
  34. #if __cplusplus >= 201103L
  35. template<typename _Iterator, typename _Sequence>
  36. class _Safe_local_iterator;
  37. #endif
  38. /** The precision to which we can calculate the distance between
  39. * two iterators.
  40. */
  41. enum _Distance_precision
  42. {
  43. __dp_none, // Not even an iterator type
  44. __dp_equality, //< Can compare iterator equality, only
  45. __dp_sign, //< Can determine equality and ordering
  46. __dp_sign_max_size, //< __dp_sign and gives max range size
  47. __dp_exact //< Can determine distance precisely
  48. };
  49. template<typename _Iterator,
  50. typename = typename std::__is_integer<_Iterator>::__type>
  51. struct _Distance_traits
  52. {
  53. private:
  54. typedef
  55. typename std::iterator_traits<_Iterator>::difference_type _ItDiffType;
  56. template<typename _DiffType,
  57. typename = typename std::__is_void<_DiffType>::__type>
  58. struct _DiffTraits
  59. { typedef _DiffType __type; };
  60. template<typename _DiffType>
  61. struct _DiffTraits<_DiffType, std::__true_type>
  62. { typedef std::ptrdiff_t __type; };
  63. typedef typename _DiffTraits<_ItDiffType>::__type _DiffType;
  64. public:
  65. typedef std::pair<_DiffType, _Distance_precision> __type;
  66. };
  67. template<typename _Integral>
  68. struct _Distance_traits<_Integral, std::__true_type>
  69. { typedef std::pair<std::ptrdiff_t, _Distance_precision> __type; };
  70. /** Determine the distance between two iterators with some known
  71. * precision.
  72. */
  73. template<typename _Iterator>
  74. _GLIBCXX_CONSTEXPR
  75. inline typename _Distance_traits<_Iterator>::__type
  76. __get_distance(_Iterator __lhs, _Iterator __rhs,
  77. std::random_access_iterator_tag)
  78. { return std::make_pair(__rhs - __lhs, __dp_exact); }
  79. template<typename _Iterator>
  80. _GLIBCXX14_CONSTEXPR
  81. inline typename _Distance_traits<_Iterator>::__type
  82. __get_distance(_Iterator __lhs, _Iterator __rhs,
  83. std::input_iterator_tag)
  84. {
  85. if (__lhs == __rhs)
  86. return std::make_pair(0, __dp_exact);
  87. return std::make_pair(1, __dp_equality);
  88. }
  89. template<typename _Iterator>
  90. _GLIBCXX_CONSTEXPR
  91. inline typename _Distance_traits<_Iterator>::__type
  92. __get_distance(_Iterator __lhs, _Iterator __rhs)
  93. {
  94. return __gnu_debug::__get_distance(__lhs, __rhs,
  95. std::__iterator_category(__lhs));
  96. }
  97. // An arbitrary iterator pointer is not singular.
  98. inline bool
  99. __check_singular_aux(const void*) { return false; }
  100. // Defined in <debug/safe_base.h>
  101. bool
  102. __check_singular_aux(const class _Safe_iterator_base*);
  103. // We may have an iterator that derives from _Safe_iterator_base but isn't
  104. // a _Safe_iterator.
  105. template<typename _Iterator>
  106. _GLIBCXX_CONSTEXPR
  107. inline bool
  108. __check_singular(_Iterator const& __x)
  109. {
  110. return ! std::__is_constant_evaluated()
  111. && __gnu_debug::__check_singular_aux(std::__addressof(__x));
  112. }
  113. /** Non-NULL pointers are nonsingular. */
  114. template<typename _Tp>
  115. _GLIBCXX_CONSTEXPR
  116. inline bool
  117. __check_singular(_Tp* const& __ptr)
  118. { return __ptr == 0; }
  119. /** We say that integral types for a valid range, and defer to other
  120. * routines to realize what to do with integral types instead of
  121. * iterators.
  122. */
  123. template<typename _Integral>
  124. _GLIBCXX_CONSTEXPR
  125. inline bool
  126. __valid_range_aux(_Integral, _Integral, std::__true_type)
  127. { return true; }
  128. template<typename _Integral>
  129. _GLIBCXX20_CONSTEXPR
  130. inline bool
  131. __valid_range_aux(_Integral, _Integral,
  132. typename _Distance_traits<_Integral>::__type& __dist,
  133. std::__true_type)
  134. {
  135. __dist = std::make_pair(0, __dp_none);
  136. return true;
  137. }
  138. template<typename _InputIterator>
  139. _GLIBCXX_CONSTEXPR
  140. inline bool
  141. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  142. std::input_iterator_tag)
  143. {
  144. return __first == __last
  145. || (!__gnu_debug::__check_singular(__first)
  146. && !__gnu_debug::__check_singular(__last));
  147. }
  148. template<typename _InputIterator>
  149. _GLIBCXX_CONSTEXPR
  150. inline bool
  151. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  152. std::random_access_iterator_tag)
  153. {
  154. return __gnu_debug::__valid_range_aux(__first, __last,
  155. std::input_iterator_tag())
  156. && __first <= __last;
  157. }
  158. /** We have iterators, so figure out what kind of iterators they are
  159. * to see if we can check the range ahead of time.
  160. */
  161. template<typename _InputIterator>
  162. _GLIBCXX_CONSTEXPR
  163. inline bool
  164. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  165. std::__false_type)
  166. {
  167. return __gnu_debug::__valid_range_aux(__first, __last,
  168. std::__iterator_category(__first));
  169. }
  170. template<typename _InputIterator>
  171. _GLIBCXX20_CONSTEXPR
  172. inline bool
  173. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  174. typename _Distance_traits<_InputIterator>::__type& __dist,
  175. std::__false_type)
  176. {
  177. if (!__gnu_debug::__valid_range_aux(__first, __last,
  178. std::input_iterator_tag()))
  179. return false;
  180. __dist = __gnu_debug::__get_distance(__first, __last);
  181. switch (__dist.second)
  182. {
  183. case __dp_none:
  184. break;
  185. case __dp_equality:
  186. if (__dist.first == 0)
  187. return true;
  188. break;
  189. case __dp_sign:
  190. case __dp_sign_max_size:
  191. case __dp_exact:
  192. return __dist.first >= 0;
  193. }
  194. // Can't tell so assume it is fine.
  195. return true;
  196. }
  197. /** Don't know what these iterators are, or if they are even
  198. * iterators (we may get an integral type for InputIterator), so
  199. * see if they are integral and pass them on to the next phase
  200. * otherwise.
  201. */
  202. template<typename _InputIterator>
  203. _GLIBCXX20_CONSTEXPR
  204. inline bool
  205. __valid_range(_InputIterator __first, _InputIterator __last,
  206. typename _Distance_traits<_InputIterator>::__type& __dist)
  207. {
  208. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  209. return __gnu_debug::__valid_range_aux(__first, __last, __dist,
  210. _Integral());
  211. }
  212. template<typename _Iterator, typename _Sequence, typename _Category>
  213. bool
  214. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  215. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  216. typename _Distance_traits<_Iterator>::__type&);
  217. #if __cplusplus >= 201103L
  218. template<typename _Iterator,typename _Sequence>
  219. bool
  220. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  221. const _Safe_local_iterator<_Iterator, _Sequence>&,
  222. typename _Distance_traits<_Iterator>::__type&);
  223. #endif
  224. template<typename _InputIterator>
  225. _GLIBCXX14_CONSTEXPR
  226. inline bool
  227. __valid_range(_InputIterator __first, _InputIterator __last)
  228. {
  229. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  230. return __gnu_debug::__valid_range_aux(__first, __last, _Integral());
  231. }
  232. template<typename _Iterator, typename _Sequence, typename _Category>
  233. bool
  234. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  235. const _Safe_iterator<_Iterator, _Sequence, _Category>&);
  236. #if __cplusplus >= 201103L
  237. template<typename _Iterator, typename _Sequence>
  238. bool
  239. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  240. const _Safe_local_iterator<_Iterator, _Sequence>&);
  241. #endif
  242. // Fallback method, always ok.
  243. template<typename _InputIterator, typename _Size>
  244. _GLIBCXX_CONSTEXPR
  245. inline bool
  246. __can_advance(_InputIterator, _Size)
  247. { return true; }
  248. template<typename _Iterator, typename _Sequence, typename _Category,
  249. typename _Size>
  250. bool
  251. __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  252. _Size);
  253. template<typename _InputIterator, typename _Diff>
  254. _GLIBCXX_CONSTEXPR
  255. inline bool
  256. __can_advance(_InputIterator, const std::pair<_Diff, _Distance_precision>&, int)
  257. { return true; }
  258. template<typename _Iterator, typename _Sequence, typename _Category,
  259. typename _Diff>
  260. bool
  261. __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  262. const std::pair<_Diff, _Distance_precision>&, int);
  263. /** Helper function to extract base iterator of random access safe iterator
  264. * in order to reduce performance impact of debug mode. Limited to random
  265. * access iterator because it is the only category for which it is possible
  266. * to check for correct iterators order in the __valid_range function
  267. * thanks to the < operator.
  268. */
  269. template<typename _Iterator>
  270. _GLIBCXX_CONSTEXPR
  271. inline _Iterator
  272. __base(_Iterator __it)
  273. { return __it; }
  274. #if __cplusplus < 201103L
  275. template<typename _Iterator>
  276. struct _Unsafe_type
  277. { typedef _Iterator _Type; };
  278. #endif
  279. /* Remove debug mode safe iterator layer, if any. */
  280. template<typename _Iterator>
  281. inline _Iterator
  282. __unsafe(_Iterator __it)
  283. { return __it; }
  284. }
  285. #endif