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 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
37 
38 template<class T>
39 struct typeid_;
40 
41 template<class Concept>
42 class binding;
43 
44 #endif
45 
46 namespace detail {
47 
48 template<class P, class T, class Table>
maybe_check_table(const T & arg,const Table * & table,boost::mpl::true_)49 bool maybe_check_table(const T& arg, const Table*& table, boost::mpl::true_)
50 {
51     if(table == 0) {
52         table = &::boost::type_erasure::detail::access::table(arg);
53         return true;
54     } else {
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> >()();
58     }
59 }
60 
61 template<class P, class T, class Table>
maybe_check_table(const T &,const Table * &,boost::mpl::false_)62 bool maybe_check_table(const T&, const Table*&, boost::mpl::false_)
63 {
64     return true;
65 }
66 
67 template<class Concept, class T>
68 struct should_check :
69     boost::mpl::and_<
70         ::boost::type_erasure::is_placeholder<T>,
71         ::boost::type_erasure::is_relaxed<Concept>
72     >
73 {};
74 
75 }
76 
77 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
78 
79 /**
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.
85  */
86 template<class Concept, class Op, class... U>
87 bool check_match(const binding<Concept>& binding_arg, const Op& f, U&&... args);
88 
89 /**
90  * \overload
91  */
92 template<class Op, class... U>
93 bool check_match(const Op& f, U&&... args);
94 
95 #else
96 
97 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
98 
99 namespace detail {
100 
101 template<class Concept, class R>
check_table(const::boost::type_erasure::binding<Concept> *,R (*)())102 bool check_table(const ::boost::type_erasure::binding<Concept>* /*t*/, R(*)())
103 {
104     return true;
105 }
106 
107 template<class Concept, class R, class T0, class... T, class U0, class... U>
check_table(const::boost::type_erasure::binding<Concept> * t,R (*)(T0,T...),const U0 & arg0,const U &...arg)108 bool check_table(
109     const ::boost::type_erasure::binding<Concept>* t,
110     R(*)(T0, T...),
111     const U0& arg0,
112     const U&... arg)
113 {
114     typedef typename ::boost::remove_cv<
115         typename ::boost::remove_reference<T0>::type
116     >::type t0;
117     if(!::boost::type_erasure::detail::maybe_check_table<t0>(
118         arg0,
119         t,
120         ::boost::type_erasure::detail::should_check<Concept, t0>()))
121         return false;
122 
123     return check_table(t, static_cast<void(*)(T...)>(0), arg...);
124 }
125 
126 }
127 
128 template<class Concept, class Op, class... U>
check_match(const::boost::type_erasure::binding<Concept> & table,const Op &,U &&...arg)129 bool check_match(
130     const ::boost::type_erasure::binding<Concept>& table,
131     const Op&,
132     U&&... arg)
133 {
134 
135     return ::boost::type_erasure::detail::check_table(
136         &table,
137         static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0),
138         arg...);
139 }
140 
141 #ifndef BOOST_TYPE_ERASURE_USE_MP11
142 
143 template<
144     class Op,
145     class... U
146 >
check_match(const Op &,U &&...arg)147 bool check_match(
148     const Op&,
149     U&&... arg)
150 {
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;
154 
155     return ::boost::type_erasure::detail::check_table(
156         p, static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), arg...);
157 }
158 
159 #else
160 
161 namespace detail {
162 
163 template<class T>
164 struct get_args;
165 
166 template<class R, class ... T>
167 struct get_args<R(T...)> { typedef ::boost::mp11::mp_list<T...> type; };
168 
169 template<class Sig>
170 using get_args_t = typename get_args<Sig>::type;
171 
172 }
173 
174 template<
175     class Op,
176     class... U
177 >
check_match(const Op &,U &&...arg)178 bool check_match(
179     const Op&,
180     U&&... arg)
181 {
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
186             >,
187             ::boost::mp11::mp_list< ::boost::remove_reference_t<U>...> > >* p = 0;
188 
189     return ::boost::type_erasure::detail::check_table(
190         p, static_cast<typename ::boost::type_erasure::detail::get_signature<Op>::type*>(0), arg...);
191 }
192 
193 #endif
194 
195 #else
196 
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()
200 
201 #endif
202 
203 #endif
204 
205 }
206 }
207 
208 #endif
209 
210 #else
211 
212 namespace detail {
213 
214 #define N BOOST_PP_ITERATION()
215 
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),                                               \
222         t,                                                                  \
223         ::boost::type_erasure::detail::should_check<Concept, BOOST_PP_CAT(t, n)>())) \
224         return false;
225 
226 template<
227     class Concept,
228     class R
229     BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)
230     BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
231 bool
BOOST_PP_CAT(check_table,N)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))
236 {
237     BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_CHECK_TABLE, ~)
238     return true;
239 }
240 
241 #if N != 0
242 
243 template<class Sig BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
244 struct BOOST_PP_CAT(do_extract_concept, N);
245 
246 template<
247     class R
248     BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)
249     BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
250 >
BOOST_PP_CAT(do_extract_concept,N)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)
254 >
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)>
258 {};
259 
260 #endif
261 
262 }
263 
264 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
265 #define RREF &
266 #else
267 #define RREF &&
268 #endif
269 
270 template<
271     class Concept,
272     class Op
273     BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
274 >
check_match(const::boost::type_erasure::binding<Concept> & table,const Op & BOOST_PP_ENUM_TRAILING_BINARY_PARAMS (N,U,RREF arg))275 bool check_match(
276     const ::boost::type_erasure::binding<Concept>& table,
277     const Op&
278     BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
279 {
280 
281     return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)(
282         &table,
283         (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0
284         BOOST_PP_ENUM_TRAILING_PARAMS(N, arg));
285 }
286 
287 #if N != 0
288 
289 template<
290     class Op
291     BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)
292 >
check_match(const Op & BOOST_PP_ENUM_TRAILING_BINARY_PARAMS (N,U,RREF arg))293 bool check_match(
294     const Op&
295     BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, RREF arg))
296 {
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;
301 
302     return ::boost::type_erasure::detail::BOOST_PP_CAT(check_table, N)(
303         p,
304         (typename ::boost::type_erasure::detail::get_signature<Op>::type*)0
305         BOOST_PP_ENUM_TRAILING_PARAMS(N, arg));
306 }
307 
308 #undef RREF
309 #undef BOOST_TYPE_ERASURE_CHECK_TABLE
310 #undef N
311 
312 #endif
313 
314 #endif
315