addressof.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Copyright (C) 2002 Brad King (brad.king@kitware.com)
  3. Douglas Gregor (gregod@cs.rpi.edu)
  4. Copyright (C) 2002, 2008, 2013 Peter Dimov
  5. Copyright (C) 2017 Glen Joseph Fernandes (glenjofe@gmail.com)
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef BHO_CORE_ADDRESSOF_HPP
  11. #define BHO_CORE_ADDRESSOF_HPP
  12. #include <asio2/bho/config.hpp>
  13. #if defined(BHO_MSVC_FULL_VER) && BHO_MSVC_FULL_VER >= 190024215
  14. #define BHO_CORE_HAS_BUILTIN_ADDRESSOF
  15. #elif defined(BHO_GCC) && BHO_GCC >= 70000
  16. #define BHO_CORE_HAS_BUILTIN_ADDRESSOF
  17. #elif defined(__has_builtin)
  18. #if __has_builtin(__builtin_addressof)
  19. #define BHO_CORE_HAS_BUILTIN_ADDRESSOF
  20. #endif
  21. #endif
  22. #if defined(BHO_CORE_HAS_BUILTIN_ADDRESSOF)
  23. #if defined(BHO_NO_CXX11_CONSTEXPR)
  24. #define BHO_CORE_NO_CONSTEXPR_ADDRESSOF
  25. #endif
  26. namespace bho {
  27. template<class T>
  28. BHO_CONSTEXPR inline T*
  29. addressof(T& o) BHO_NOEXCEPT
  30. {
  31. return __builtin_addressof(o);
  32. }
  33. } /* boost */
  34. #else
  35. #include <asio2/bho/config/workaround.hpp>
  36. #include <cstddef>
  37. namespace bho {
  38. namespace detail {
  39. template<class T>
  40. class addrof_ref {
  41. public:
  42. BHO_FORCEINLINE addrof_ref(T& o) BHO_NOEXCEPT
  43. : o_(o) { }
  44. BHO_FORCEINLINE operator T&() const BHO_NOEXCEPT {
  45. return o_;
  46. }
  47. private:
  48. addrof_ref& operator=(const addrof_ref&);
  49. T& o_;
  50. };
  51. template<class T>
  52. struct addrof {
  53. static BHO_FORCEINLINE T* get(T& o, long) BHO_NOEXCEPT {
  54. return reinterpret_cast<T*>(&
  55. const_cast<char&>(reinterpret_cast<const volatile char&>(o)));
  56. }
  57. static BHO_FORCEINLINE T* get(T* p, int) BHO_NOEXCEPT {
  58. return p;
  59. }
  60. };
  61. #if !defined(BHO_NO_CXX11_NULLPTR)
  62. #if !defined(BHO_NO_CXX11_DECLTYPE) && \
  63. (defined(__INTEL_COMPILER) || \
  64. (defined(__clang__) && !defined(_LIBCPP_VERSION)))
  65. typedef decltype(nullptr) addrof_null_t;
  66. #else
  67. typedef std::nullptr_t addrof_null_t;
  68. #endif
  69. template<>
  70. struct addrof<addrof_null_t> {
  71. typedef addrof_null_t type;
  72. static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT {
  73. return &o;
  74. }
  75. };
  76. template<>
  77. struct addrof<const addrof_null_t> {
  78. typedef const addrof_null_t type;
  79. static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT {
  80. return &o;
  81. }
  82. };
  83. template<>
  84. struct addrof<volatile addrof_null_t> {
  85. typedef volatile addrof_null_t type;
  86. static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT {
  87. return &o;
  88. }
  89. };
  90. template<>
  91. struct addrof<const volatile addrof_null_t> {
  92. typedef const volatile addrof_null_t type;
  93. static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT {
  94. return &o;
  95. }
  96. };
  97. #endif
  98. } /* detail */
  99. #if defined(BHO_NO_CXX11_SFINAE_EXPR) || \
  100. defined(BHO_NO_CXX11_CONSTEXPR) || \
  101. defined(BHO_NO_CXX11_DECLTYPE)
  102. #define BHO_CORE_NO_CONSTEXPR_ADDRESSOF
  103. template<class T>
  104. BHO_FORCEINLINE T*
  105. addressof(T& o) BHO_NOEXCEPT
  106. {
  107. #if BHO_WORKAROUND(BHO_BORLANDC, BHO_TESTED_AT(0x610)) || \
  108. BHO_WORKAROUND(__SUNPRO_CC, <= 0x5120)
  109. return bho::detail::addrof<T>::get(o, 0);
  110. #else
  111. return bho::detail::addrof<T>::get(bho::detail::addrof_ref<T>(o), 0);
  112. #endif
  113. }
  114. #if BHO_WORKAROUND(__SUNPRO_CC, BHO_TESTED_AT(0x590))
  115. namespace detail {
  116. template<class T>
  117. struct addrof_result {
  118. typedef T* type;
  119. };
  120. } /* detail */
  121. template<class T, std::size_t N>
  122. BHO_FORCEINLINE typename bho::detail::addrof_result<T[N]>::type
  123. addressof(T (&o)[N]) BHO_NOEXCEPT
  124. {
  125. return &o;
  126. }
  127. #endif
  128. #if BHO_WORKAROUND(BHO_BORLANDC, BHO_TESTED_AT(0x564))
  129. template<class T, std::size_t N>
  130. BHO_FORCEINLINE
  131. T (*addressof(T (&o)[N]) BHO_NOEXCEPT)[N]
  132. {
  133. return reinterpret_cast<T(*)[N]>(&o);
  134. }
  135. template<class T, std::size_t N>
  136. BHO_FORCEINLINE
  137. const T (*addressof(const T (&o)[N]) BHO_NOEXCEPT)[N]
  138. {
  139. return reinterpret_cast<const T(*)[N]>(&o);
  140. }
  141. #endif
  142. #else
  143. namespace detail {
  144. template<class T>
  145. T addrof_declval() BHO_NOEXCEPT;
  146. template<class>
  147. struct addrof_void {
  148. typedef void type;
  149. };
  150. template<class T, class E = void>
  151. struct addrof_member_operator {
  152. static constexpr bool value = false;
  153. };
  154. template<class T>
  155. struct addrof_member_operator<T, typename
  156. addrof_void<decltype(addrof_declval<T&>().operator&())>::type> {
  157. static constexpr bool value = true;
  158. };
  159. #if BHO_WORKAROUND(BHO_INTEL, < 1600)
  160. struct addrof_addressable { };
  161. addrof_addressable*
  162. operator&(addrof_addressable&) BHO_NOEXCEPT;
  163. #endif
  164. template<class T, class E = void>
  165. struct addrof_non_member_operator {
  166. static constexpr bool value = false;
  167. };
  168. template<class T>
  169. struct addrof_non_member_operator<T, typename
  170. addrof_void<decltype(operator&(addrof_declval<T&>()))>::type> {
  171. static constexpr bool value = true;
  172. };
  173. template<class T, class E = void>
  174. struct addrof_expression {
  175. static constexpr bool value = false;
  176. };
  177. template<class T>
  178. struct addrof_expression<T,
  179. typename addrof_void<decltype(&addrof_declval<T&>())>::type> {
  180. static constexpr bool value = true;
  181. };
  182. template<class T>
  183. struct addrof_is_constexpr {
  184. static constexpr bool value = addrof_expression<T>::value &&
  185. !addrof_member_operator<T>::value &&
  186. !addrof_non_member_operator<T>::value;
  187. };
  188. template<bool E, class T>
  189. struct addrof_if { };
  190. template<class T>
  191. struct addrof_if<true, T> {
  192. typedef T* type;
  193. };
  194. template<class T>
  195. BHO_FORCEINLINE
  196. typename addrof_if<!addrof_is_constexpr<T>::value, T>::type
  197. addressof(T& o) BHO_NOEXCEPT
  198. {
  199. return addrof<T>::get(addrof_ref<T>(o), 0);
  200. }
  201. template<class T>
  202. constexpr BHO_FORCEINLINE
  203. typename addrof_if<addrof_is_constexpr<T>::value, T>::type
  204. addressof(T& o) BHO_NOEXCEPT
  205. {
  206. return &o;
  207. }
  208. } /* detail */
  209. template<class T>
  210. constexpr BHO_FORCEINLINE T*
  211. addressof(T& o) BHO_NOEXCEPT
  212. {
  213. return bho::detail::addressof(o);
  214. }
  215. #endif
  216. } /* boost */
  217. #endif
  218. #if !defined(BHO_NO_CXX11_RVALUE_REFERENCES) && \
  219. !defined(BHO_NO_CXX11_DELETED_FUNCTIONS)
  220. namespace bho {
  221. template<class T>
  222. const T* addressof(const T&&) = delete;
  223. } /* boost */
  224. #endif
  225. #endif