]>
Commit | Line | Data |
---|---|---|
1 | // bind_tests_advanced.cpp -- The Boost Lambda Library ------------------ | |
2 | // | |
3 | // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) | |
4 | // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com) | |
5 | // Copyright (C) 2010 Steven Watanabe | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See | |
8 | // accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | // For more information, see www.boost.org | |
12 | ||
13 | // ----------------------------------------------------------------------- | |
14 | ||
15 | ||
16 | #include <boost/core/lightweight_test.hpp> | |
17 | #define BOOST_CHECK BOOST_TEST | |
18 | ||
19 | #include "boost/lambda/lambda.hpp" | |
20 | #include "boost/lambda/bind.hpp" | |
21 | ||
22 | ||
23 | #include "boost/any.hpp" | |
24 | #include "boost/type_traits/is_reference.hpp" | |
25 | #include "boost/mpl/assert.hpp" | |
26 | #include "boost/mpl/if.hpp" | |
27 | ||
28 | #include <iostream> | |
29 | ||
30 | #include <functional> | |
31 | ||
32 | #include <algorithm> | |
33 | ||
34 | ||
35 | using namespace boost::lambda; | |
36 | namespace bl = boost::lambda; | |
37 | ||
38 | int sum_0() { return 0; } | |
39 | int sum_1(int a) { return a; } | |
40 | int sum_2(int a, int b) { return a+b; } | |
41 | ||
42 | int product_2(int a, int b) { return a*b; } | |
43 | ||
44 | // unary function that returns a pointer to a binary function | |
45 | typedef int (*fptr_type)(int, int); | |
46 | fptr_type sum_or_product(bool x) { | |
47 | return x ? sum_2 : product_2; | |
48 | } | |
49 | ||
50 | // a nullary functor that returns a pointer to a unary function that | |
51 | // returns a pointer to a binary function. | |
52 | struct which_one { | |
53 | typedef fptr_type (*result_type)(bool x); | |
54 | template <class T> struct sig { typedef result_type type; }; | |
55 | ||
56 | result_type operator()() const { return sum_or_product; } | |
57 | }; | |
58 | ||
59 | void test_nested_binds() | |
60 | { | |
61 | int j = 2; int k = 3; | |
62 | ||
63 | // bind calls can be nested (the target function can be a lambda functor) | |
64 | // The interpretation is, that the innermost lambda functor returns something | |
65 | // that is bindable (another lambda functor, function pointer ...) | |
66 | bool condition; | |
67 | ||
68 | condition = true; | |
69 | BOOST_CHECK(bind(bind(&sum_or_product, _1), 1, 2)(condition)==3); | |
70 | BOOST_CHECK(bind(bind(&sum_or_product, _1), _2, _3)(condition, j, k)==5); | |
71 | ||
72 | condition = false; | |
73 | BOOST_CHECK(bind(bind(&sum_or_product, _1), 1, 2)(condition)==2); | |
74 | BOOST_CHECK(bind(bind(&sum_or_product, _1), _2, _3)(condition, j, k)==6); | |
75 | ||
76 | ||
77 | which_one wo; | |
78 | BOOST_CHECK(bind(bind(bind(wo), _1), _2, _3)(condition, j, k)==6); | |
79 | ||
80 | ||
81 | return; | |
82 | } | |
83 | ||
84 | ||
85 | // unlambda ------------------------------------------------- | |
86 | ||
87 | // Sometimes it may be necessary to prevent the argument substitution of | |
88 | // taking place. For example, we may end up with a nested bind expression | |
89 | // inadvertently when using the target function is received as a parameter | |
90 | ||
91 | template<class F> | |
92 | int call_with_100(const F& f) { | |
93 | ||
94 | ||
95 | ||
96 | // bind(f, _1)(make_const(100)); | |
97 | // This would result in; | |
98 | // bind(_1 + 1, _1)(make_const(100)) , which would be a compile time error | |
99 | ||
100 | return bl::bind(unlambda(f), _1)(make_const(100)); | |
101 | ||
102 | // for other functors than lambda functors, unlambda has no effect | |
103 | // (except for making them const) | |
104 | } | |
105 | ||
106 | template<class F> | |
107 | int call_with_101(const F& f) { | |
108 | ||
109 | return bind(unlambda(f), _1)(make_const(101)); | |
110 | ||
111 | } | |
112 | ||
113 | ||
114 | void test_unlambda() { | |
115 | ||
116 | int i = 1; | |
117 | ||
118 | BOOST_CHECK(unlambda(_1 + _2)(i, i) == 2); | |
119 | BOOST_CHECK(unlambda(++var(i))() == 2); | |
120 | BOOST_CHECK(call_with_100(_1 + 1) == 101); | |
121 | ||
122 | ||
123 | BOOST_CHECK(call_with_101(_1 + 1) == 102); | |
124 | ||
125 | #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) | |
126 | ||
127 | BOOST_CHECK(call_with_100(bl::bind(std_functor(std::bind1st(std::plus<int>(), 1)), _1)) == 101); | |
128 | ||
129 | #elif BOOST_CXX_VERSION > 201703L | |
130 | ||
131 | // In C++20, standard functors no longer have ::result_type | |
132 | BOOST_CHECK(call_with_100(bl::bind(std::bind(std::plus<int>(), 1, std::placeholders::_1), _1)) == 101); | |
133 | ||
134 | #elif defined(BOOST_MSVC) && BOOST_MSVC < 1900 | |
135 | ||
136 | // Mysterious failures under msvc-12.0 and below | |
137 | ||
138 | #else | |
139 | ||
140 | BOOST_CHECK(call_with_100(bl::bind(std_functor(std::bind(std::plus<int>(), 1, std::placeholders::_1)), _1)) == 101); | |
141 | ||
142 | #endif | |
143 | ||
144 | #if BOOST_CXX_VERSION <= 201703L | |
145 | ||
146 | // std_functor insturcts LL that the functor defines a result_type typedef | |
147 | // rather than a sig template. | |
148 | bl::bind(std_functor(std::plus<int>()), _1, _2)(i, i); | |
149 | ||
150 | #else | |
151 | ||
152 | // In C++20, standard functors no longer have ::result_type | |
153 | bl::bind(std::plus<int>(), _1, _2)(i, i); | |
154 | ||
155 | #endif | |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | ||
161 | // protect ------------------------------------------------------------ | |
162 | ||
163 | // protect protects a lambda functor from argument substitution. | |
164 | // protect is useful e.g. with nested stl algorithm calls. | |
165 | ||
166 | namespace ll { | |
167 | ||
168 | struct for_each { | |
169 | ||
170 | // note, std::for_each returns it's last argument | |
171 | // We want the same behaviour from our ll::for_each. | |
172 | // However, the functor can be called with any arguments, and | |
173 | // the return type thus depends on the argument types. | |
174 | ||
175 | // 1. Provide a sig class member template: | |
176 | ||
177 | // The return type deduction system instantiate this class as: | |
178 | // sig<Args>::type, where Args is a boost::tuples::cons-list | |
179 | // The head type is the function object type itself | |
180 | // cv-qualified (so it is possilbe to provide different return types | |
181 | // for differently cv-qualified operator()'s. | |
182 | ||
183 | // The tail type is the list of the types of the actual arguments the | |
184 | // function was called with. | |
185 | // So sig should contain a typedef type, which defines a mapping from | |
186 | // the operator() arguments to its return type. | |
187 | // Note, that it is possible to provide different sigs for the same functor | |
188 | // if the functor has several operator()'s, even if they have different | |
189 | // number of arguments. | |
190 | ||
191 | // Note, that the argument types in Args are guaranteed to be non-reference | |
192 | // types, but they can have cv-qualifiers. | |
193 | ||
194 | template <class Args> | |
195 | struct sig { | |
196 | typedef typename boost::remove_const< | |
197 | typename boost::tuples::element<3, Args>::type | |
198 | >::type type; | |
199 | }; | |
200 | ||
201 | template <class A, class B, class C> | |
202 | C | |
203 | operator()(const A& a, const B& b, const C& c) const | |
204 | { return std::for_each(a, b, c);} | |
205 | }; | |
206 | ||
207 | } // end of ll namespace | |
208 | ||
209 | void test_protect() | |
210 | { | |
211 | int i = 0; | |
212 | int b[3][5]; | |
213 | int* a[3]; | |
214 | ||
215 | for(int j=0; j<3; ++j) a[j] = b[j]; | |
216 | ||
217 | std::for_each(a, a+3, | |
218 | bind(ll::for_each(), _1, _1 + 5, protect(_1 = ++var(i)))); | |
219 | ||
220 | // This is how you could output the values (it is uncommented, no output | |
221 | // from a regression test file): | |
222 | // std::for_each(a, a+3, | |
223 | // bind(ll::for_each(), _1, _1 + 5, | |
224 | // std::cout << constant("\nLine ") << (&_1 - a) << " : " | |
225 | // << protect(_1) | |
226 | // ) | |
227 | // ); | |
228 | ||
229 | int sum = 0; | |
230 | ||
231 | std::for_each(a, a+3, | |
232 | bind(ll::for_each(), _1, _1 + 5, | |
233 | protect(sum += _1)) | |
234 | ); | |
235 | BOOST_CHECK(sum == (1+15)*15/2); | |
236 | ||
237 | sum = 0; | |
238 | ||
239 | std::for_each(a, a+3, | |
240 | bind(ll::for_each(), _1, _1 + 5, | |
241 | sum += 1 + protect(_1)) // add element count | |
242 | ); | |
243 | BOOST_CHECK(sum == (1+15)*15/2 + 15); | |
244 | ||
245 | (1 + protect(_1))(sum); | |
246 | ||
247 | int k = 0; | |
248 | ((k += constant(1)) += protect(constant(2)))(); | |
249 | BOOST_CHECK(k==1); | |
250 | ||
251 | k = 0; | |
252 | ((k += constant(1)) += protect(constant(2)))()(); | |
253 | BOOST_CHECK(k==3); | |
254 | ||
255 | // note, the following doesn't work: | |
256 | ||
257 | // ((var(k) = constant(1)) = protect(constant(2)))(); | |
258 | ||
259 | // (var(k) = constant(1))() returns int& and thus the | |
260 | // second assignment fails. | |
261 | ||
262 | // We should have something like: | |
263 | // bind(var, var(k) = constant(1)) = protect(constant(2)))(); | |
264 | // But currently var is not bindable. | |
265 | ||
266 | // The same goes with ret. A bindable ret could be handy sometimes as well | |
267 | // (protect(std::cout << _1), std::cout << _1)(i)(j); does not work | |
268 | // because the comma operator tries to store the result of the evaluation | |
269 | // of std::cout << _1 as a copy (and you can't copy std::ostream). | |
270 | // something like this: | |
271 | // (protect(std::cout << _1), bind(ref, std::cout << _1))(i)(j); | |
272 | ||
273 | ||
274 | // the stuff below works, but we do not want extra output to | |
275 | // cout, must be changed to stringstreams but stringstreams do not | |
276 | // work due to a bug in the type deduction. Will be fixed... | |
277 | #if 0 | |
278 | // But for now, ref is not bindable. There are other ways around this: | |
279 | ||
280 | int x = 1, y = 2; | |
281 | (protect(std::cout << _1), (std::cout << _1, 0))(x)(y); | |
282 | ||
283 | // added one dummy value to make the argument to comma an int | |
284 | // instead of ostream& | |
285 | ||
286 | // Note, the same problem is more apparent without protect | |
287 | // (std::cout << 1, std::cout << constant(2))(); // does not work | |
288 | ||
289 | (boost::ref(std::cout << 1), std::cout << constant(2))(); // this does | |
290 | ||
291 | #endif | |
292 | ||
293 | } | |
294 | ||
295 | ||
296 | void test_lambda_functors_as_arguments_to_lambda_functors() { | |
297 | ||
298 | // lambda functor is a function object, and can therefore be used | |
299 | // as an argument to another lambda functors function call object. | |
300 | ||
301 | // Note however, that the argument/type substitution is not entered again. | |
302 | // This means, that something like this will not work: | |
303 | ||
304 | (_1 + _2)(_1, make_const(7)); | |
305 | (_1 + _2)(bind(&sum_0), make_const(7)); | |
306 | ||
307 | // or it does work, but the effect is not to call | |
308 | // sum_0() + 7, but rather | |
309 | // bind(sum_0) + 7, which results in another lambda functor | |
310 | // (lambda functor + int) and can be called again | |
311 | BOOST_CHECK((_1 + _2)(bind(&sum_0), make_const(7))() == 7); | |
312 | ||
313 | int i = 3, j = 12; | |
314 | BOOST_CHECK((_1 - _2)(_2, _1)(i, j) == j - i); | |
315 | ||
316 | // also, note that lambda functor are no special case for bind if received | |
317 | // as a parameter. In oder to be bindable, the functor must | |
318 | // defint the sig template, or then | |
319 | // the return type must be defined within the bind call. Lambda functors | |
320 | // do define the sig template, so if the return type deduction system | |
321 | // covers the case, there is no need to specify the return type | |
322 | // explicitly. | |
323 | ||
324 | int a = 5, b = 6; | |
325 | ||
326 | // Let type deduction find out the return type | |
327 | BOOST_CHECK(bind(_1, _2, _3)(unlambda(_1 + _2), a, b) == 11); | |
328 | ||
329 | //specify it yourself: | |
330 | BOOST_CHECK(bind(_1, _2, _3)(ret<int>(_1 + _2), a, b) == 11); | |
331 | BOOST_CHECK(ret<int>(bind(_1, _2, _3))(_1 + _2, a, b) == 11); | |
332 | BOOST_CHECK(bind<int>(_1, _2, _3)(_1 + _2, a, b) == 11); | |
333 | ||
334 | bind(_1,1.0)(_1+_1); | |
335 | return; | |
336 | ||
337 | } | |
338 | ||
339 | ||
340 | void test_const_parameters() { | |
341 | ||
342 | // (_1 + _2)(1, 2); // this would fail, | |
343 | ||
344 | // Either make arguments const: | |
345 | BOOST_CHECK((_1 + _2)(make_const(1), make_const(2)) == 3); | |
346 | ||
347 | // Or use const_parameters: | |
348 | BOOST_CHECK(const_parameters(_1 + _2)(1, 2) == 3); | |
349 | ||
350 | ||
351 | ||
352 | } | |
353 | ||
354 | void test_rvalue_arguments() | |
355 | { | |
356 | // Not quite working yet. | |
357 | // Problems with visual 7.1 | |
358 | // BOOST_CHECK((_1 + _2)(1, 2) == 3); | |
359 | } | |
360 | ||
361 | void test_break_const() | |
362 | { | |
363 | ||
364 | // break_const is currently unnecessary, as LL supports perfect forwarding | |
365 | // for up to there argument lambda functors, and LL does not support | |
366 | // lambda functors with more than 3 args. | |
367 | ||
368 | // I'll keep the test case around anyway, if more arguments will be supported | |
369 | // in the future. | |
370 | ||
371 | ||
372 | ||
373 | // break_const breaks constness! Be careful! | |
374 | // You need this only if you need to have side effects on some argument(s) | |
375 | // and some arguments are non-const rvalues and your lambda functors | |
376 | // take more than 3 arguments. | |
377 | ||
378 | ||
379 | int i = 1; | |
380 | // OLD COMMENT: (_1 += _2)(i, 2) // fails, 2 is a non-const rvalue | |
381 | // OLD COMMENT: const_parameters(_1 += _2)(i, 2) // fails, side-effect to i | |
382 | break_const(_1 += _2)(i, 2); // ok | |
383 | BOOST_CHECK(i == 3); | |
384 | } | |
385 | ||
386 | template<class T> | |
387 | struct func { | |
388 | template<class Args> | |
389 | struct sig { | |
390 | typedef typename boost::tuples::element<1, Args>::type arg1; | |
391 | // If the argument type is not the same as the expected type, | |
392 | // return void, which will cause an error. Note that we | |
393 | // can't just assert that the types are the same, because | |
394 | // both const and non-const versions can be instantiated | |
395 | // even though only one is ultimately used. | |
396 | typedef typename boost::mpl::if_<boost::is_same<arg1, T>, | |
397 | typename boost::remove_const<arg1>::type, | |
398 | void | |
399 | >::type type; | |
400 | }; | |
401 | template<class U> | |
402 | U operator()(const U& arg) const { | |
403 | return arg; | |
404 | } | |
405 | }; | |
406 | ||
407 | void test_sig() | |
408 | { | |
409 | int i = 1; | |
410 | BOOST_CHECK(bind(func<int>(), 1)() == 1); | |
411 | BOOST_CHECK(bind(func<const int>(), _1)(static_cast<const int&>(i)) == 1); | |
412 | BOOST_CHECK(bind(func<int>(), _1)(i) == 1); | |
413 | } | |
414 | ||
415 | class base { | |
416 | public: | |
417 | virtual int foo() = 0; | |
418 | }; | |
419 | ||
420 | class derived : public base { | |
421 | public: | |
422 | virtual int foo() { | |
423 | return 1; | |
424 | } | |
425 | }; | |
426 | ||
427 | void test_abstract() | |
428 | { | |
429 | derived d; | |
430 | base& b = d; | |
431 | BOOST_CHECK(bind(&base::foo, var(b))() == 1); | |
432 | BOOST_CHECK(bind(&base::foo, *_1)(&b) == 1); | |
433 | } | |
434 | ||
435 | int main() { | |
436 | ||
437 | test_nested_binds(); | |
438 | test_unlambda(); | |
439 | test_protect(); | |
440 | test_lambda_functors_as_arguments_to_lambda_functors(); | |
441 | test_const_parameters(); | |
442 | test_rvalue_arguments(); | |
443 | test_break_const(); | |
444 | test_sig(); | |
445 | test_abstract(); | |
446 | return boost::report_errors(); | |
447 | } |