1 // Boost.TypeErasure library
3 // Copyright 2011-2012 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_REQUIRE_MATCH_HPP_INCLUDED
14 #define BOOST_TYPE_ERASURE_REQUIRE_MATCH_HPP_INCLUDED
16 #include <boost/throw_exception.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/mpl/and.hpp>
19 #include <boost/type_traits/is_same.hpp>
20 #include <boost/preprocessor/cat.hpp>
21 #include <boost/preprocessor/facilities/intercept.hpp>
22 #include <boost/preprocessor/iteration/iterate.hpp>
23 #include <boost/preprocessor/repetition/repeat.hpp>
24 #include <boost/preprocessor/repetition/enum_params.hpp>
25 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
26 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
27 #include <boost/type_erasure/detail/extract_concept.hpp>
28 #include <boost/type_erasure/relaxed.hpp>
29 #include <boost/type_erasure/check_match.hpp>
30 #include <boost/type_erasure/exception.hpp>
33 namespace type_erasure {
35 template<class Concept>
38 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
41 * Checks that the actual types stored in all the @ref any
42 * arguments match the types specified by @c binding. If
43 * they do not match then,
44 * - If @ref relaxed is in @c Concept, throws @ref bad_function_call.
45 * - Otherwise the behavior is undefined.
47 * If @c binding is not specified, it will be deduced from
50 * \post \call<code>(binding, f, args...)</code> is valid.
52 template<class Concept, class Op, class... U>
53 void require_match(const binding<Concept>& binding_arg, const Op& f, U&&... args);
58 template<class Op, class... U>
59 void require_match(const Op& f, U&&... args);
63 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
67 template<class Concept, class Op, class... U>
68 void require_match_impl(
70 const ::boost::type_erasure::binding<Concept>& table,
74 if(!::boost::type_erasure::check_match(table, op, std::forward<U>(arg)...)) {
75 BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call());
79 template<class Concept, class Op, class... U>
80 void require_match_impl(
82 const ::boost::type_erasure::binding<Concept>&,
87 template<class Op, class... U>
88 void require_match_impl(
93 if(!::boost::type_erasure::check_match(op, ::std::forward<U>(arg)...)) {
94 BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call());
98 template<class Op, class... U>
99 void require_match_impl(
100 ::boost::mpl::false_,
107 template<class Concept, class Op, class... U>
109 const ::boost::type_erasure::binding<Concept>& table,
113 ::boost::type_erasure::is_relaxed<Concept> cond;
114 ::boost::type_erasure::detail::require_match_impl(cond, table, op, ::std::forward<U>(arg)...);
117 template<class Op, class... U>
122 ::boost::type_erasure::is_relaxed<
123 typename ::boost::type_erasure::detail::extract_concept<
124 typename ::boost::type_erasure::detail::get_signature<Op>::type,
127 ::boost::type_erasure::detail::require_match_impl(cond, op, ::std::forward<U>(arg)...);
132 #define BOOST_PP_FILENAME_1 <boost/type_erasure/require_match.hpp>
133 #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY)
134 #include BOOST_PP_ITERATE()
147 #define N BOOST_PP_ITERATION()
149 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
151 #define BOOST_TYPE_ERASURE_FORWARD_ARGS(N, X, x) BOOST_PP_ENUM_TRAILING_PARAMS(N, x)
154 #define BOOST_TYPE_ERASURE_FORWARD_ARGS_I(z, n, data) std::forward<BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 0, data), n)>(BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 1, data), n))
155 #define BOOST_TYPE_ERASURE_FORWARD_ARGS(N, X, x) BOOST_PP_ENUM_TRAILING(N, BOOST_TYPE_ERASURE_FORWARD_ARGS_I, (X, x))
163 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
165 void require_match_impl(
167 const ::boost::type_erasure::binding<Concept>& table,
169 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
171 if(!::boost::type_erasure::check_match
172 (table, op BOOST_TYPE_ERASURE_FORWARD_ARGS(N, U, arg))) {
173 BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call());
180 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
182 void require_match_impl(
183 ::boost::mpl::false_,
184 const ::boost::type_erasure::binding<Concept>&,
186 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF BOOST_PP_INTERCEPT))
193 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
195 void require_match_impl(
198 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
200 if(!::boost::type_erasure::check_match
201 (op BOOST_TYPE_ERASURE_FORWARD_ARGS(N, U, arg))) {
202 BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call());
208 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
210 void require_match_impl(
211 ::boost::mpl::false_,
213 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF BOOST_PP_INTERCEPT))
223 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
226 const ::boost::type_erasure::binding<Concept>& table,
228 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
230 ::boost::type_erasure::is_relaxed<Concept> cond;
231 ::boost::type_erasure::detail::require_match_impl
232 (cond, table, op BOOST_TYPE_ERASURE_FORWARD_ARGS(N, U, arg));
239 BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
243 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
245 ::boost::type_erasure::is_relaxed<
246 typename ::boost::type_erasure::detail::BOOST_PP_CAT(do_extract_concept, N)<
247 typename ::boost::type_erasure::detail::get_signature<Op>::type,
248 BOOST_PP_ENUM_PARAMS(N, U)>::type
250 ::boost::type_erasure::detail::require_match_impl
251 (cond, op BOOST_TYPE_ERASURE_FORWARD_ARGS(N, U, arg));
257 #undef BOOST_TYPE_ERASURE_FORWARD_ARGS
258 #undef BOOST_TYPE_ERASURE_FORWARD_ARGS_I