pointer_traits.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_POINTER_TRAITS_H
  10. #define _LIBCPP___MEMORY_POINTER_TRAITS_H
  11. #include <__config>
  12. #include <__memory/addressof.h>
  13. #include <__type_traits/conditional.h>
  14. #include <__type_traits/conjunction.h>
  15. #include <__type_traits/decay.h>
  16. #include <__type_traits/is_class.h>
  17. #include <__type_traits/is_function.h>
  18. #include <__type_traits/is_void.h>
  19. #include <__type_traits/void_t.h>
  20. #include <__utility/declval.h>
  21. #include <cstddef>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. template <class _Tp, class = void>
  27. struct __has_element_type : false_type {};
  28. template <class _Tp>
  29. struct __has_element_type<_Tp, __void_t<typename _Tp::element_type> > : true_type {};
  30. template <class _Ptr, bool = __has_element_type<_Ptr>::value>
  31. struct __pointer_traits_element_type;
  32. template <class _Ptr>
  33. struct __pointer_traits_element_type<_Ptr, true>
  34. {
  35. typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;
  36. };
  37. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  38. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
  39. {
  40. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;
  41. };
  42. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  43. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
  44. {
  45. typedef _LIBCPP_NODEBUG _Tp type;
  46. };
  47. template <class _Tp, class = void>
  48. struct __has_difference_type : false_type {};
  49. template <class _Tp>
  50. struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {};
  51. template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
  52. struct __pointer_traits_difference_type
  53. {
  54. typedef _LIBCPP_NODEBUG ptrdiff_t type;
  55. };
  56. template <class _Ptr>
  57. struct __pointer_traits_difference_type<_Ptr, true>
  58. {
  59. typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;
  60. };
  61. template <class _Tp, class _Up>
  62. struct __has_rebind
  63. {
  64. private:
  65. template <class _Xp> static false_type __test(...);
  66. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  67. template <class _Xp> static true_type __test(typename _Xp::template rebind<_Up>* = 0);
  68. _LIBCPP_SUPPRESS_DEPRECATED_POP
  69. public:
  70. static const bool value = decltype(__test<_Tp>(0))::value;
  71. };
  72. template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
  73. struct __pointer_traits_rebind
  74. {
  75. #ifndef _LIBCPP_CXX03_LANG
  76. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;
  77. #else
  78. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;
  79. #endif
  80. };
  81. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  82. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
  83. {
  84. #ifndef _LIBCPP_CXX03_LANG
  85. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
  86. #else
  87. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
  88. #endif
  89. };
  90. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  91. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
  92. {
  93. typedef _Sp<_Up, _Args...> type;
  94. };
  95. template <class _Ptr>
  96. struct _LIBCPP_TEMPLATE_VIS pointer_traits
  97. {
  98. typedef _Ptr pointer;
  99. typedef typename __pointer_traits_element_type<pointer>::type element_type;
  100. typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
  101. #ifndef _LIBCPP_CXX03_LANG
  102. template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
  103. #else
  104. template <class _Up> struct rebind
  105. {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
  106. #endif // _LIBCPP_CXX03_LANG
  107. private:
  108. struct __nat {};
  109. public:
  110. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  111. static pointer pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r)
  112. {return pointer::pointer_to(__r);}
  113. };
  114. template <class _Tp>
  115. struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
  116. {
  117. typedef _Tp* pointer;
  118. typedef _Tp element_type;
  119. typedef ptrdiff_t difference_type;
  120. #ifndef _LIBCPP_CXX03_LANG
  121. template <class _Up> using rebind = _Up*;
  122. #else
  123. template <class _Up> struct rebind {typedef _Up* other;};
  124. #endif
  125. private:
  126. struct __nat {};
  127. public:
  128. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  129. static pointer pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT
  130. {return _VSTD::addressof(__r);}
  131. };
  132. #ifndef _LIBCPP_CXX03_LANG
  133. template <class _From, class _To>
  134. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;
  135. #else
  136. template <class _From, class _To>
  137. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
  138. #endif
  139. // to_address
  140. template <class _Pointer, class = void>
  141. struct __to_address_helper;
  142. template <class _Tp>
  143. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  144. _Tp* __to_address(_Tp* __p) _NOEXCEPT {
  145. static_assert(!is_function<_Tp>::value, "_Tp is a function type");
  146. return __p;
  147. }
  148. template <class _Pointer, class = void>
  149. struct _HasToAddress : false_type {};
  150. template <class _Pointer>
  151. struct _HasToAddress<_Pointer,
  152. decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))
  153. > : true_type {};
  154. template <class _Pointer, class = void>
  155. struct _HasArrow : false_type {};
  156. template <class _Pointer>
  157. struct _HasArrow<_Pointer,
  158. decltype((void)std::declval<const _Pointer&>().operator->())
  159. > : true_type {};
  160. template <class _Pointer>
  161. struct _IsFancyPointer {
  162. static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value;
  163. };
  164. // enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
  165. template <class _Pointer, class = __enable_if_t<
  166. _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value
  167. > >
  168. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  169. typename decay<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>::type
  170. __to_address(const _Pointer& __p) _NOEXCEPT {
  171. return __to_address_helper<_Pointer>::__call(__p);
  172. }
  173. template <class _Pointer, class>
  174. struct __to_address_helper {
  175. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  176. static decltype(_VSTD::__to_address(std::declval<const _Pointer&>().operator->()))
  177. __call(const _Pointer& __p) _NOEXCEPT {
  178. return _VSTD::__to_address(__p.operator->());
  179. }
  180. };
  181. template <class _Pointer>
  182. struct __to_address_helper<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {
  183. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  184. static decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))
  185. __call(const _Pointer& __p) _NOEXCEPT {
  186. return pointer_traits<_Pointer>::to_address(__p);
  187. }
  188. };
  189. #if _LIBCPP_STD_VER > 17
  190. template <class _Tp>
  191. inline _LIBCPP_INLINE_VISIBILITY constexpr
  192. auto to_address(_Tp *__p) noexcept {
  193. return _VSTD::__to_address(__p);
  194. }
  195. template <class _Pointer>
  196. inline _LIBCPP_INLINE_VISIBILITY constexpr
  197. auto to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {
  198. return _VSTD::__to_address(__p);
  199. }
  200. #endif
  201. _LIBCPP_END_NAMESPACE_STD
  202. #endif // _LIBCPP___MEMORY_POINTER_TRAITS_H