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