dynamic_vtable.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2015 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. #ifndef BOOST_TYPE_ERASURE_DETAIL_DYNAMIC_VTABLE_HPP_INCLUDED
  11. #define BOOST_TYPE_ERASURE_DETAIL_DYNAMIC_VTABLE_HPP_INCLUDED
  12. #include <boost/type_erasure/detail/get_placeholders.hpp>
  13. #include <boost/type_erasure/detail/rebind_placeholders.hpp>
  14. #include <boost/type_erasure/detail/normalize.hpp>
  15. #include <boost/type_erasure/detail/adapt_to_vtable.hpp>
  16. #include <boost/type_erasure/detail/vtable.hpp>
  17. #include <boost/type_erasure/static_binding.hpp>
  18. #include <boost/type_erasure/register_binding.hpp>
  19. #include <boost/mpl/transform.hpp>
  20. #include <boost/mpl/at.hpp>
  21. #include <boost/mpl/set.hpp>
  22. #include <boost/mpl/for_each.hpp>
  23. #include <boost/mpl/index_of.hpp>
  24. #include <typeinfo>
  25. namespace boost {
  26. namespace type_erasure {
  27. namespace detail {
  28. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  29. template<class P>
  30. struct dynamic_binding_impl {
  31. const std::type_info * type;
  32. dynamic_binding_impl() = default;
  33. constexpr dynamic_binding_impl(const std::type_info * t) : type(t) {}
  34. };
  35. template<class T>
  36. struct dynamic_binding_element {
  37. typedef const std::type_info * type;
  38. };
  39. template<class Table>
  40. struct append_to_key {
  41. template<class P>
  42. void operator()(P) {
  43. key->push_back(static_cast<const dynamic_binding_impl<P>*>(table)->type);
  44. }
  45. const Table * table;
  46. key_type * key;
  47. };
  48. template<class... P>
  49. struct dynamic_vtable : dynamic_binding_impl<P>... {
  50. dynamic_vtable() = default;
  51. constexpr dynamic_vtable(typename dynamic_binding_element<P>::type ...t) : dynamic_binding_impl<P>(t)... {}
  52. template<class F>
  53. typename F::type lookup(F*) const {
  54. key_type key;
  55. #ifndef BOOST_TYPE_ERASURE_USE_MP11
  56. typedef typename ::boost::type_erasure::detail::get_placeholders<F, ::boost::mpl::set0<> >::type placeholders;
  57. #else
  58. typedef typename ::boost::type_erasure::detail::get_placeholders<F, ::boost::mp11::mp_list<> >::type placeholders;
  59. #endif
  60. typedef typename ::boost::mpl::fold<
  61. placeholders,
  62. ::boost::mpl::map0<>,
  63. ::boost::type_erasure::detail::counting_map_appender
  64. >::type placeholder_map;
  65. key.push_back(&typeid(typename ::boost::type_erasure::detail::rebind_placeholders<F, placeholder_map>::type));
  66. ::boost::mpl::for_each<placeholders>(append_to_key<dynamic_vtable>{this, &key});
  67. return reinterpret_cast<typename F::type>(lookup_function_impl(key));
  68. }
  69. template<class Bindings>
  70. void init() {
  71. *this = dynamic_vtable(&typeid(typename boost::mpl::at<Bindings, P>::type)...);
  72. }
  73. template<class Bindings, class Src>
  74. void convert_from(const Src& src) {
  75. #ifndef BOOST_TYPE_ERASURE_USE_MP11
  76. *this = dynamic_vtable(
  77. (&src.lookup((::boost::type_erasure::typeid_<typename ::boost::mpl::at<Bindings, P>::type>*)0)())...);
  78. #else
  79. *this = dynamic_vtable(
  80. (&src.lookup((::boost::type_erasure::typeid_< ::boost::mp11::mp_second< ::boost::mp11::mp_map_find<Bindings, P> > >*)0)())...);
  81. #endif
  82. }
  83. };
  84. template<class L>
  85. struct make_dynamic_vtable_impl;
  86. template<class... P>
  87. struct make_dynamic_vtable_impl<stored_arg_pack<P...> >
  88. {
  89. typedef dynamic_vtable<P...> type;
  90. };
  91. template<class PlaceholderList>
  92. struct make_dynamic_vtable
  93. : make_dynamic_vtable_impl<typename make_arg_pack<PlaceholderList>::type>
  94. {};
  95. #else
  96. template<class Bindings>
  97. struct dynamic_vtable_initializer
  98. {
  99. dynamic_vtable_initializer(const std::type_info**& ptr) : types(&ptr) {}
  100. template<class P>
  101. void operator()(P)
  102. {
  103. *(*types)++ = &typeid(typename ::boost::mpl::at<Bindings, P>::type);
  104. }
  105. const ::std::type_info*** types;
  106. };
  107. template<class Placeholders>
  108. struct dynamic_vtable
  109. {
  110. const ::std::type_info * types[(::boost::mpl::size<Placeholders>::value)];
  111. struct append_to_key
  112. {
  113. append_to_key(const std::type_info * const * t, key_type* k) : types(t), key(k) {}
  114. template<class P>
  115. void operator()(P)
  116. {
  117. key->push_back(types[(::boost::mpl::index_of<Placeholders, P>::type::value)]);
  118. }
  119. const std::type_info * const * types;
  120. key_type * key;
  121. };
  122. template<class F>
  123. typename F::type lookup(F*) const {
  124. key_type key;
  125. typedef typename ::boost::type_erasure::detail::get_placeholders<F, ::boost::mpl::set0<> >::type placeholders;
  126. typedef typename ::boost::mpl::fold<
  127. placeholders,
  128. ::boost::mpl::map0<>,
  129. ::boost::type_erasure::detail::counting_map_appender
  130. >::type placeholder_map;
  131. key.push_back(&typeid(typename ::boost::type_erasure::detail::rebind_placeholders<F, placeholder_map>::type));
  132. ::boost::mpl::for_each<placeholders>(append_to_key(types, &key));
  133. return reinterpret_cast<typename F::type>(lookup_function_impl(key));
  134. }
  135. template<class Bindings>
  136. void init()
  137. {
  138. const std::type_info* ptr = types;
  139. ::boost::mpl::for_each<Placeholders>(dynamic_vtable_initializer<Bindings>(ptr));
  140. }
  141. template<class Bindings, class Src>
  142. struct converter
  143. {
  144. converter(const std::type_info**& t, const Src& s) : types(&t), src(&s) {}
  145. template<class P>
  146. void operator()(P)
  147. {
  148. *(*types)++ = &src->lookup((::boost::type_erasure::typeid_<typename ::boost::mpl::at<Bindings, P>::type>*)0)();
  149. }
  150. const std::type_info*** types;
  151. const Src * src;
  152. };
  153. template<class Bindings, class Src>
  154. void convert_from(const Src& src) {
  155. const ::std::type_info** ptr = types;
  156. ::boost::mpl::for_each<Placeholders>(converter<Bindings, Src>(ptr, src));
  157. }
  158. };
  159. template<class Placeholders>
  160. struct make_dynamic_vtable
  161. {
  162. typedef dynamic_vtable<Placeholders> type;
  163. };
  164. #endif
  165. }
  166. }
  167. }
  168. #endif