helper_functions.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2019 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/stl_iterator_base_types.h> // for iterator_traits,
  26. // categories and _Iter_base
  27. #include <bits/cpp_type_traits.h> // for __is_integer
  28. #include <bits/stl_pair.h> // for pair
  29. namespace __gnu_debug
  30. {
  31. template<typename _Iterator, typename _Sequence, typename _Category>
  32. class _Safe_iterator;
  33. #if __cplusplus >= 201103L
  34. template<typename _Iterator, typename _Sequence>
  35. class _Safe_local_iterator;
  36. #endif
  37. /** The precision to which we can calculate the distance between
  38. * two iterators.
  39. */
  40. enum _Distance_precision
  41. {
  42. __dp_none, // Not even an iterator type
  43. __dp_equality, //< Can compare iterator equality, only
  44. __dp_sign, //< Can determine equality and ordering
  45. __dp_exact //< Can determine distance precisely
  46. };
  47. template<typename _Iterator,
  48. typename = typename std::__is_integer<_Iterator>::__type>
  49. struct _Distance_traits
  50. {
  51. private:
  52. typedef
  53. typename std::iterator_traits<_Iterator>::difference_type _ItDiffType;
  54. template<typename _DiffType,
  55. typename = typename std::__is_void<_DiffType>::__type>
  56. struct _DiffTraits
  57. { typedef _DiffType __type; };
  58. template<typename _DiffType>
  59. struct _DiffTraits<_DiffType, std::__true_type>
  60. { typedef std::ptrdiff_t __type; };
  61. typedef typename _DiffTraits<_ItDiffType>::__type _DiffType;
  62. public:
  63. typedef std::pair<_DiffType, _Distance_precision> __type;
  64. };
  65. template<typename _Integral>
  66. struct _Distance_traits<_Integral, std::__true_type>
  67. { typedef std::pair<std::ptrdiff_t, _Distance_precision> __type; };
  68. /** Determine the distance between two iterators with some known
  69. * precision.
  70. */
  71. template<typename _Iterator>
  72. inline typename _Distance_traits<_Iterator>::__type
  73. __get_distance(_Iterator __lhs, _Iterator __rhs,
  74. std::random_access_iterator_tag)
  75. { return std::make_pair(__rhs - __lhs, __dp_exact); }
  76. template<typename _Iterator>
  77. inline typename _Distance_traits<_Iterator>::__type
  78. __get_distance(_Iterator __lhs, _Iterator __rhs,
  79. std::input_iterator_tag)
  80. {
  81. if (__lhs == __rhs)
  82. return std::make_pair(0, __dp_exact);
  83. return std::make_pair(1, __dp_equality);
  84. }
  85. template<typename _Iterator>
  86. inline typename _Distance_traits<_Iterator>::__type
  87. __get_distance(_Iterator __lhs, _Iterator __rhs)
  88. { return __get_distance(__lhs, __rhs, std::__iterator_category(__lhs)); }
  89. /** We say that integral types for a valid range, and defer to other
  90. * routines to realize what to do with integral types instead of
  91. * iterators.
  92. */
  93. template<typename _Integral>
  94. inline bool
  95. __valid_range_aux(_Integral, _Integral,
  96. typename _Distance_traits<_Integral>::__type& __dist,
  97. std::__true_type)
  98. {
  99. __dist = std::make_pair(0, __dp_none);
  100. return true;
  101. }
  102. /** We have iterators, so figure out what kind of iterators they are
  103. * to see if we can check the range ahead of time.
  104. */
  105. template<typename _InputIterator>
  106. inline bool
  107. __valid_range_aux(_InputIterator __first, _InputIterator __last,
  108. typename _Distance_traits<_InputIterator>::__type& __dist,
  109. std::__false_type)
  110. {
  111. __dist = __get_distance(__first, __last);
  112. switch (__dist.second)
  113. {
  114. case __dp_none:
  115. break;
  116. case __dp_equality:
  117. if (__dist.first == 0)
  118. return true;
  119. break;
  120. case __dp_sign:
  121. case __dp_exact:
  122. return __dist.first >= 0;
  123. }
  124. // Can't tell so assume it is fine.
  125. return true;
  126. }
  127. /** Don't know what these iterators are, or if they are even
  128. * iterators (we may get an integral type for InputIterator), so
  129. * see if they are integral and pass them on to the next phase
  130. * otherwise.
  131. */
  132. template<typename _InputIterator>
  133. inline bool
  134. __valid_range(_InputIterator __first, _InputIterator __last,
  135. typename _Distance_traits<_InputIterator>::__type& __dist)
  136. {
  137. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  138. return __valid_range_aux(__first, __last, __dist, _Integral());
  139. }
  140. template<typename _Iterator, typename _Sequence, typename _Category>
  141. bool
  142. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  143. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  144. typename _Distance_traits<_Iterator>::__type&);
  145. #if __cplusplus >= 201103L
  146. template<typename _Iterator,typename _Sequence>
  147. bool
  148. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  149. const _Safe_local_iterator<_Iterator, _Sequence>&,
  150. typename _Distance_traits<_Iterator>::__type&);
  151. #endif
  152. template<typename _InputIterator>
  153. inline bool
  154. __valid_range(_InputIterator __first, _InputIterator __last)
  155. {
  156. typename _Distance_traits<_InputIterator>::__type __dist;
  157. return __valid_range(__first, __last, __dist);
  158. }
  159. template<typename _Iterator, typename _Sequence, typename _Category>
  160. bool
  161. __valid_range(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  162. const _Safe_iterator<_Iterator, _Sequence, _Category>&);
  163. #if __cplusplus >= 201103L
  164. template<typename _Iterator, typename _Sequence>
  165. bool
  166. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>&,
  167. const _Safe_local_iterator<_Iterator, _Sequence>&);
  168. #endif
  169. // Fallback method, always ok.
  170. template<typename _InputIterator, typename _Size>
  171. inline bool
  172. __can_advance(_InputIterator, _Size)
  173. { return true; }
  174. template<typename _Iterator, typename _Sequence, typename _Category,
  175. typename _Size>
  176. bool
  177. __can_advance(const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  178. _Size);
  179. /** Helper function to extract base iterator of random access safe iterator
  180. * in order to reduce performance impact of debug mode. Limited to random
  181. * access iterator because it is the only category for which it is possible
  182. * to check for correct iterators order in the __valid_range function
  183. * thanks to the < operator.
  184. */
  185. template<typename _Iterator>
  186. inline _Iterator
  187. __base(_Iterator __it)
  188. { return __it; }
  189. #if __cplusplus < 201103L
  190. template<typename _Iterator>
  191. struct _Unsafe_type
  192. { typedef _Iterator _Type; };
  193. #endif
  194. /* Remove debug mode safe iterator layer, if any. */
  195. template<typename _Iterator>
  196. inline _Iterator
  197. __unsafe(_Iterator __it)
  198. { return __it; }
  199. }
  200. #endif