pointer_traits.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;
  35. };
  36. template <template <class, class...> class _Sp, class _Tp, class... _Args>
  37. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> {
  38. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;
  39. };
  40. template <template <class, class...> class _Sp, class _Tp, class... _Args>
  41. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false> {
  42. typedef _LIBCPP_NODEBUG _Tp type;
  43. };
  44. template <class _Tp, class = void>
  45. struct __has_difference_type : false_type {};
  46. template <class _Tp>
  47. struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {};
  48. template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
  49. struct __pointer_traits_difference_type {
  50. typedef _LIBCPP_NODEBUG ptrdiff_t type;
  51. };
  52. template <class _Ptr>
  53. struct __pointer_traits_difference_type<_Ptr, true> {
  54. typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;
  55. };
  56. template <class _Tp, class _Up>
  57. struct __has_rebind {
  58. private:
  59. template <class _Xp>
  60. static false_type __test(...);
  61. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  62. template <class _Xp>
  63. static true_type __test(typename _Xp::template rebind<_Up>* = 0);
  64. _LIBCPP_SUPPRESS_DEPRECATED_POP
  65. public:
  66. static const bool value = decltype(__test<_Tp>(0))::value;
  67. };
  68. template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
  69. struct __pointer_traits_rebind {
  70. #ifndef _LIBCPP_CXX03_LANG
  71. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;
  72. #else
  73. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;
  74. #endif
  75. };
  76. template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
  77. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> {
  78. #ifndef _LIBCPP_CXX03_LANG
  79. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
  80. #else
  81. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
  82. #endif
  83. };
  84. template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
  85. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> {
  86. typedef _Sp<_Up, _Args...> type;
  87. };
  88. template <class _Ptr, class = void>
  89. struct __pointer_traits_impl {};
  90. template <class _Ptr>
  91. struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {
  92. typedef _Ptr pointer;
  93. typedef typename __pointer_traits_element_type<pointer>::type element_type;
  94. typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
  95. #ifndef _LIBCPP_CXX03_LANG
  96. template <class _Up>
  97. using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
  98. #else
  99. template <class _Up>
  100. struct rebind {
  101. typedef typename __pointer_traits_rebind<pointer, _Up>::type other;
  102. };
  103. #endif // _LIBCPP_CXX03_LANG
  104. private:
  105. struct __nat {};
  106. public:
  107. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
  108. pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) {
  109. return pointer::pointer_to(__r);
  110. }
  111. };
  112. template <class _Ptr>
  113. struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};
  114. template <class _Tp>
  115. struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*> {
  116. typedef _Tp* pointer;
  117. typedef _Tp element_type;
  118. typedef ptrdiff_t difference_type;
  119. #ifndef _LIBCPP_CXX03_LANG
  120. template <class _Up>
  121. using rebind = _Up*;
  122. #else
  123. template <class _Up>
  124. struct rebind {
  125. typedef _Up* other;
  126. };
  127. #endif
  128. private:
  129. struct __nat {};
  130. public:
  131. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
  132. pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT {
  133. return std::addressof(__r);
  134. }
  135. };
  136. #ifndef _LIBCPP_CXX03_LANG
  137. template <class _From, class _To>
  138. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;
  139. #else
  140. template <class _From, class _To>
  141. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
  142. #endif
  143. // to_address
  144. template <class _Pointer, class = void>
  145. struct __to_address_helper;
  146. template <class _Tp>
  147. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __to_address(_Tp* __p) _NOEXCEPT {
  148. static_assert(!is_function<_Tp>::value, "_Tp is a function type");
  149. return __p;
  150. }
  151. template <class _Pointer, class = void>
  152. struct _HasToAddress : false_type {};
  153. template <class _Pointer>
  154. struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) >
  155. : true_type {};
  156. template <class _Pointer, class = void>
  157. struct _HasArrow : false_type {};
  158. template <class _Pointer>
  159. struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : 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, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0>
  166. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  167. __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
  168. __to_address(const _Pointer& __p) _NOEXCEPT {
  169. return __to_address_helper<_Pointer>::__call(__p);
  170. }
  171. template <class _Pointer, class>
  172. struct __to_address_helper {
  173. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static decltype(std::__to_address(
  174. std::declval<const _Pointer&>().operator->()))
  175. __call(const _Pointer& __p) _NOEXCEPT {
  176. return std::__to_address(__p.operator->());
  177. }
  178. };
  179. template <class _Pointer>
  180. struct __to_address_helper<_Pointer,
  181. decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {
  182. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static decltype(pointer_traits<_Pointer>::to_address(
  183. std::declval<const _Pointer&>()))
  184. __call(const _Pointer& __p) _NOEXCEPT {
  185. return pointer_traits<_Pointer>::to_address(__p);
  186. }
  187. };
  188. #if _LIBCPP_STD_VER >= 20
  189. template <class _Tp>
  190. inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {
  191. return std::__to_address(__p);
  192. }
  193. template <class _Pointer>
  194. inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(const _Pointer& __p) noexcept
  195. -> decltype(std::__to_address(__p)) {
  196. return std::__to_address(__p);
  197. }
  198. #endif
  199. _LIBCPP_END_NAMESPACE_STD
  200. #endif // _LIBCPP___MEMORY_POINTER_TRAITS_H