1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
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)
11 #if !defined(BOOST_PP_IS_ITERATING)
13 #ifndef BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED
14 #define BOOST_TYPE_ERASURE_DETAIL_ADAPT_TO_VTABLE_HPP_INCLUDED
16 #include <boost/detail/workaround.hpp>
17 #include <boost/utility/addressof.hpp>
18 #include <boost/mpl/if.hpp>
19 #include <boost/mpl/eval_if.hpp>
20 #include <boost/mpl/has_xxx.hpp>
21 #include <boost/type_traits/function_traits.hpp>
22 #include <boost/type_traits/remove_cv.hpp>
23 #include <boost/preprocessor/cat.hpp>
24 #include <boost/preprocessor/iteration/iterate.hpp>
25 #include <boost/preprocessor/repetition/enum.hpp>
26 #include <boost/preprocessor/repetition/enum_params.hpp>
27 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
28 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
29 #include <boost/type_erasure/detail/get_signature.hpp>
30 #include <boost/type_erasure/detail/storage.hpp>
31 #include <boost/type_erasure/is_placeholder.hpp>
32 #include <boost/type_erasure/config.hpp>
35 namespace type_erasure {
39 template<class T, class Out>
40 struct get_placeholders;
42 template<class PrimitiveConcept, class Sig>
43 struct vtable_adapter;
45 template<class PrimitiveConcept, class Sig, class Out>
46 struct get_placeholders<vtable_adapter<PrimitiveConcept, Sig>, Out>
48 typedef typename get_placeholders<PrimitiveConcept, Out>::type type;
52 struct replace_param_for_vtable
54 typedef typename ::boost::mpl::if_<
55 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
56 const ::boost::type_erasure::detail::storage&,
62 struct replace_param_for_vtable<T&>
64 typedef typename ::boost::mpl::if_<
65 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
66 ::boost::type_erasure::detail::storage&,
72 struct replace_param_for_vtable<const T&>
74 typedef typename ::boost::mpl::if_<
75 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
76 const ::boost::type_erasure::detail::storage&,
81 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
84 struct replace_param_for_vtable<T&&>
86 typedef typename ::boost::mpl::if_<
87 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
88 ::boost::type_erasure::detail::storage&&,
96 struct replace_result_for_vtable
98 typedef typename ::boost::mpl::if_<
99 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
100 ::boost::type_erasure::detail::storage,
106 struct replace_result_for_vtable<T&>
108 typedef typename ::boost::mpl::if_<
109 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
110 ::boost::type_erasure::detail::storage&,
116 struct replace_result_for_vtable<const T&>
118 typedef typename ::boost::mpl::if_<
119 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
120 ::boost::type_erasure::detail::storage&,
125 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
128 struct replace_result_for_vtable<T&&>
130 typedef typename ::boost::mpl::if_<
131 ::boost::type_erasure::is_placeholder<typename ::boost::remove_cv<T>::type>,
132 ::boost::type_erasure::detail::storage&&,
140 struct get_vtable_signature;
142 BOOST_MPL_HAS_XXX_TRAIT_DEF(type)
145 struct is_internal_concept :
146 ::boost::type_erasure::detail::has_type<T>
149 template<class PrimitiveConcept>
150 struct adapt_to_vtable
152 typedef ::boost::type_erasure::detail::vtable_adapter<
154 typename ::boost::type_erasure::detail::get_vtable_signature<
155 typename ::boost::type_erasure::detail::get_signature<
162 template<class Concept>
163 struct maybe_adapt_to_vtable
165 typedef typename ::boost::mpl::eval_if<
166 ::boost::type_erasure::detail::is_internal_concept<Concept>,
167 ::boost::mpl::identity<Concept>,
168 ::boost::type_erasure::detail::adapt_to_vtable<Concept>
172 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
173 !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
174 !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
176 template<class PrimitiveConcept, class Sig, class ConceptSig>
177 struct vtable_adapter_impl;
179 template<class PrimitiveConcept, class R, class... T, class R2, class... U>
180 struct vtable_adapter_impl<PrimitiveConcept, R(T...), R2(U...)>
182 typedef R (*type)(T...);
183 static R value(T... arg)
185 return PrimitiveConcept::apply(
186 ::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...);
190 template<class PrimitiveConcept, class... T, class R2, class... U>
191 struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage(T...), R2(U...)>
193 typedef ::boost::type_erasure::detail::storage (*type)(T...);
194 static ::boost::type_erasure::detail::storage value(T... arg)
196 return ::boost::type_erasure::detail::storage(
197 PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...));
201 template<class PrimitiveConcept, class... T, class R2, class... U>
202 struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage&(T...), R2(U...)>
204 typedef ::boost::type_erasure::detail::storage (*type)(T...);
205 static ::boost::type_erasure::detail::storage value(T... arg)
207 ::boost::type_erasure::detail::storage result;
208 typename ::boost::remove_reference<R2>::type* p =
210 PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...));
211 result.data = const_cast<void*>(static_cast<const void*>(p));
216 template<class PrimitiveConcept, class... T, class R2, class... U>
217 struct vtable_adapter_impl<PrimitiveConcept, ::boost::type_erasure::detail::storage&&(T...), R2(U...)>
219 typedef ::boost::type_erasure::detail::storage (*type)(T...);
220 static ::boost::type_erasure::detail::storage value(T... arg)
222 ::boost::type_erasure::detail::storage result;
223 R2 tmp = PrimitiveConcept::apply(::boost::type_erasure::detail::extract<U>(std::forward<T>(arg))...);
224 typename ::boost::remove_reference<R2>::type* p = ::boost::addressof(tmp);
225 result.data = const_cast<void*>(static_cast<const void*>(p));
230 template<class PrimitiveConcept, class Sig>
231 struct vtable_adapter
232 : vtable_adapter_impl<
235 typename ::boost::type_erasure::detail::get_signature<
241 template<class R, class... T>
242 struct get_vtable_signature<R(T...)>
244 typedef typename ::boost::type_erasure::detail::replace_result_for_vtable<
246 >::type type(typename ::boost::type_erasure::detail::replace_param_for_vtable<T>::type...);
251 #define BOOST_PP_FILENAME_1 <boost/type_erasure/detail/adapt_to_vtable.hpp>
252 #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY)
253 #include BOOST_PP_ITERATE()
265 #define N BOOST_PP_ITERATION()
267 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
269 #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \
270 ::boost::type_erasure::detail::extract< \
272 BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \
273 >(std::forward<BOOST_PP_CAT(T, n)>(BOOST_PP_CAT(arg, n)))
277 #define BOOST_TYPE_ERASURE_EXTRACT(z, n, data) \
278 ::boost::type_erasure::detail::extract< \
280 BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) \
281 >(BOOST_PP_CAT(arg, n))
285 #define BOOST_TYPE_ERASURE_REPLACE_PARAM(z, n, data) \
286 typename ::boost::type_erasure::detail::replace_param_for_vtable< \
287 BOOST_PP_CAT(T, n)>::type
289 template<class PrimitiveConcept, class R
290 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
291 struct vtable_adapter<PrimitiveConcept, R(BOOST_PP_ENUM_PARAMS(N, T))>
293 typedef R (*type)(BOOST_PP_ENUM_PARAMS(N, T));
294 static R value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
297 typedef typename ::boost::function_traits<
298 typename ::boost::type_erasure::detail::get_signature<
303 return PrimitiveConcept::apply(
304 BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~));
308 template<class PrimitiveConcept
309 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
310 struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage(BOOST_PP_ENUM_PARAMS(N, T))>
312 typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
313 static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
316 typedef typename ::boost::function_traits<
317 typename ::boost::type_erasure::detail::get_signature<
322 return ::boost::type_erasure::detail::storage(
323 PrimitiveConcept::apply(
324 BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)));
328 template<class PrimitiveConcept
329 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
330 struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage&(BOOST_PP_ENUM_PARAMS(N, T))>
332 typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
333 static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
335 typedef typename ::boost::function_traits<
336 typename ::boost::type_erasure::detail::get_signature<
340 ::boost::type_erasure::detail::storage result;
341 typename ::boost::remove_reference<typename traits::result_type>::type* p =
343 PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~)));
344 result.data = const_cast<void*>(static_cast<const void*>(p));
349 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
351 template<class PrimitiveConcept
352 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
353 struct vtable_adapter<PrimitiveConcept, ::boost::type_erasure::detail::storage&&(BOOST_PP_ENUM_PARAMS(N, T))>
355 typedef ::boost::type_erasure::detail::storage (*type)(BOOST_PP_ENUM_PARAMS(N, T));
356 static ::boost::type_erasure::detail::storage value(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
358 typedef typename ::boost::function_traits<
359 typename ::boost::type_erasure::detail::get_signature<
363 ::boost::type_erasure::detail::storage result;
364 typename traits::result_type tmp =
365 PrimitiveConcept::apply(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_EXTRACT, ~));
366 typename ::boost::remove_reference<typename traits::result_type>::type* p =
367 ::boost::addressof(tmp);
368 result.data = const_cast<void*>(static_cast<const void*>(p));
375 template<class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
376 struct get_vtable_signature<R(BOOST_PP_ENUM_PARAMS(N, T))>
378 typedef typename ::boost::type_erasure::detail::replace_result_for_vtable<
380 >::type type(BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_REPLACE_PARAM, ~));
383 #undef BOOST_TYPE_ERASURE_REPLACE_PARAM
384 #undef BOOST_TYPE_ERASURE_EXTRACT