// Boost.TypeErasure library // // Copyright 2011 Steven Watanabe // // Distributed under the Boost Software License Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // $Id$ #if !defined(BOOST_PP_IS_ITERATING) #ifndef BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED #define BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace type_erasure { namespace detail { template struct get_placeholders; template struct vtable_adapter; template struct get_placeholders, Out> { typedef typename get_placeholders::type type; }; template struct replace_param_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, const ::boost::type_erasure::detail::storage&, T >::type type; }; template struct replace_param_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage&, T& >::type type; }; template struct replace_param_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, const ::boost::type_erasure::detail::storage&, const T& >::type type; }; #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES template struct replace_param_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage&&, T&& >::type type; }; #endif template struct replace_result_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage, T >::type type; }; template struct replace_result_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage&, T& >::type type; }; template struct replace_result_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage&, const T& >::type type; }; #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES template struct replace_result_for_vtable { typedef typename ::boost::mpl::if_< ::boost::type_erasure::is_placeholder::type>, ::boost::type_erasure::detail::storage&&, T&& >::type type; }; #endif template struct get_vtable_signature; BOOST_MPL_HAS_XXX_TRAIT_DEF(type) template struct is_internal_concept : ::boost::type_erasure::detail::has_type {}; template struct adapt_to_vtable { typedef ::boost::type_erasure::detail::vtable_adapter< PrimitiveConcept, typename ::boost::type_erasure::detail::get_vtable_signature< typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type >::type > type; }; template struct maybe_adapt_to_vtable { typedef typename ::boost::mpl::eval_if< ::boost::type_erasure::detail::is_internal_concept, ::boost::mpl::identity, ::boost::type_erasure::detail::adapt_to_vtable >::type type; }; #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ !BOOST_WORKAROUND(BOOST_MSVC, == 1800) template struct vtable_adapter_impl; template struct vtable_adapter_impl { typedef R (*type)(T...); static R value(T... arg) { return PrimitiveConcept::apply( ::boost::type_erasure::detail::extract(std::forward(arg))...); } }; template struct vtable_adapter_impl { typedef ::boost::type_erasure::detail::storage (*type)(T...); static ::boost::type_erasure::detail::storage value(T... arg) { return ::boost::type_erasure::detail::storage( PrimitiveConcept::apply(::boost::type_erasure::detail::extract(std::forward(arg))...)); } }; template struct vtable_adapter_impl { typedef ::boost::type_erasure::detail::storage (*type)(T...); static ::boost::type_erasure::detail::storage value(T... arg) { ::boost::type_erasure::detail::storage result; typename ::boost::remove_reference::type* p = ::boost::addressof( PrimitiveConcept::apply(::boost::type_erasure::detail::extract(std::forward(arg))...)); result.data = const_cast(static_cast(p)); return result; } }; template struct vtable_adapter_impl { typedef ::boost::type_erasure::detail::storage (*type)(T...); static ::boost::type_erasure::detail::storage value(T... arg) { ::boost::type_erasure::detail::storage result; R2 tmp = PrimitiveConcept::apply(::boost::type_erasure::detail::extract(std::forward(arg))...); typename ::boost::remove_reference::type* p = ::boost::addressof(tmp); result.data = const_cast(static_cast(p)); return result; } }; template struct vtable_adapter : vtable_adapter_impl< PrimitiveConcept, Sig, typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type > {}; template struct get_vtable_signature { typedef typename ::boost::type_erasure::detail::replace_result_for_vtable< R >::type type(typename ::boost::type_erasure::detail::replace_param_for_vtable::type...); }; #else #define BOOST_PP_FILENAME_1 #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY) #include BOOST_PP_ITERATE() #endif } } } #endif #else #define N BOOST_PP_ITERATION() #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \ ::boost::type_erasure::detail::extract< \ typename traits:: \ BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \ >(std::forward(BOOST_PP_CAT(arg, n))) #else #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \ ::boost::type_erasure::detail::extract< \ typename traits:: \ BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \ >(BOOST_PP_CAT(arg, n)) #endif #define BOOST_TYPE_ERASURE_REPLACE_PARAM(z, n, data) \ typename ::boost::type_erasure::detail::replace_param_for_vtable< \ BOOST_PP_CAT(T, n)>::type template struct vtable_adapter { typedef R (*type)(BOOST_PP_ENUM_PARAMS(N, T)); static R value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg)) { #if N > 0 typedef typename ::boost::function_traits< typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type > traits; #endif return PrimitiveConcept::apply( BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)); } }; template struct vtable_adapter { typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T)); static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg)) { #if N > 0 typedef typename ::boost::function_traits< typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type > traits; #endif return ::boost::type_erasure::detail::storage( PrimitiveConcept::apply( BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~))); } }; template struct vtable_adapter { typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T)); static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg)) { typedef typename ::boost::function_traits< typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type > traits; ::boost::type_erasure::detail::storage result; typename ::boost::remove_reference::type* p = ::boost::addressof( PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~))); result.data = const_cast(static_cast(p)); return result; } }; #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES template struct vtable_adapter { typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T)); static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg)) { typedef typename ::boost::function_traits< typename ::boost::type_erasure::detail::get_signature< PrimitiveConcept >::type > traits; ::boost::type_erasure::detail::storage result; typename traits::result_type tmp = PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)); typename ::boost::remove_reference::type* p = ::boost::addressof(tmp); result.data = const_cast(static_cast(p)); return result; } }; #endif template struct get_vtable_signature { typedef typename ::boost::type_erasure::detail::replace_result_for_vtable< R >::type type(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_REPLACE_PARAM, ~)); }; #undef BOOST_TYPE_ERASURE_REPLACE_PARAM #undef BOOST_TYPE_ERASURE_EXTRACT #undef N #endif