adapt_to_vtable.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #if !defined(BOOST_PP_IS_ITERATING)
  11. #ifndef BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED
  12. #define BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/utility/addressof.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/mpl/eval_if.hpp>
  17. #include <boost/mpl/has_xxx.hpp>
  18. #include <boost/type_traits/function_traits.hpp>
  19. #include <boost/type_traits/remove_cv.hpp>
  20. #include <boost/preprocessor/cat.hpp>
  21. #include <boost/preprocessor/iteration/iterate.hpp>
  22. #include <boost/preprocessor/repetition/enum.hpp>
  23. #include <boost/preprocessor/repetition/enum_params.hpp>
  24. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  25. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  26. #include <boost/type_erasure/detail/get_signature.hpp>
  27. #include <boost/type_erasure/detail/storage.hpp>
  28. #include <boost/type_erasure/is_placeholder.hpp>
  29. #include <boost/type_erasure/config.hpp>
  30. namespace boost {
  31. namespace type_erasure {
  32. namespace detail {
  33. template<class T, class Out>
  34. struct get_placeholders;
  35. template<class PrimitiveConcept, class Sig>
  36. struct vtable_adapter;
  37. template<class PrimitiveConcept, class Sig, class Out>
  38. struct get_placeholders<vtable_adapter<PrimitiveConcept, Sig>, Out>
  39. {
  40. typedef typename get_placeholders<PrimitiveConcept, Out>::type type;
  41. };
  42. template<class T>
  43. struct replace_param_for_vtable
  44. {
  45. typedef typename ::boost::mpl::if_<
  46. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  47. const ::boost::type_erasure::detail::storage&,
  48. T
  49. >::type type;
  50. };
  51. template<class T>
  52. struct replace_param_for_vtable<T&>
  53. {
  54. typedef typename ::boost::mpl::if_<
  55. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  56. ::boost::type_erasure::detail::storage&,
  57. T&
  58. >::type type;
  59. };
  60. template<class T>
  61. struct replace_param_for_vtable<const T&>
  62. {
  63. typedef typename ::boost::mpl::if_<
  64. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  65. const ::boost::type_erasure::detail::storage&,
  66. const T&
  67. >::type type;
  68. };
  69. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  70. template<class T>
  71. struct replace_param_for_vtable<T&&>
  72. {
  73. typedef typename ::boost::mpl::if_<
  74. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  75. ::boost::type_erasure::detail::storage&&,
  76. T&&
  77. >::type type;
  78. };
  79. #endif
  80. template<class T>
  81. struct replace_result_for_vtable
  82. {
  83. typedef typename ::boost::mpl::if_<
  84. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  85. ::boost::type_erasure::detail::storage,
  86. T
  87. >::type type;
  88. };
  89. template<class T>
  90. struct replace_result_for_vtable<T&>
  91. {
  92. typedef typename ::boost::mpl::if_<
  93. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  94. ::boost::type_erasure::detail::storage&,
  95. T&
  96. >::type type;
  97. };
  98. template<class T>
  99. struct replace_result_for_vtable<const T&>
  100. {
  101. typedef typename ::boost::mpl::if_<
  102. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  103. ::boost::type_erasure::detail::storage&,
  104. const T&
  105. >::type type;
  106. };
  107. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  108. template<class T>
  109. struct replace_result_for_vtable<T&&>
  110. {
  111. typedef typename ::boost::mpl::if_<
  112. ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
  113. ::boost::type_erasure::detail::storage&&,
  114. T&&
  115. >::type type;
  116. };
  117. #endif
  118. template<class Sig>
  119. struct get_vtable_signature;
  120. BOOST_MPL_HAS_XXX_TRAIT_DEF(type)
  121. template<class T>
  122. struct is_internal_concept :
  123. ::boost::type_erasure::detail::has_type<T>
  124. {};
  125. template<class PrimitiveConcept>
  126. struct adapt_to_vtable
  127. {
  128. typedef ::boost::type_erasure::detail::vtable_adapter<
  129. PrimitiveConcept,
  130. typename ::boost::type_erasure::detail::get_vtable_signature<
  131. typename ::boost::type_erasure::detail::get_signature<
  132. PrimitiveConcept
  133. >::type
  134. >::type
  135. > type;
  136. };
  137. template<class Concept>
  138. struct maybe_adapt_to_vtable
  139. {
  140. typedef typename ::boost::mpl::eval_if<
  141. ::boost::type_erasure::detail::is_internal_concept<Concept>,
  142. ::boost::mpl::identity<Concept>,
  143. ::boost::type_erasure::detail::adapt_to_vtable<Concept>
  144. >::type type;
  145. };
  146. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  147. !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  148. !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
  149. template<class PrimitiveConcept, class Sig, class ConceptSig>
  150. struct vtable_adapter_impl;
  151. template<class PrimitiveConcept, class R, class... T, class R2, class... U>
  152. struct vtable_adapter_impl<PrimitiveConcept, R(T...), R2(U...)>
  153. {
  154. typedef R (*type)(T...);
  155. static R value(T... arg)
  156. {
  157. return PrimitiveConcept::apply(
  158. ::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...);
  159. }
  160. };
  161. template<class PrimitiveConcept, class... T, class R2, class... U>
  162. struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage(T...), R2(U...)>
  163. {
  164. typedef ::boost::type_erasure::detail::storage (*type)(T...);
  165. static ::boost::type_erasure::detail::storage value(T... arg)
  166. {
  167. return ::boost::type_erasure::detail::storage(
  168. PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...));
  169. }
  170. };
  171. template<class PrimitiveConcept, class... T, class R2, class... U>
  172. struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage&(T...), R2(U...)>
  173. {
  174. typedef ::boost::type_erasure::detail::storage (*type)(T...);
  175. static ::boost::type_erasure::detail::storage value(T... arg)
  176. {
  177. ::boost::type_erasure::detail::storage result;
  178. typename ::boost::remove_reference<R2>::type* p =
  179. ::boost::addressof(
  180. PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...));
  181. result.data = const_cast<void*>(static_cast<const void*>(p));
  182. return result;
  183. }
  184. };
  185. template<class PrimitiveConcept, class... T, class R2, class... U>
  186. struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage&&(T...), R2(U...)>
  187. {
  188. typedef ::boost::type_erasure::detail::storage (*type)(T...);
  189. static ::boost::type_erasure::detail::storage value(T... arg)
  190. {
  191. ::boost::type_erasure::detail::storage result;
  192. R2 tmp = PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...);
  193. typename ::boost::remove_reference<R2>::type* p = ::boost::addressof(tmp);
  194. result.data = const_cast<void*>(static_cast<const void*>(p));
  195. return result;
  196. }
  197. };
  198. template<class PrimitiveConcept, class Sig>
  199. struct vtable_adapter
  200. : vtable_adapter_impl<
  201. PrimitiveConcept,
  202. Sig,
  203. typename ::boost::type_erasure::detail::get_signature<
  204. PrimitiveConcept
  205. >::type
  206. >
  207. {};
  208. template<class R, class... T>
  209. struct get_vtable_signature<R(T...)>
  210. {
  211. typedef typename ::boost::type_erasure::detail::replace_result_for_vtable<
  212. R
  213. >::type type(typename ::boost::type_erasure::detail::replace_param_for_vtable<T>::type...);
  214. };
  215. #else
  216. #define BOOST_PP_FILENAME_1 <boost/type_erasure/detail/adapt_to_vtable.hpp>
  217. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY)
  218. #include BOOST_PP_ITERATE()
  219. #endif
  220. }
  221. }
  222. }
  223. #endif
  224. #else
  225. #define N BOOST_PP_ITERATION()
  226. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  227. #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \
  228. ::boost::type_erasure::detail::extract< \
  229. typename traits:: \
  230. BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \
  231. >(std::forward<BOOST_PP_CAT(T, n)>(BOOST_PP_CAT(arg, n)))
  232. #else
  233. #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \
  234. ::boost::type_erasure::detail::extract< \
  235. typename traits:: \
  236. BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \
  237. >(BOOST_PP_CAT(arg, n))
  238. #endif
  239. #define BOOST_TYPE_ERASURE_REPLACE_PARAM(z, n, data) \
  240. typename ::boost::type_erasure::detail::replace_param_for_vtable< \
  241. BOOST_PP_CAT(T, n)>::type
  242. template<class PrimitiveConcept, class R
  243. BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  244. struct vtable_adapter<PrimitiveConcept, R(BOOST_PP_ENUM_PARAMS(N, T))>
  245. {
  246. typedef R (*type)(BOOST_PP_ENUM_PARAMS(N, T));
  247. static R value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
  248. {
  249. #if N > 0
  250. typedef typename ::boost::function_traits<
  251. typename ::boost::type_erasure::detail::get_signature<
  252. PrimitiveConcept
  253. >::type
  254. > traits;
  255. #endif
  256. return PrimitiveConcept::apply(
  257. BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~));
  258. }
  259. };
  260. template<class PrimitiveConcept
  261. BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  262. struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage(BOOST_PP_ENUM_PARAMS(N, T))>
  263. {
  264. typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
  265. static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
  266. {
  267. #if N > 0
  268. typedef typename ::boost::function_traits<
  269. typename ::boost::type_erasure::detail::get_signature<
  270. PrimitiveConcept
  271. >::type
  272. > traits;
  273. #endif
  274. return ::boost::type_erasure::detail::storage(
  275. PrimitiveConcept::apply(
  276. BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)));
  277. }
  278. };
  279. template<class PrimitiveConcept
  280. BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  281. struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage&(BOOST_PP_ENUM_PARAMS(N, T))>
  282. {
  283. typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
  284. static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
  285. {
  286. typedef typename ::boost::function_traits<
  287. typename ::boost::type_erasure::detail::get_signature<
  288. PrimitiveConcept
  289. >::type
  290. > traits;
  291. ::boost::type_erasure::detail::storage result;
  292. typename ::boost::remove_reference<typename traits::result_type>::type* p =
  293. ::boost::addressof(
  294. PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)));
  295. result.data = const_cast<void*>(static_cast<const void*>(p));
  296. return result;
  297. }
  298. };
  299. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  300. template<class PrimitiveConcept
  301. BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  302. struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage&&(BOOST_PP_ENUM_PARAMS(N, T))>
  303. {
  304. typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
  305. static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
  306. {
  307. typedef typename ::boost::function_traits<
  308. typename ::boost::type_erasure::detail::get_signature<
  309. PrimitiveConcept
  310. >::type
  311. > traits;
  312. ::boost::type_erasure::detail::storage result;
  313. typename traits::result_type tmp =
  314. PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~));
  315. typename ::boost::remove_reference<typename traits::result_type>::type* p =
  316. ::boost::addressof(tmp);
  317. result.data = const_cast<void*>(static_cast<const void*>(p));
  318. return result;
  319. }
  320. };
  321. #endif
  322. template<class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  323. struct get_vtable_signature<R(BOOST_PP_ENUM_PARAMS(N, T))>
  324. {
  325. typedef typename ::boost::type_erasure::detail::replace_result_for_vtable<
  326. R
  327. >::type type(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_REPLACE_PARAM, ~));
  328. };
  329. #undef BOOST_TYPE_ERASURE_REPLACE_PARAM
  330. #undef BOOST_TYPE_ERASURE_EXTRACT
  331. #undef N
  332. #endif