helper_functions.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2022 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. { return __get_distance(__lhs, __rhs, std::__iterator_category(__lhs)); }
  94. // An arbitrary iterator pointer is not singular.
  95. inline bool
  96. __check_singular_aux(const void*) { return false; }
  97. // We may have an iterator that derives from _Safe_iterator_base but isn't
  98. // a _Safe_iterator.
  99. template<typename _Iterator>
  100. _GLIBCXX_CONSTEXPR
  101. inline bool
  102. __check_singular(_Iterator const& __x)
  103. {
  104. return ! std::__is_constant_evaluated()
  105. && __check_singular_aux(std::__addressof(__x));
  106. }
  107. /** Non-NULL pointers are nonsingular. */
  108. template<typename _Tp>
  109. _GLIBCXX_CONSTEXPR
  110. inline bool
  111. __check_singular(_Tp* const& __ptr)
  112. { return __ptr == 0; }
  113. /** We say that integral types for a valid range, and defer to other
  114. * routines to realize what to do with integral types instead of
  115. * iterators.
  116. */
  117. template<typename _Integral>
  118. _GLIBCXX_CONSTEXPR
  119. inline bool
  120. __valid_range_aux(_Integral, _Integral, std::__true_type)
  121. { return true; }
  122. template<typename _Integral>
  123. _GLIBCXX20_CONSTEXPR
  124. inline bool
  125. __valid_range_aux(_Integral, _Integral,
  126. typename _Distance_traits<_Integral>::__type& __dist,
  127. std::__true_type)
  128. {
  129. __dist = std::make_pair(0, __dp_none);
  130. return true;
  131. }
  132. template<typename _InputIterator>
  133. _GLIBCXX_CONSTEXPR
  134. inline bool
  135. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  136. std::input_iterator_tag)
  137. {
  138. return __first == __last
  139. || (!__check_singular(__first) && !__check_singular(__last));
  140. }
  141. template<typename _InputIterator>
  142. _GLIBCXX_CONSTEXPR
  143. inline bool
  144. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  145. std::random_access_iterator_tag)
  146. {
  147. return
  148. __valid_range_aux(__first, __last, std::input_iterator_tag())
  149. && __first <= __last;
  150. }
  151. /** We have iterators, so figure out what kind of iterators they are
  152. * to see if we can check the range ahead of time.
  153. */
  154. template<typename _InputIterator>
  155. _GLIBCXX_CONSTEXPR
  156. inline bool
  157. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  158. std::__false_type)
  159. {
  160. return __valid_range_aux(__first, __last,
  161. std::__iterator_category(__first));
  162. }
  163. template<typename _InputIterator>
  164. _GLIBCXX20_CONSTEXPR
  165. inline bool
  166. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  167. typename _Distance_traits<_InputIterator>::__type& __dist,
  168. std::__false_type)
  169. {
  170. if (!__valid_range_aux(__first, __last, std::input_iterator_tag()))
  171. return false;
  172. __dist = __get_distance(__first, __last);
  173. switch (__dist.second)
  174. {
  175. case __dp_none:
  176. break;
  177. case __dp_equality:
  178. if (__dist.first == 0)
  179. return true;
  180. break;
  181. case __dp_sign:
  182. case __dp_sign_max_size:
  183. case __dp_exact:
  184. return __dist.first >= 0;
  185. }
  186. // Can't tell so assume it is fine.
  187. return true;
  188. }
  189. /** Don't know what these iterators are, or if they are even
  190. * iterators (we may get an integral type for InputIterator), so
  191. * see if they are integral and pass them on to the next phase
  192. * otherwise.
  193. */
  194. template<typename _InputIterator>
  195. _GLIBCXX20_CONSTEXPR
  196. inline bool
  197. __valid_range(_InputIterator __first, _InputIterator __last,
  198. typename _Distance_traits<_InputIterator>::__type& __dist)
  199. {
  200. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  201. return __valid_range_aux(__first, __last, __dist, _Integral());
  202. }
  203. template<typename _Iterator, typename _Sequence, typename _Category>
  204. bool
  205. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  206. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  207. typename _Distance_traits<_Iterator>::__type&);
  208. #if __cplusplus >= 201103L
  209. template<typename _Iterator,typename _Sequence>
  210. bool
  211. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  212. const _Safe_local_iterator<_Iterator, _Sequence>&,
  213. typename _Distance_traits<_Iterator>::__type&);
  214. #endif
  215. template<typename _InputIterator>
  216. _GLIBCXX14_CONSTEXPR
  217. inline bool
  218. __valid_range(_InputIterator __first, _InputIterator __last)
  219. {
  220. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  221. return __valid_range_aux(__first, __last, _Integral());
  222. }
  223. template<typename _Iterator, typename _Sequence, typename _Category>
  224. bool
  225. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  226. const _Safe_iterator<_Iterator, _Sequence, _Category>&);
  227. #if __cplusplus >= 201103L
  228. template<typename _Iterator, typename _Sequence>
  229. bool
  230. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  231. const _Safe_local_iterator<_Iterator, _Sequence>&);
  232. #endif
  233. // Fallback method, always ok.
  234. template<typename _InputIterator, typename _Size>
  235. _GLIBCXX_CONSTEXPR
  236. inline bool
  237. __can_advance(_InputIterator, _Size)
  238. { return true; }
  239. template<typename _Iterator, typename _Sequence, typename _Category,
  240. typename _Size>
  241. bool
  242. __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  243. _Size);
  244. template<typename _InputIterator, typename _Diff>
  245. _GLIBCXX_CONSTEXPR
  246. inline bool
  247. __can_advance(_InputIterator, const std::pair<_Diff, _Distance_precision>&, int)
  248. { return true; }
  249. template<typename _Iterator, typename _Sequence, typename _Category,
  250. typename _Diff>
  251. bool
  252. __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  253. const std::pair<_Diff, _Distance_precision>&, int);
  254. /** Helper function to extract base iterator of random access safe iterator
  255. * in order to reduce performance impact of debug mode. Limited to random
  256. * access iterator because it is the only category for which it is possible
  257. * to check for correct iterators order in the __valid_range function
  258. * thanks to the < operator.
  259. */
  260. template<typename _Iterator>
  261. _GLIBCXX_CONSTEXPR
  262. inline _Iterator
  263. __base(_Iterator __it)
  264. { return __it; }
  265. #if __cplusplus < 201103L
  266. template<typename _Iterator>
  267. struct _Unsafe_type
  268. { typedef _Iterator _Type; };
  269. #endif
  270. /* Remove debug mode safe iterator layer, if any. */
  271. template<typename _Iterator>
  272. inline _Iterator
  273. __unsafe(_Iterator __it)
  274. { return __it; }
  275. }
  276. #endif