pointer_traits.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2017-2018 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. #if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
  11. #include <memory>
  12. #else
  13. #include <boost/core/addressof.hpp>
  14. #endif
  15. namespace boost {
  16. #if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
  17. template<class T>
  18. struct pointer_traits
  19. : std::pointer_traits<T> {
  20. template<class U>
  21. struct rebind_to {
  22. typedef typename std::pointer_traits<T>::template rebind<U> type;
  23. };
  24. };
  25. template<class T>
  26. struct pointer_traits<T*>
  27. : std::pointer_traits<T*> {
  28. template<class U>
  29. struct rebind_to {
  30. typedef U* type;
  31. };
  32. };
  33. #else
  34. namespace detail {
  35. template<class>
  36. struct ptr_void {
  37. typedef void type;
  38. };
  39. template<class T>
  40. struct ptr_first;
  41. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  42. template<template<class, class...> class T, class U, class... Args>
  43. struct ptr_first<T<U, Args...> > {
  44. typedef U type;
  45. };
  46. #else
  47. template<template<class> class T, class U>
  48. struct ptr_first<T<U> > {
  49. typedef U type;
  50. };
  51. template<template<class, class> class T, class U1, class U2>
  52. struct ptr_first<T<U1, U2> > {
  53. typedef U1 type;
  54. };
  55. template<template<class, class, class> class T, class U1, class U2, class U3>
  56. struct ptr_first<T<U1, U2, U3> > {
  57. typedef U1 type;
  58. };
  59. #endif
  60. template<class T, class = void>
  61. struct ptr_element {
  62. typedef typename ptr_first<T>::type type;
  63. };
  64. template<class T>
  65. struct ptr_element<T, typename ptr_void<typename T::element_type>::type> {
  66. typedef typename T::element_type type;
  67. };
  68. template<class, class = void>
  69. struct ptr_difference {
  70. typedef std::ptrdiff_t type;
  71. };
  72. template<class T>
  73. struct ptr_difference<T,
  74. typename ptr_void<typename T::difference_type>::type> {
  75. typedef typename T::difference_type type;
  76. };
  77. template<class T, class V>
  78. struct ptr_transform;
  79. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  80. template<template<class, class...> class T, class U, class... Args, class V>
  81. struct ptr_transform<T<U, Args...>, V> {
  82. typedef T<V, Args...> type;
  83. };
  84. #else
  85. template<template<class> class T, class U, class V>
  86. struct ptr_transform<T<U>, V> {
  87. typedef T<V> type;
  88. };
  89. template<template<class, class> class T, class U1, class U2, class V>
  90. struct ptr_transform<T<U1, U2>, V> {
  91. typedef T<V, U2> type;
  92. };
  93. template<template<class, class, class> class T,
  94. class U1, class U2, class U3, class V>
  95. struct ptr_transform<T<U1, U2, U3>, V> {
  96. typedef T<V, U2, U3> type;
  97. };
  98. #endif
  99. template<class T, class U, class = void>
  100. struct ptr_rebind {
  101. typedef typename ptr_transform<T, U>::type type;
  102. };
  103. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  104. template<class T, class U>
  105. struct ptr_rebind<T, U,
  106. typename ptr_void<typename T::template rebind<U> >::type> {
  107. typedef typename T::template rebind<U> type;
  108. };
  109. #endif
  110. template<class T>
  111. struct ptr_value {
  112. typedef T type;
  113. };
  114. template<>
  115. struct ptr_value<void> {
  116. typedef struct { } type;
  117. };
  118. } /* detail */
  119. template<class T>
  120. struct pointer_traits {
  121. typedef T pointer;
  122. typedef typename detail::ptr_element<T>::type element_type;
  123. typedef typename detail::ptr_difference<T>::type difference_type;
  124. template<class U>
  125. struct rebind_to {
  126. typedef typename detail::ptr_rebind<T, U>::type type;
  127. };
  128. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  129. template<class U>
  130. using rebind = typename detail::ptr_rebind<T, U>::type;
  131. #endif
  132. static pointer
  133. pointer_to(typename detail::ptr_value<element_type>::type& v) {
  134. return pointer::pointer_to(v);
  135. }
  136. };
  137. template<class T>
  138. struct pointer_traits<T*> {
  139. typedef T* pointer;
  140. typedef T element_type;
  141. typedef std::ptrdiff_t difference_type;
  142. template<class U>
  143. struct rebind_to {
  144. typedef U* type;
  145. };
  146. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  147. template<class U>
  148. using rebind = U*;
  149. #endif
  150. static T*
  151. pointer_to(typename detail::ptr_value<T>::type& v) BOOST_NOEXCEPT {
  152. return boost::addressof(v);
  153. }
  154. };
  155. #endif
  156. template<class T>
  157. BOOST_CONSTEXPR inline T*
  158. to_address(T* v) BOOST_NOEXCEPT
  159. {
  160. return v;
  161. }
  162. #if !defined(BOOST_NO_CXX14_RETURN_TYPE_DEDUCTION)
  163. namespace detail {
  164. template<class T>
  165. inline T*
  166. ptr_address(T* v, int) BOOST_NOEXCEPT
  167. {
  168. return v;
  169. }
  170. template<class T>
  171. inline auto
  172. ptr_address(const T& v, int) BOOST_NOEXCEPT
  173. -> decltype(boost::pointer_traits<T>::to_address(v))
  174. {
  175. return boost::pointer_traits<T>::to_address(v);
  176. }
  177. template<class T>
  178. inline auto
  179. ptr_address(const T& v, long) BOOST_NOEXCEPT
  180. {
  181. return boost::detail::ptr_address(v.operator->(), 0);
  182. }
  183. } /* detail */
  184. template<class T>
  185. inline auto
  186. to_address(const T& v) BOOST_NOEXCEPT
  187. {
  188. return boost::detail::ptr_address(v, 0);
  189. }
  190. #else
  191. template<class T>
  192. inline typename pointer_traits<T>::element_type*
  193. to_address(const T& v) BOOST_NOEXCEPT
  194. {
  195. return boost::to_address(v.operator->());
  196. }
  197. #endif
  198. } /* boost */
  199. #endif