pointer_traits.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, class = void>
  96. struct __pointer_traits_impl {};
  97. template <class _Ptr>
  98. struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {
  99. typedef _Ptr pointer;
  100. typedef typename __pointer_traits_element_type<pointer>::type element_type;
  101. typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
  102. #ifndef _LIBCPP_CXX03_LANG
  103. template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
  104. #else
  105. template <class _Up> struct rebind
  106. {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
  107. #endif // _LIBCPP_CXX03_LANG
  108. private:
  109. struct __nat {};
  110. public:
  111. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  112. static pointer pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r)
  113. {return pointer::pointer_to(__r);}
  114. };
  115. template <class _Ptr>
  116. struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};
  117. template <class _Tp>
  118. struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
  119. {
  120. typedef _Tp* pointer;
  121. typedef _Tp element_type;
  122. typedef ptrdiff_t difference_type;
  123. #ifndef _LIBCPP_CXX03_LANG
  124. template <class _Up> using rebind = _Up*;
  125. #else
  126. template <class _Up> struct rebind {typedef _Up* other;};
  127. #endif
  128. private:
  129. struct __nat {};
  130. public:
  131. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  132. static pointer pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT
  133. {return _VSTD::addressof(__r);}
  134. };
  135. #ifndef _LIBCPP_CXX03_LANG
  136. template <class _From, class _To>
  137. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;
  138. #else
  139. template <class _From, class _To>
  140. using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
  141. #endif
  142. // to_address
  143. template <class _Pointer, class = void>
  144. struct __to_address_helper;
  145. template <class _Tp>
  146. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  147. _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,
  155. decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))
  156. > : true_type {};
  157. template <class _Pointer, class = void>
  158. struct _HasArrow : false_type {};
  159. template <class _Pointer>
  160. struct _HasArrow<_Pointer,
  161. decltype((void)std::declval<const _Pointer&>().operator->())
  162. > : true_type {};
  163. template <class _Pointer>
  164. struct _IsFancyPointer {
  165. static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value;
  166. };
  167. // enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
  168. template <class _Pointer, class = __enable_if_t<
  169. _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value
  170. > >
  171. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  172. __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
  173. __to_address(const _Pointer& __p) _NOEXCEPT {
  174. return __to_address_helper<_Pointer>::__call(__p);
  175. }
  176. template <class _Pointer, class>
  177. struct __to_address_helper {
  178. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  179. static decltype(_VSTD::__to_address(std::declval<const _Pointer&>().operator->()))
  180. __call(const _Pointer& __p) _NOEXCEPT {
  181. return _VSTD::__to_address(__p.operator->());
  182. }
  183. };
  184. template <class _Pointer>
  185. struct __to_address_helper<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {
  186. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  187. static decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))
  188. __call(const _Pointer& __p) _NOEXCEPT {
  189. return pointer_traits<_Pointer>::to_address(__p);
  190. }
  191. };
  192. #if _LIBCPP_STD_VER >= 20
  193. template <class _Tp>
  194. inline _LIBCPP_INLINE_VISIBILITY constexpr
  195. auto to_address(_Tp *__p) noexcept {
  196. return _VSTD::__to_address(__p);
  197. }
  198. template <class _Pointer>
  199. inline _LIBCPP_INLINE_VISIBILITY constexpr
  200. auto to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {
  201. return _VSTD::__to_address(__p);
  202. }
  203. #endif
  204. _LIBCPP_END_NAMESPACE_STD
  205. #endif // _LIBCPP___MEMORY_POINTER_TRAITS_H