]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.TypeErasure library |
2 | // | |
3 | // Copyright 2011-2012 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 | #if !defined(BOOST_PP_IS_ITERATING) | |
12 | ||
13 | #ifndef BOOST_TYPE_ERASURE_REQUIRE_MATCH_HPP_INCLUDED | |
14 | #define BOOST_TYPE_ERASURE_REQUIRE_MATCH_HPP_INCLUDED | |
15 | ||
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> | |
31 | ||
32 | namespace boost { | |
33 | namespace type_erasure { | |
34 | ||
35 | template<class Concept> | |
36 | class binding; | |
37 | ||
38 | #ifdef BOOST_TYPE_ERASURE_DOXYGEN | |
39 | ||
40 | /** | |
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. | |
46 | * | |
47 | * If @c binding is not specified, it will be deduced from | |
48 | * the arguments. | |
49 | * | |
50 | * \post \call<code>(binding, f, args...)</code> is valid. | |
51 | */ | |
52 | template<class Concept, class Op, class... U> | |
53 | void require_match(const binding<Concept>& binding_arg, const Op& f, U&&... args); | |
54 | ||
55 | /** | |
56 | * \overload | |
57 | */ | |
58 | template<class Op, class... U> | |
59 | void require_match(const Op& f, U&&... args); | |
60 | ||
61 | #else | |
62 | ||
63 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
64 | ||
65 | namespace detail { | |
66 | ||
67 | template<class Concept, class Op, class... U> | |
68 | void require_match_impl( | |
69 | ::boost::mpl::true_, | |
70 | const ::boost::type_erasure::binding<Concept>& table, | |
71 | const Op& op, | |
72 | U&&... arg) | |
73 | { | |
74 | if(!::boost::type_erasure::check_match(table, op, std::forward<U>(arg)...)) { | |
75 | BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call()); | |
76 | } | |
77 | } | |
78 | ||
79 | template<class Concept, class Op, class... U> | |
80 | void require_match_impl( | |
81 | ::boost::mpl::false_, | |
82 | const ::boost::type_erasure::binding<Concept>&, | |
83 | const Op&, | |
84 | U&&...) | |
85 | {} | |
86 | ||
87 | template<class Op, class... U> | |
88 | void require_match_impl( | |
89 | ::boost::mpl::true_, | |
90 | const Op& op, | |
91 | U&&... arg) | |
92 | { | |
93 | if(!::boost::type_erasure::check_match(op, ::std::forward<U>(arg)...)) { | |
94 | BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_function_call()); | |
95 | } | |
96 | } | |
97 | ||
98 | template<class Op, class... U> | |
99 | void require_match_impl( | |
100 | ::boost::mpl::false_, | |
101 | const Op&, | |
102 | U&&...) | |
103 | {} | |
104 | ||
105 | } | |
106 | ||
107 | template<class Concept, class Op, class... U> | |
108 | void require_match( | |
109 | const ::boost::type_erasure::binding<Concept>& table, | |
110 | const Op& op, | |
111 | U&&... arg) | |
112 | { | |
113 | ::boost::type_erasure::is_relaxed<Concept> cond; | |
114 | ::boost::type_erasure::detail::require_match_impl(cond, table, op, ::std::forward<U>(arg)...); | |
115 | } | |
116 | ||
117 | template<class Op, class... U> | |
118 | void require_match( | |
119 | const Op& op, | |
120 | U&&... arg) | |
121 | { | |
122 | ::boost::type_erasure::is_relaxed< | |
123 | typename ::boost::type_erasure::detail::extract_concept< | |
124 | typename ::boost::type_erasure::detail::get_signature<Op>::type, | |
125 | U...>::type | |
126 | > cond; | |
127 | ::boost::type_erasure::detail::require_match_impl(cond, op, ::std::forward<U>(arg)...); | |
128 | } | |
129 | ||
130 | #else | |
131 | ||
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() | |
135 | ||
136 | #endif | |
137 | ||
138 | #endif | |
139 | ||
140 | } | |
141 | } | |
142 | ||
143 | #endif | |
144 | ||
145 | #else | |
146 | ||
147 | #define N BOOST_PP_ITERATION() | |
148 | ||
149 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
150 | #define RREF & | |
151 | #define BOOST_TYPE_ERASURE_FORWARD_ARGS(N, X, x) BOOST_PP_ENUM_TRAILING_PARAMS(N, x) | |
152 | #else | |
153 | #define RREF && | |
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)) | |
156 | #endif | |
157 | ||
158 | namespace detail { | |
159 | ||
160 | template< | |
161 | class Concept, | |
162 | class Op | |
163 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
164 | > | |
165 | void require_match_impl( | |
166 | ::boost::mpl::true_, | |
167 | const ::boost::type_erasure::binding<Concept>& table, | |
168 | const Op& op | |
169 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
170 | { | |
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()); | |
174 | } | |
175 | } | |
176 | ||
177 | template< | |
178 | class Concept, | |
179 | class Op | |
180 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
181 | > | |
182 | void require_match_impl( | |
183 | ::boost::mpl::false_, | |
184 | const ::boost::type_erasure::binding<Concept>&, | |
185 | const Op& | |
186 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF BOOST_PP_INTERCEPT)) | |
187 | {} | |
188 | ||
189 | #if N != 0 | |
190 | ||
191 | template< | |
192 | class Op | |
193 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
194 | > | |
195 | void require_match_impl( | |
196 | ::boost::mpl::true_, | |
197 | const Op& op | |
198 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
199 | { | |
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()); | |
203 | } | |
204 | } | |
205 | ||
206 | template< | |
207 | class Op | |
208 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
209 | > | |
210 | void require_match_impl( | |
211 | ::boost::mpl::false_, | |
212 | const Op& | |
213 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF BOOST_PP_INTERCEPT)) | |
214 | {} | |
215 | ||
216 | #endif | |
217 | ||
218 | } | |
219 | ||
220 | template< | |
221 | class Concept, | |
222 | class Op | |
223 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
224 | > | |
225 | void require_match( | |
226 | const ::boost::type_erasure::binding<Concept>& table, | |
227 | const Op& op | |
228 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
229 | { | |
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)); | |
233 | } | |
234 | ||
235 | #if N != 0 | |
236 | ||
237 | template< | |
238 | class Op | |
239 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
240 | > | |
241 | void require_match( | |
242 | const Op& op | |
243 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
244 | { | |
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 | |
249 | > cond; | |
250 | ::boost::type_erasure::detail::require_match_impl | |
251 | (cond, op BOOST_TYPE_ERASURE_FORWARD_ARGS(N, U, arg)); | |
252 | } | |
253 | ||
254 | #endif | |
255 | ||
256 | #undef RREF | |
257 | #undef BOOST_TYPE_ERASURE_FORWARD_ARGS | |
258 | #undef BOOST_TYPE_ERASURE_FORWARD_ARGS_I | |
259 | #undef N | |
260 | ||
261 | #endif |