bind_tests_advanced.cpp 12 KB

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