bind_tests_advanced.cpp 12 KB

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