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