pointer_traits.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. template <class _Tp, class = void>
  19. struct __has_element_type : false_type {};
  20. template <class _Tp>
  21. struct __has_element_type<_Tp,
  22. typename __void_t<typename _Tp::element_type>::type> : true_type {};
  23. template <class _Ptr, bool = __has_element_type<_Ptr>::value>
  24. struct __pointer_traits_element_type;
  25. template <class _Ptr>
  26. struct __pointer_traits_element_type<_Ptr, true>
  27. {
  28. typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;
  29. };
  30. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  31. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
  32. {
  33. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;
  34. };
  35. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  36. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
  37. {
  38. typedef _LIBCPP_NODEBUG _Tp type;
  39. };
  40. template <class _Tp, class = void>
  41. struct __has_difference_type : false_type {};
  42. template <class _Tp>
  43. struct __has_difference_type<_Tp,
  44. typename __void_t<typename _Tp::difference_type>::type> : true_type {};
  45. template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
  46. struct __pointer_traits_difference_type
  47. {
  48. typedef _LIBCPP_NODEBUG ptrdiff_t type;
  49. };
  50. template <class _Ptr>
  51. struct __pointer_traits_difference_type<_Ptr, true>
  52. {
  53. typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;
  54. };
  55. template <class _Tp, class _Up>
  56. struct __has_rebind
  57. {
  58. private:
  59. struct __two {char __lx; char __lxx;};
  60. template <class _Xp> static __two __test(...);
  61. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  62. // Fix for MSVC which allows to reference private types. Wrap into declval to prevent that.
  63. template <class _Xp> static char __test(decltype(_VSTD::declval<typename _Xp::template rebind<_Up>*>()) = 0);
  64. _LIBCPP_SUPPRESS_DEPRECATED_POP
  65. public:
  66. static const bool value = sizeof(__test<_Tp>(0)) == 1;
  67. };
  68. template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
  69. struct __pointer_traits_rebind
  70. {
  71. #ifndef _LIBCPP_CXX03_LANG
  72. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;
  73. #else
  74. typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;
  75. #endif
  76. };
  77. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  78. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
  79. {
  80. #ifndef _LIBCPP_CXX03_LANG
  81. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
  82. #else
  83. typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
  84. #endif
  85. };
  86. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  87. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
  88. {
  89. typedef _Sp<_Up, _Args...> type;
  90. };
  91. template <class _Ptr>
  92. struct _LIBCPP_TEMPLATE_VIS pointer_traits
  93. {
  94. typedef _Ptr pointer;
  95. typedef typename __pointer_traits_element_type<pointer>::type element_type;
  96. typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
  97. #ifndef _LIBCPP_CXX03_LANG
  98. template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
  99. #else
  100. template <class _Up> struct rebind
  101. {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
  102. #endif // _LIBCPP_CXX03_LANG
  103. private:
  104. struct __nat {};
  105. public:
  106. _LIBCPP_INLINE_VISIBILITY
  107. static pointer pointer_to(typename conditional<is_void<element_type>::value,
  108. __nat, element_type>::type& __r)
  109. {return pointer::pointer_to(__r);}
  110. };
  111. template <class _Tp>
  112. struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
  113. {
  114. typedef _Tp* pointer;
  115. typedef _Tp element_type;
  116. typedef ptrdiff_t difference_type;
  117. #ifndef _LIBCPP_CXX03_LANG
  118. template <class _Up> using rebind = _Up*;
  119. #else
  120. template <class _Up> struct rebind {typedef _Up* other;};
  121. #endif
  122. private:
  123. struct __nat {};
  124. public:
  125. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  126. static pointer pointer_to(typename conditional<is_void<element_type>::value,
  127. __nat, element_type>::type& __r) _NOEXCEPT
  128. {return _VSTD::addressof(__r);}
  129. };
  130. template <class _From, class _To>
  131. struct __rebind_pointer {
  132. #ifndef _LIBCPP_CXX03_LANG
  133. typedef typename pointer_traits<_From>::template rebind<_To> type;
  134. #else
  135. typedef typename pointer_traits<_From>::template rebind<_To>::other type;
  136. #endif
  137. };
  138. // to_address
  139. template <class _Pointer, class = void>
  140. struct __to_address_helper;
  141. template <class _Tp>
  142. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  143. _Tp* __to_address(_Tp* __p) _NOEXCEPT {
  144. static_assert(!is_function<_Tp>::value, "_Tp is a function type");
  145. return __p;
  146. }
  147. // enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
  148. template <class _Pointer, class = __enable_if_t<
  149. !is_pointer<_Pointer>::value && !is_array<_Pointer>::value && !is_function<_Pointer>::value
  150. > >
  151. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  152. typename decay<decltype(__to_address_helper<_Pointer>::__call(declval<const _Pointer&>()))>::type
  153. __to_address(const _Pointer& __p) _NOEXCEPT {
  154. return __to_address_helper<_Pointer>::__call(__p);
  155. }
  156. template <class _Pointer, class>
  157. struct __to_address_helper {
  158. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  159. static decltype(_VSTD::__to_address(declval<const _Pointer&>().operator->()))
  160. __call(const _Pointer& __p) _NOEXCEPT {
  161. return _VSTD::__to_address(__p.operator->());
  162. }
  163. };
  164. template <class _Pointer>
  165. struct __to_address_helper<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(declval<const _Pointer&>()))> {
  166. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  167. static decltype(pointer_traits<_Pointer>::to_address(declval<const _Pointer&>()))
  168. __call(const _Pointer& __p) _NOEXCEPT {
  169. return pointer_traits<_Pointer>::to_address(__p);
  170. }
  171. };
  172. #if _LIBCPP_STD_VER > 17
  173. template <class _Tp>
  174. inline _LIBCPP_INLINE_VISIBILITY constexpr
  175. auto to_address(_Tp *__p) noexcept {
  176. return _VSTD::__to_address(__p);
  177. }
  178. template <class _Pointer>
  179. inline _LIBCPP_INLINE_VISIBILITY constexpr
  180. auto to_address(const _Pointer& __p) noexcept {
  181. return _VSTD::__to_address(__p);
  182. }
  183. #endif
  184. _LIBCPP_END_NAMESPACE_STD
  185. #endif // _LIBCPP___MEMORY_POINTER_TRAITS_H