pointer_traits.hpp 5.0 KB

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