blob: a6a9a7cf04fbae1bef9b7df6b8fffdaaa21b24f4 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// Boost Lambda Library - lambda_functors.hpp -------------------------------
2
3// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
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// For more information, see http://www.boost.org
10
11// ------------------------------------------------
12
13#ifndef BOOST_LAMBDA_LAMBDA_FUNCTORS_HPP
14#define BOOST_LAMBDA_LAMBDA_FUNCTORS_HPP
15
16#include <ndnboost/config.hpp>
17#include <ndnboost/detail/workaround.hpp>
18#include <ndnboost/utility/result_of.hpp>
19
20#if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
21
22#include <ndnboost/mpl/or.hpp>
23#include <ndnboost/utility/enable_if.hpp>
24#include <ndnboost/type_traits/is_array.hpp>
25
26#define BOOST_LAMBDA_DISABLE_IF_ARRAY1(A1, R1)\
27 typename lazy_disable_if<is_array<A1>, typename R1 >::type
28#define BOOST_LAMBDA_DISABLE_IF_ARRAY2(A1, A2, R1, R2) \
29 typename lazy_disable_if<mpl::or_<is_array<A1>, is_array<A2> >, typename R1, R2 >::type
30#define BOOST_LAMBDA_DISABLE_IF_ARRAY3(A1, A2, A3, R1, R2, R3) \
31 typename lazy_disable_if<mpl::or_<is_array<A1>, is_array<A2>, is_array<A3> >, typename R1, R2, R3 >::type
32
33#else
34
35#define BOOST_LAMBDA_DISABLE_IF_ARRAY1(A1, R1) typename R1::type
36#define BOOST_LAMBDA_DISABLE_IF_ARRAY2(A1, A2, R1, R2) typename R1, R2::type
37#define BOOST_LAMBDA_DISABLE_IF_ARRAY3(A1, A2, A3, R1, R2, R3) typename R1, R2, R3::type
38
39#endif
40
41namespace ndnboost {
42namespace lambda {
43
44// -- lambda_functor --------------------------------------------
45// --------------------------------------------------------------
46
47//inline const null_type const_null_type() { return null_type(); }
48
49namespace detail {
50namespace {
51
52 static const null_type constant_null_type = null_type();
53
54} // unnamed
55} // detail
56
57class unused {};
58
59#define cnull_type() detail::constant_null_type
60
61// -- free variables types --------------------------------------------------
62
63 // helper to work around the case where the nullary return type deduction
64 // is always performed, even though the functor is not nullary
65namespace detail {
66 template<int N, class Tuple> struct get_element_or_null_type {
67 typedef typename
68 detail::tuple_element_as_reference<N, Tuple>::type type;
69 };
70 template<int N> struct get_element_or_null_type<N, null_type> {
71 typedef null_type type;
72 };
73}
74
75template <int I> struct placeholder;
76
77template<> struct placeholder<FIRST> {
78
79 template<class SigArgs> struct sig {
80 typedef typename detail::get_element_or_null_type<0, SigArgs>::type type;
81 };
82
83 template<class RET, CALL_TEMPLATE_ARGS>
84 RET call(CALL_FORMAL_ARGS) const {
85 BOOST_STATIC_ASSERT(ndnboost::is_reference<RET>::value);
86 CALL_USE_ARGS; // does nothing, prevents warnings for unused args
87 return a;
88 }
89};
90
91template<> struct placeholder<SECOND> {
92
93 template<class SigArgs> struct sig {
94 typedef typename detail::get_element_or_null_type<1, SigArgs>::type type;
95 };
96
97 template<class RET, CALL_TEMPLATE_ARGS>
98 RET call(CALL_FORMAL_ARGS) const { CALL_USE_ARGS; return b; }
99};
100
101template<> struct placeholder<THIRD> {
102
103 template<class SigArgs> struct sig {
104 typedef typename detail::get_element_or_null_type<2, SigArgs>::type type;
105 };
106
107 template<class RET, CALL_TEMPLATE_ARGS>
108 RET call(CALL_FORMAL_ARGS) const { CALL_USE_ARGS; return c; }
109};
110
111template<> struct placeholder<EXCEPTION> {
112
113 template<class SigArgs> struct sig {
114 typedef typename detail::get_element_or_null_type<3, SigArgs>::type type;
115 };
116
117 template<class RET, CALL_TEMPLATE_ARGS>
118 RET call(CALL_FORMAL_ARGS) const { CALL_USE_ARGS; return env; }
119};
120
121typedef const lambda_functor<placeholder<FIRST> > placeholder1_type;
122typedef const lambda_functor<placeholder<SECOND> > placeholder2_type;
123typedef const lambda_functor<placeholder<THIRD> > placeholder3_type;
124
125
126///////////////////////////////////////////////////////////////////////////////
127
128
129// free variables are lambda_functors. This is to allow uniform handling with
130// other lambda_functors.
131// -------------------------------------------------------------------
132
133#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
134#pragma warning(push)
135#pragma warning(disable:4512) //assignment operator could not be generated
136#endif
137
138// -- lambda_functor NONE ------------------------------------------------
139template <class T>
140class lambda_functor : public T
141{
142
143BOOST_STATIC_CONSTANT(int, arity_bits = get_arity<T>::value);
144
145public:
146 typedef T inherited;
147
148 lambda_functor() {}
149 lambda_functor(const lambda_functor& l) : inherited(l) {}
150
151 lambda_functor(const T& t) : inherited(t) {}
152
153 template <class SigArgs> struct sig {
154 typedef typename inherited::template
155 sig<typename SigArgs::tail_type>::type type;
156 };
157
158 // Note that this return type deduction template is instantiated, even
159 // if the nullary
160 // operator() is not called at all. One must make sure that it does not fail.
161 typedef typename
162 inherited::template sig<null_type>::type
163 nullary_return_type;
164
165 // Support for ndnboost::result_of.
166 template <class Sig> struct result;
167 template <class F>
168 struct result<F()> {
169 typedef nullary_return_type type;
170 };
171 template <class F, class A>
172 struct result<F(A)> {
173 typedef typename sig<tuple<F, A> >::type type;
174 };
175 template <class F, class A, class B>
176 struct result<F(A, B)> {
177 typedef typename sig<tuple<F, A, B> >::type type;
178 };
179 template <class F, class A, class B, class C>
180 struct result<F(A, B, C)> {
181 typedef typename sig<tuple<F, A, B, C> >::type type;
182 };
183
184 nullary_return_type operator()() const {
185 return inherited::template
186 call<nullary_return_type>
187 (cnull_type(), cnull_type(), cnull_type(), cnull_type());
188 }
189
190 template<class A>
191 typename inherited::template sig<tuple<A&> >::type
192 operator()(A& a) const {
193 return inherited::template call<
194 typename inherited::template sig<tuple<A&> >::type
195 >(a, cnull_type(), cnull_type(), cnull_type());
196 }
197
198 template<class A>
199 BOOST_LAMBDA_DISABLE_IF_ARRAY1(A, inherited::template sig<tuple<A const&> >)
200 operator()(A const& a) const {
201 return inherited::template call<
202 typename inherited::template sig<tuple<A const&> >::type
203 >(a, cnull_type(), cnull_type(), cnull_type());
204 }
205
206 template<class A, class B>
207 typename inherited::template sig<tuple<A&, B&> >::type
208 operator()(A& a, B& b) const {
209 return inherited::template call<
210 typename inherited::template sig<tuple<A&, B&> >::type
211 >(a, b, cnull_type(), cnull_type());
212 }
213
214 template<class A, class B>
215 BOOST_LAMBDA_DISABLE_IF_ARRAY2(A, B, inherited::template sig<tuple<A const&, B&> >)
216 operator()(A const& a, B& b) const {
217 return inherited::template call<
218 typename inherited::template sig<tuple<A const&, B&> >::type
219 >(a, b, cnull_type(), cnull_type());
220 }
221
222 template<class A, class B>
223 BOOST_LAMBDA_DISABLE_IF_ARRAY2(A, B, inherited::template sig<tuple<A&, B const&> >)
224 operator()(A& a, B const& b) const {
225 return inherited::template call<
226 typename inherited::template sig<tuple<A&, B const&> >::type
227 >(a, b, cnull_type(), cnull_type());
228 }
229
230 template<class A, class B>
231 BOOST_LAMBDA_DISABLE_IF_ARRAY2(A, B, inherited::template sig<tuple<A const&, B const&> >)
232 operator()(A const& a, B const& b) const {
233 return inherited::template call<
234 typename inherited::template sig<tuple<A const&, B const&> >::type
235 >(a, b, cnull_type(), cnull_type());
236 }
237
238 template<class A, class B, class C>
239 typename inherited::template sig<tuple<A&, B&, C&> >::type
240 operator()(A& a, B& b, C& c) const
241 {
242 return inherited::template call<
243 typename inherited::template sig<tuple<A&, B&, C&> >::type
244 >(a, b, c, cnull_type());
245 }
246
247 template<class A, class B, class C>
248 BOOST_LAMBDA_DISABLE_IF_ARRAY3(A, B, C, inherited::template sig<tuple<A const&, B const&, C const&> >)
249 operator()(A const& a, B const& b, C const& c) const
250 {
251 return inherited::template call<
252 typename inherited::template sig<tuple<A const&, B const&, C const&> >::type
253 >(a, b, c, cnull_type());
254 }
255
256 // for internal calls with env
257 template<CALL_TEMPLATE_ARGS>
258 typename inherited::template sig<tuple<CALL_REFERENCE_TYPES> >::type
259 internal_call(CALL_FORMAL_ARGS) const {
260 return inherited::template
261 call<typename inherited::template
262 sig<tuple<CALL_REFERENCE_TYPES> >::type>(CALL_ACTUAL_ARGS);
263 }
264
265 template<class A>
266 const lambda_functor<lambda_functor_base<
267 other_action<assignment_action>,
268 ndnboost::tuple<lambda_functor,
269 typename const_copy_argument <const A>::type> > >
270 operator=(const A& a) const {
271 return lambda_functor_base<
272 other_action<assignment_action>,
273 ndnboost::tuple<lambda_functor,
274 typename const_copy_argument <const A>::type> >
275 ( ndnboost::tuple<lambda_functor,
276 typename const_copy_argument <const A>::type>(*this, a) );
277 }
278
279 template<class A>
280 const lambda_functor<lambda_functor_base<
281 other_action<subscript_action>,
282 ndnboost::tuple<lambda_functor,
283 typename const_copy_argument <const A>::type> > >
284 operator[](const A& a) const {
285 return lambda_functor_base<
286 other_action<subscript_action>,
287 ndnboost::tuple<lambda_functor,
288 typename const_copy_argument <const A>::type> >
289 ( ndnboost::tuple<lambda_functor,
290 typename const_copy_argument <const A>::type>(*this, a ) );
291 }
292};
293
294#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
295#pragma warning(pop)
296#endif
297
298} // namespace lambda
299} // namespace ndnboost
300
301namespace ndnboost {
302
303#if !defined(BOOST_RESULT_OF_USE_DECLTYPE) || defined(BOOST_NO_DECLTYPE)
304
305template<class T>
306struct result_of<ndnboost::lambda::lambda_functor<T>()>
307{
308 typedef typename ndnboost::lambda::lambda_functor<T>::nullary_return_type type;
309};
310
311template<class T>
312struct result_of<const ndnboost::lambda::lambda_functor<T>()>
313{
314 typedef typename ndnboost::lambda::lambda_functor<T>::nullary_return_type type;
315};
316
317#endif
318
319template<class T>
320struct tr1_result_of<ndnboost::lambda::lambda_functor<T>()>
321{
322 typedef typename ndnboost::lambda::lambda_functor<T>::nullary_return_type type;
323};
324
325template<class T>
326struct tr1_result_of<const ndnboost::lambda::lambda_functor<T>()>
327{
328 typedef typename ndnboost::lambda::lambda_functor<T>::nullary_return_type type;
329};
330
331}
332
333// is_placeholder
334
335#include <ndnboost/is_placeholder.hpp>
336
337namespace ndnboost
338{
339
340template<> struct is_placeholder< lambda::lambda_functor< lambda::placeholder<lambda::FIRST> > >
341{
342 enum _vt { value = 1 };
343};
344
345template<> struct is_placeholder< lambda::lambda_functor< lambda::placeholder<lambda::SECOND> > >
346{
347 enum _vt { value = 2 };
348};
349
350template<> struct is_placeholder< lambda::lambda_functor< lambda::placeholder<lambda::THIRD> > >
351{
352 enum _vt { value = 3 };
353};
354
355} // namespace ndnboost
356
357#endif