]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.TypeErasure library |
2 | // | |
3 | // Copyright 2011 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_CHECK_MATCH_HPP_INCLUDED | |
14 | #define BOOST_TYPE_ERASURE_CHECK_MATCH_HPP_INCLUDED | |
15 | ||
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> | |
32 | ||
33 | namespace boost { | |
34 | namespace type_erasure { | |
35 | ||
36 | template<class T> | |
37 | struct typeid_; | |
38 | ||
39 | template<class Concept> | |
40 | class binding; | |
41 | ||
42 | namespace detail { | |
43 | ||
44 | template<class P, class T, class Table> | |
45 | bool maybe_check_table(const T& arg, const Table*& table, boost::mpl::true_) | |
46 | { | |
47 | if(table == 0) { | |
48 | table = &::boost::type_erasure::detail::access::table(arg); | |
49 | return true; | |
50 | } else { | |
51 | return table->template find< ::boost::type_erasure::typeid_<P> >()() == | |
52 | ::boost::type_erasure::detail::access::table(arg). | |
53 | template find< ::boost::type_erasure::typeid_<P> >()(); | |
54 | } | |
55 | } | |
56 | ||
57 | template<class P, class T, class Table> | |
58 | bool maybe_check_table(const T&, const Table*&, boost::mpl::false_) | |
59 | { | |
60 | return true; | |
61 | } | |
62 | ||
63 | template<class Concept, class T> | |
64 | struct should_check : | |
65 | boost::mpl::and_< | |
66 | ::boost::type_erasure::is_placeholder<T>, | |
67 | ::boost::type_erasure::is_relaxed<Concept> | |
68 | > | |
69 | {}; | |
70 | ||
71 | } | |
72 | ||
73 | #ifdef BOOST_TYPE_ERASURE_DOXYGEN | |
74 | ||
75 | /** | |
76 | * If @ref relaxed is in @c Concept, checks whether the | |
77 | * arguments to @c f match the types specified by | |
78 | * @c binding. If @ref relaxed is not in @c Concept, | |
79 | * returns true. If @c binding is not specified, it will | |
80 | * be deduced from the arguments. | |
81 | */ | |
82 | template<class Concept, class Op, class... U> | |
83 | bool check_match(const binding<Concept>& binding_arg, const Op& f, U&&... args); | |
84 | ||
85 | /** | |
86 | * \overload | |
87 | */ | |
88 | template<class Op, class... U> | |
89 | bool check_match(const Op& f, U&&... args); | |
90 | ||
91 | #else | |
92 | ||
93 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
94 | ||
95 | namespace detail { | |
96 | ||
97 | template<class Concept> | |
98 | bool check_table(const ::boost::type_erasure::binding<Concept>* t, void(*)()) | |
99 | { | |
100 | return true; | |
101 | } | |
102 | ||
103 | template<class Concept, class R, class T0, class... T, class U0, class... U> | |
104 | bool check_table( | |
105 | const ::boost::type_erasure::binding<Concept>* t, | |
106 | R(*)(T0, T...), | |
107 | const U0& arg0, | |
108 | const U&... arg) | |
109 | { | |
110 | typedef typename ::boost::remove_cv< | |
111 | typename ::boost::remove_reference<T0>::type | |
112 | >::type t0; | |
113 | if(!::boost::type_erasure::detail::maybe_check_table<t0>( | |
114 | arg0, | |
115 | t, | |
116 | ::boost::type_erasure::detail::should_check<Concept, t0>())) | |
117 | return false; | |
118 | ||
119 | return check_table(t, static_cast<void(*)(T...)>(0), arg...); | |
120 | } | |
121 | ||
122 | } | |
123 | ||
124 | template<class Concept, class Op, class... U> | |
125 | bool check_match( | |
126 | const ::boost::type_erasure::binding<Concept>& table, | |
127 | const Op&, | |
128 | U&&... arg) | |
129 | { | |
130 | ||
131 | return ::boost::type_erasure::detail::check_table( | |
132 | &table, | |
133 | static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), | |
134 | arg...); | |
135 | } | |
136 | ||
137 | template< | |
138 | class Op, | |
139 | class... U | |
140 | > | |
141 | bool check_match( | |
142 | const Op&, | |
143 | U&&... arg) | |
144 | { | |
145 | const ::boost::type_erasure::binding< | |
146 | typename ::boost::type_erasure::detail::extract_concept< | |
147 | typename ::boost::type_erasure::detail::get_signature<Op>::type, U...>::type>* p = 0; | |
148 | ||
149 | return ::boost::type_erasure::detail::check_table( | |
150 | p, static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), arg...); | |
151 | } | |
152 | ||
153 | #else | |
154 | ||
155 | #define BOOST_PP_FILENAME_1 <boost/type_erasure/check_match.hpp> | |
156 | #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY) | |
157 | #include BOOST_PP_ITERATE() | |
158 | ||
159 | #endif | |
160 | ||
161 | #endif | |
162 | ||
163 | } | |
164 | } | |
165 | ||
166 | #endif | |
167 | ||
168 | #else | |
169 | ||
170 | namespace detail { | |
171 | ||
172 | #define N BOOST_PP_ITERATION() | |
173 | ||
174 | #define BOOST_TYPE_ERASURE_CHECK_TABLE(z, n, data) \ | |
175 | typedef typename ::boost::remove_cv< \ | |
176 | typename ::boost::remove_reference<BOOST_PP_CAT(T, n)>::type \ | |
177 | >::type BOOST_PP_CAT(t, n); \ | |
178 | if(!::boost::type_erasure::detail::maybe_check_table<BOOST_PP_CAT(t, n)>( \ | |
179 | BOOST_PP_CAT(arg, n), \ | |
180 | t, \ | |
181 | ::boost::type_erasure::detail::should_check<Concept, BOOST_PP_CAT(t, n)>())) \ | |
182 | return false; | |
183 | ||
184 | template< | |
185 | class Concept, | |
186 | class R | |
187 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class T) | |
188 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)> | |
189 | bool | |
190 | BOOST_PP_CAT(check_table, N)( | |
191 | const ::boost::type_erasure::binding<Concept>* t, | |
192 | R (*)(BOOST_PP_ENUM_PARAMS(N, T)) | |
193 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, const U, &arg)) | |
194 | { | |
195 | BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_CHECK_TABLE, ~) | |
196 | return true; | |
197 | } | |
198 | ||
199 | #if N != 0 | |
200 | ||
201 | template<class Sig BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)> | |
202 | struct BOOST_PP_CAT(do_extract_concept, N); | |
203 | ||
204 | template< | |
205 | class R | |
206 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class T) | |
207 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
208 | > | |
209 | struct BOOST_PP_CAT(do_extract_concept, N)< | |
210 | R(BOOST_PP_ENUM_PARAMS(N, T)) | |
211 | BOOST_PP_ENUM_TRAILING_PARAMS(N, U) | |
212 | > | |
213 | : ::boost::type_erasure::detail::BOOST_PP_CAT(extract_concept, N)< | |
214 | BOOST_PP_ENUM_PARAMS(N, T) | |
215 | BOOST_PP_ENUM_TRAILING_PARAMS(N, U)> | |
216 | {}; | |
217 | ||
218 | #endif | |
219 | ||
220 | } | |
221 | ||
222 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
223 | #define RREF & | |
224 | #else | |
225 | #define RREF && | |
226 | #endif | |
227 | ||
228 | template< | |
229 | class Concept, | |
230 | class Op | |
231 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
232 | > | |
233 | bool check_match( | |
234 | const ::boost::type_erasure::binding<Concept>& table, | |
235 | const Op& | |
236 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
237 | { | |
238 | ||
239 | return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)( | |
240 | &table, | |
241 | (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0 | |
242 | BOOST_PP_ENUM_TRAILING_PARAMS(N, arg)); | |
243 | } | |
244 | ||
245 | #if N != 0 | |
246 | ||
247 | template< | |
248 | class Op | |
249 | BOOST_PP_ENUM_TRAILING_PARAMS(N, class U) | |
250 | > | |
251 | bool check_match( | |
252 | const Op& | |
253 | BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg)) | |
254 | { | |
255 | const ::boost::type_erasure::binding< | |
256 | typename ::boost::type_erasure::detail::BOOST_PP_CAT(do_extract_concept, N)< | |
257 | typename ::boost::type_erasure::detail::get_signature<Op>::type, | |
258 | BOOST_PP_ENUM_PARAMS(N, U)>::type>* p = 0; | |
259 | ||
260 | return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)( | |
261 | p, | |
262 | (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0 | |
263 | BOOST_PP_ENUM_TRAILING_PARAMS(N, arg)); | |
264 | } | |
265 | ||
266 | #undef RREF | |
267 | #undef BOOST_TYPE_ERASURE_CHECK_TABLE | |
268 | #undef N | |
269 | ||
270 | #endif | |
271 | ||
272 | #endif |