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_CHECK_MATCH_HPP_INCLUDED
14 #define BOOST_TYPE_ERASURE_CHECK_MATCH_HPP_INCLUDED
16 #include <boost/mpl/vector.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/mpl/is_sequence.hpp>
19 #include <boost/mpl/find_if.hpp>
20 #include <boost/mpl/not.hpp>
21 #include <boost/mpl/end.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/preprocessor/cat.hpp>
24 #include <boost/preprocessor/iteration/iterate.hpp>
25 #include <boost/preprocessor/repetition/repeat.hpp>
26 #include <boost/preprocessor/repetition/enum_params.hpp>
27 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
28 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
29 #include <boost/type_erasure/detail/extract_concept.hpp>
30 #include <boost/type_erasure/relaxed.hpp>
31 #include <boost/type_erasure/detail/access.hpp>
34 namespace type_erasure {
36 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
41 template<class Concept>
48 template<class P, class T, class Table>
49 bool maybe_check_table(const T& arg, const Table*& table, boost::mpl::true_)
52 table = &::boost::type_erasure::detail::access::table(arg);
55 return table->template find< ::boost::type_erasure::typeid_<P> >()() ==
56 ::boost::type_erasure::detail::access::table(arg).
57 template find< ::boost::type_erasure::typeid_<P> >()();
61 template<class P, class T, class Table>
62 bool maybe_check_table(const T&, const Table*&, boost::mpl::false_)
67 template<class Concept, class T>
70 ::boost::type_erasure::is_placeholder<T>,
71 ::boost::type_erasure::is_relaxed<Concept>
77 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
80 * If @ref relaxed is in @c Concept, checks whether the
81 * arguments to @c f match the types specified by
82 * @c binding. If @ref relaxed is not in @c Concept,
83 * returns true. If @c binding is not specified, it will
84 * be deduced from the arguments.
86 template<class Concept, class Op, class... U>
87 bool check_match(const binding<Concept>& binding_arg, const Op& f, U&&... args);
92 template<class Op, class... U>
93 bool check_match(const Op& f, U&&... args);
97 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
101 template<class Concept, class R>
102 bool check_table(const ::boost::type_erasure::binding<Concept>* /*t*/, R(*)())
107 template<class Concept, class R, class T0, class... T, class U0, class... U>
109 const ::boost::type_erasure::binding<Concept>* t,
114 typedef typename ::boost::remove_cv<
115 typename ::boost::remove_reference<T0>::type
117 if(!::boost::type_erasure::detail::maybe_check_table<t0>(
120 ::boost::type_erasure::detail::should_check<Concept, t0>()))
123 return check_table(t, static_cast<void(*)(T...)>(0), arg...);
128 template<class Concept, class Op, class... U>
130 const ::boost::type_erasure::binding<Concept>& table,
135 return ::boost::type_erasure::detail::check_table(
137 static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0),
141 #ifndef BOOST_TYPE_ERASURE_USE_MP11
151 const ::boost::type_erasure::binding<
152 typename ::boost::type_erasure::detail::extract_concept<
153 typename ::boost::type_erasure::detail::get_signature<Op>::type, U...>::type>* p = 0;
155 return ::boost::type_erasure::detail::check_table(
156 p, static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), arg...);
166 template<class R, class ... T>
167 struct get_args<R(T...)> { typedef ::boost::mp11::mp_list<T...> type; };
170 using get_args_t = typename get_args<Sig>::type;
182 const ::boost::type_erasure::binding<
183 ::boost::type_erasure::detail::extract_concept_t<
184 ::boost::type_erasure::detail::get_args_t<
185 typename ::boost::type_erasure::detail::get_signature<Op>::type
187 ::boost::mp11::mp_list< ::boost::remove_reference_t<U>...> > >* p = 0;
189 return ::boost::type_erasure::detail::check_table(
190 p, static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), arg...);
197 #define BOOST_PP_FILENAME_1 <boost/type_erasure/check_match.hpp>
198 #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY)
199 #include BOOST_PP_ITERATE()
214 #define N BOOST_PP_ITERATION()
216 #define BOOST_TYPE_ERASURE_CHECK_TABLE(z, n, data) \
217 typedef typename ::boost::remove_cv< \
218 typename ::boost::remove_reference<BOOST_PP_CAT(T, n)>::type \
219 >::type BOOST_PP_CAT(t, n); \
220 if(!::boost::type_erasure::detail::maybe_check_table<BOOST_PP_CAT(t, n)>( \
221 BOOST_PP_CAT(arg, n), \
223 ::boost::type_erasure::detail::should_check<Concept, BOOST_PP_CAT(t, n)>())) \
229 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)
230 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
232 BOOST_PP_CAT(check_table, N)(
233 const ::boost::type_erasure::binding<Concept>* t,
234 R (*)(BOOST_PP_ENUM_PARAMS(N, T))
235 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, const U, &arg))
237 BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_CHECK_TABLE, ~)
243 template<class Sig BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
244 struct BOOST_PP_CAT(do_extract_concept, N);
248 BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)
249 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
251 struct BOOST_PP_CAT(do_extract_concept, N)<
252 R(BOOST_PP_ENUM_PARAMS(N, T))
253 BOOST_PP_ENUM_TRAILING_PARAMS(N, U)
255 : ::boost::type_erasure::detail::BOOST_PP_CAT(extract_concept, N)<
256 BOOST_PP_ENUM_PARAMS(N, T)
257 BOOST_PP_ENUM_TRAILING_PARAMS(N, U)>
264 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
273 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
276 const ::boost::type_erasure::binding<Concept>& table,
278 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
281 return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)(
283 (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0
284 BOOST_PP_ENUM_TRAILING_PARAMS(N, arg));
291 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
295 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
297 const ::boost::type_erasure::binding<
298 typename ::boost::type_erasure::detail::BOOST_PP_CAT(do_extract_concept, N)<
299 typename ::boost::type_erasure::detail::get_signature<Op>::type,
300 BOOST_PP_ENUM_PARAMS(N, U)>::type>* p = 0;
302 return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)(
304 (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0
305 BOOST_PP_ENUM_TRAILING_PARAMS(N, arg));
309 #undef BOOST_TYPE_ERASURE_CHECK_TABLE