選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

316 行
9.8KB

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