pointer_traits.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright 2017-2021 Glen Joseph Fernandes
  3. ([email protected])
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_CORE_POINTER_TRAITS_HPP
  8. #define BOOST_CORE_POINTER_TRAITS_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/core/addressof.hpp>
  11. #include <cstddef>
  12. namespace boost {
  13. namespace detail {
  14. struct ptr_none { };
  15. template<class>
  16. struct ptr_valid {
  17. typedef void type;
  18. };
  19. template<class>
  20. struct ptr_first {
  21. typedef ptr_none type;
  22. };
  23. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  24. template<template<class, class...> class T, class U, class... Args>
  25. struct ptr_first<T<U, Args...> > {
  26. typedef U type;
  27. };
  28. #else
  29. template<template<class> class T, class U>
  30. struct ptr_first<T<U> > {
  31. typedef U type;
  32. };
  33. template<template<class, class> class T, class U1, class U2>
  34. struct ptr_first<T<U1, U2> > {
  35. typedef U1 type;
  36. };
  37. template<template<class, class, class> class T, class U1, class U2, class U3>
  38. struct ptr_first<T<U1, U2, U3> > {
  39. typedef U1 type;
  40. };
  41. #endif
  42. template<class T, class = void>
  43. struct ptr_element {
  44. typedef typename ptr_first<T>::type type;
  45. };
  46. template<class T>
  47. struct ptr_element<T, typename ptr_valid<typename T::element_type>::type> {
  48. typedef typename T::element_type type;
  49. };
  50. template<class, class = void>
  51. struct ptr_difference {
  52. typedef std::ptrdiff_t type;
  53. };
  54. template<class T>
  55. struct ptr_difference<T,
  56. typename ptr_valid<typename T::difference_type>::type> {
  57. typedef typename T::difference_type type;
  58. };
  59. template<class, class>
  60. struct ptr_transform { };
  61. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  62. template<template<class, class...> class T, class U, class... Args, class V>
  63. struct ptr_transform<T<U, Args...>, V> {
  64. typedef T<V, Args...> type;
  65. };
  66. #else
  67. template<template<class> class T, class U, class V>
  68. struct ptr_transform<T<U>, V> {
  69. typedef T<V> type;
  70. };
  71. template<template<class, class> class T, class U1, class U2, class V>
  72. struct ptr_transform<T<U1, U2>, V> {
  73. typedef T<V, U2> type;
  74. };
  75. template<template<class, class, class> class T,
  76. class U1, class U2, class U3, class V>
  77. struct ptr_transform<T<U1, U2, U3>, V> {
  78. typedef T<V, U2, U3> type;
  79. };
  80. #endif
  81. template<class T, class U, class = void>
  82. struct ptr_rebind
  83. : ptr_transform<T, U> { };
  84. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  85. template<class T, class U>
  86. struct ptr_rebind<T, U,
  87. typename ptr_valid<typename T::template rebind<U> >::type> {
  88. typedef typename T::template rebind<U> type;
  89. };
  90. #else
  91. template<class T, class U>
  92. struct ptr_rebind<T, U,
  93. typename ptr_valid<typename T::template rebind<U>::other>::type> {
  94. typedef typename T::template rebind<U>::other type;
  95. };
  96. #endif
  97. #if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
  98. template<class T, class E>
  99. class ptr_to_expr {
  100. template<class>
  101. struct result {
  102. char x, y;
  103. };
  104. static E& source();
  105. template<class O>
  106. static auto check(int) -> result<decltype(O::pointer_to(source()))>;
  107. template<class>
  108. static char check(long);
  109. public:
  110. BOOST_STATIC_CONSTEXPR bool value = sizeof(check<T>(0)) > 1;
  111. };
  112. template<class T, class E>
  113. struct ptr_to_expr<T*, E> {
  114. BOOST_STATIC_CONSTEXPR bool value = true;
  115. };
  116. template<class T, class E>
  117. struct ptr_has_to {
  118. BOOST_STATIC_CONSTEXPR bool value = ptr_to_expr<T, E>::value;
  119. };
  120. #else
  121. template<class, class>
  122. struct ptr_has_to {
  123. BOOST_STATIC_CONSTEXPR bool value = true;
  124. };
  125. #endif
  126. template<class T>
  127. struct ptr_has_to<T, void> {
  128. BOOST_STATIC_CONSTEXPR bool value = false;
  129. };
  130. template<class T>
  131. struct ptr_has_to<T, const void> {
  132. BOOST_STATIC_CONSTEXPR bool value = false;
  133. };
  134. template<class T>
  135. struct ptr_has_to<T, volatile void> {
  136. BOOST_STATIC_CONSTEXPR bool value = false;
  137. };
  138. template<class T>
  139. struct ptr_has_to<T, const volatile void> {
  140. BOOST_STATIC_CONSTEXPR bool value = false;
  141. };
  142. template<class T, class E, bool = ptr_has_to<T, E>::value>
  143. struct ptr_to { };
  144. template<class T, class E>
  145. struct ptr_to<T, E, true> {
  146. static T pointer_to(E& v) {
  147. return T::pointer_to(v);
  148. }
  149. };
  150. template<class T>
  151. struct ptr_to<T*, T, true> {
  152. static T* pointer_to(T& v) BOOST_NOEXCEPT {
  153. return boost::addressof(v);
  154. }
  155. };
  156. template<class T, class E>
  157. struct ptr_traits
  158. : ptr_to<T, E> {
  159. typedef T pointer;
  160. typedef E element_type;
  161. typedef typename ptr_difference<T>::type difference_type;
  162. template<class U>
  163. struct rebind_to
  164. : ptr_rebind<T, U> { };
  165. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  166. template<class U>
  167. using rebind = typename rebind_to<U>::type;
  168. #endif
  169. };
  170. template<class T>
  171. struct ptr_traits<T, ptr_none> { };
  172. } /* detail */
  173. template<class T>
  174. struct pointer_traits
  175. : detail::ptr_traits<T, typename detail::ptr_element<T>::type> { };
  176. template<class T>
  177. struct pointer_traits<T*>
  178. : detail::ptr_to<T*, T> {
  179. typedef T* pointer;
  180. typedef T element_type;
  181. typedef std::ptrdiff_t difference_type;
  182. template<class U>
  183. struct rebind_to {
  184. typedef U* type;
  185. };
  186. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  187. template<class U>
  188. using rebind = typename rebind_to<U>::type;
  189. #endif
  190. };
  191. template<class T>
  192. BOOST_CONSTEXPR inline T*
  193. to_address(T* v) BOOST_NOEXCEPT
  194. {
  195. return v;
  196. }
  197. #if !defined(BOOST_NO_CXX14_RETURN_TYPE_DEDUCTION)
  198. namespace detail {
  199. template<class T>
  200. inline T*
  201. ptr_address(T* v, int) BOOST_NOEXCEPT
  202. {
  203. return v;
  204. }
  205. template<class T>
  206. inline auto
  207. ptr_address(const T& v, int) BOOST_NOEXCEPT
  208. -> decltype(boost::pointer_traits<T>::to_address(v))
  209. {
  210. return boost::pointer_traits<T>::to_address(v);
  211. }
  212. template<class T>
  213. inline auto
  214. ptr_address(const T& v, long) BOOST_NOEXCEPT
  215. {
  216. return boost::detail::ptr_address(v.operator->(), 0);
  217. }
  218. } /* detail */
  219. template<class T>
  220. inline auto
  221. to_address(const T& v) BOOST_NOEXCEPT
  222. {
  223. return boost::detail::ptr_address(v, 0);
  224. }
  225. #else
  226. template<class T>
  227. inline typename pointer_traits<T>::element_type*
  228. to_address(const T& v) BOOST_NOEXCEPT
  229. {
  230. return boost::to_address(v.operator->());
  231. }
  232. #endif
  233. } /* boost */
  234. #endif