vtable.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_VTABLE_HPP_INCLUDED
  12. #define BOOST_TYPE_ERASURE_DETAIL_VTABLE_HPP_INCLUDED
  13. #include <boost/config.hpp>
  14. #include <boost/mpl/at.hpp>
  15. #include <boost/mpl/size.hpp>
  16. #include <boost/preprocessor/cat.hpp>
  17. #include <boost/preprocessor/expr_if.hpp>
  18. #include <boost/preprocessor/iteration/iterate.hpp>
  19. #include <boost/preprocessor/repetition/enum.hpp>
  20. #include <boost/preprocessor/repetition/enum_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_trailing.hpp>
  22. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  23. #include <boost/preprocessor/repetition/repeat.hpp>
  24. #include <boost/type_erasure/detail/rebind_placeholders.hpp>
  25. #include <boost/type_erasure/config.hpp>
  26. namespace boost {
  27. namespace type_erasure {
  28. namespace detail {
  29. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  30. template<class... T>
  31. struct stored_arg_pack;
  32. template<class It, class End, class... T>
  33. struct make_arg_pack_impl
  34. {
  35. typedef typename make_arg_pack_impl<
  36. typename ::boost::mpl::next<It>::type,
  37. End,
  38. T...,
  39. typename ::boost::mpl::deref<It>::type
  40. >::type type;
  41. };
  42. template<class End, class... T>
  43. struct make_arg_pack_impl<End, End, T...>
  44. {
  45. typedef stored_arg_pack<T...> type;
  46. };
  47. template<class Seq>
  48. struct make_arg_pack
  49. {
  50. typedef typename make_arg_pack_impl<
  51. typename ::boost::mpl::begin<Seq>::type,
  52. typename ::boost::mpl::end<Seq>::type
  53. >::type type;
  54. };
  55. template<class Args>
  56. struct make_vtable_impl;
  57. template<class Seq>
  58. struct make_vtable
  59. {
  60. typedef typename ::boost::type_erasure::detail::make_vtable_impl<
  61. typename ::boost::type_erasure::detail::make_arg_pack<Seq>::type
  62. >::type type;
  63. };
  64. template<class Table, class Args>
  65. struct make_vtable_init_impl;
  66. template<class Seq, class Table>
  67. struct make_vtable_init
  68. {
  69. typedef typename make_vtable_init_impl<
  70. Table,
  71. typename ::boost::type_erasure::detail::make_arg_pack<Seq>::type
  72. >::type type;
  73. };
  74. template<class T>
  75. struct vtable_entry
  76. {
  77. typename T::type value;
  78. vtable_entry() = default;
  79. constexpr vtable_entry(typename T::type arg) : value(arg) {}
  80. };
  81. template<class... T>
  82. struct compare_vtable;
  83. template<>
  84. struct compare_vtable<> {
  85. template<class S>
  86. static bool apply(const S& /*s1*/, const S& /*s2*/)
  87. {
  88. return true;
  89. }
  90. };
  91. template<class T0, class... T>
  92. struct compare_vtable<T0, T...> {
  93. template<class S>
  94. static bool apply(const S& s1, const S& s2)
  95. {
  96. return static_cast<const vtable_entry<T0>&>(s1).value ==
  97. static_cast<const vtable_entry<T0>&>(s2).value &&
  98. compare_vtable<T...>::apply(s1, s2);
  99. }
  100. };
  101. template<class... T>
  102. struct vtable_storage : vtable_entry<T>...
  103. {
  104. vtable_storage() = default;
  105. constexpr vtable_storage(typename T::type... arg)
  106. : vtable_entry<T>(arg)... {}
  107. template<class Bindings, class Src>
  108. void convert_from(const Src& src)
  109. {
  110. *this = vtable_storage(
  111. src.lookup(
  112. (typename ::boost::type_erasure::detail::rebind_placeholders<
  113. T, Bindings
  114. >::type*)0)...);
  115. }
  116. bool operator==(const vtable_storage& other) const
  117. { return compare_vtable<T...>::apply(*this, other); }
  118. template<class U>
  119. typename U::type lookup(U*) const
  120. {
  121. return static_cast<const vtable_entry<U>*>(this)->value;
  122. }
  123. };
  124. // Provide this specialization manually.
  125. // gcc 4.7.2 fails to instantiate the primary template.
  126. template<>
  127. struct vtable_storage<>
  128. {
  129. vtable_storage() = default;
  130. template<class Bindings, class Src>
  131. void convert_from(const Src& /*src*/) {}
  132. bool operator==(const vtable_storage& /*other*/) const
  133. { return true; }
  134. };
  135. template<class... T>
  136. struct make_vtable_impl<stored_arg_pack<T...> >
  137. {
  138. typedef vtable_storage<T...> type;
  139. };
  140. template<class Table, class... T>
  141. struct vtable_init
  142. {
  143. static constexpr Table value = Table(T::value...);
  144. };
  145. template<class Table, class... T>
  146. constexpr Table vtable_init<Table, T...>::value;
  147. template<class Table, class... T>
  148. struct make_vtable_init_impl<Table, stored_arg_pack<T...> >
  149. {
  150. typedef vtable_init<Table, T...> type;
  151. };
  152. #else
  153. template<int N>
  154. struct make_vtable_impl;
  155. template<class Seq>
  156. struct make_vtable
  157. {
  158. typedef typename make_vtable_impl<
  159. (::boost::mpl::size<Seq>::value)>::template apply<Seq>::type type;
  160. };
  161. template<int N>
  162. struct make_vtable_init_impl;
  163. template<class Seq, class Table>
  164. struct make_vtable_init
  165. {
  166. typedef typename make_vtable_init_impl<
  167. (::boost::mpl::size<Seq>::value)>::template apply<Seq, Table>::type type;
  168. };
  169. #define BOOST_PP_FILENAME_1 <boost/type_erasure/detail/vtable.hpp>
  170. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_FUNCTIONS)
  171. #include BOOST_PP_ITERATE()
  172. #endif
  173. }
  174. }
  175. }
  176. #endif
  177. #else
  178. #define N BOOST_PP_ITERATION()
  179. #define BOOST_TYPE_ERASURE_VTABLE_ENTRY(z, n, data) \
  180. typename BOOST_PP_CAT(T, n)::type BOOST_PP_CAT(t, n); \
  181. typename BOOST_PP_CAT(T, n)::type lookup(BOOST_PP_CAT(T, n)*) const \
  182. { \
  183. return BOOST_PP_CAT(t, n); \
  184. }
  185. #define BOOST_TYPE_ERASURE_VTABLE_COMPARE(z, n, data) \
  186. && BOOST_PP_CAT(t, n) == other.BOOST_PP_CAT(t, n)
  187. #define BOOST_TYPE_ERASURE_VTABLE_INIT(z, n, data) \
  188. BOOST_PP_CAT(data, n)::value
  189. #define BOOST_TYPE_ERASURE_AT(z, n, data) \
  190. typename ::boost::mpl::at_c<data, n>::type
  191. #define BOOST_TYPE_ERASURE_CONVERT_ELEMENT(z, n, data) \
  192. BOOST_PP_CAT(t, n) = src.lookup( \
  193. (typename ::boost::type_erasure::detail::rebind_placeholders< \
  194. BOOST_PP_CAT(T, n), Bindings \
  195. >::type*)0 \
  196. );
  197. #if N != 0
  198. template<BOOST_PP_ENUM_PARAMS(N, class T)>
  199. #else
  200. template<class D = void>
  201. #endif
  202. struct BOOST_PP_CAT(vtable_storage, N)
  203. {
  204. BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_VTABLE_ENTRY, ~)
  205. template<class Bindings, class Src>
  206. void convert_from(const Src& BOOST_PP_EXPR_IF(N, src))
  207. {
  208. BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_CONVERT_ELEMENT, ~)
  209. }
  210. bool operator==(const BOOST_PP_CAT(vtable_storage, N)& BOOST_PP_EXPR_IF(N, other)) const
  211. { return true BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_VTABLE_COMPARE, ~); }
  212. };
  213. template<>
  214. struct make_vtable_impl<N>
  215. {
  216. template<class Seq>
  217. struct apply
  218. {
  219. typedef ::boost::type_erasure::detail::BOOST_PP_CAT(vtable_storage, N)<
  220. BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_AT, Seq)
  221. > type;
  222. };
  223. };
  224. template<class Table BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  225. struct BOOST_PP_CAT(vtable_init, N)
  226. {
  227. static const Table value;
  228. };
  229. template<class Table BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  230. const Table BOOST_PP_CAT(vtable_init, N)<
  231. Table BOOST_PP_ENUM_TRAILING_PARAMS(N, T)>::value =
  232. {
  233. BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_VTABLE_INIT, T)
  234. };
  235. template<>
  236. struct make_vtable_init_impl<N>
  237. {
  238. template<class Seq, class Table>
  239. struct apply
  240. {
  241. typedef ::boost::type_erasure::detail::BOOST_PP_CAT(vtable_init, N)<
  242. Table
  243. BOOST_PP_ENUM_TRAILING(N, BOOST_TYPE_ERASURE_AT, Seq)
  244. > type;
  245. };
  246. };
  247. #undef BOOST_TYPE_ERASURE_CONVERT_ELEMENT
  248. #undef BOOST_TYPE_ERASURE_AT
  249. #undef BOOST_TYPE_ERASURE_VTABLE_INIT
  250. #undef BOOST_TYPE_ERASURE_VTABLE_COMPARE
  251. #undef BOOST_TYPE_ERASURE_VTABLE_ENTRY
  252. #undef N
  253. #endif