binders_tests.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*=============================================================================
  2. Phoenix V1.2.1
  3. Copyright (c) 2001-2003 Joel de Guzman
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <iostream>
  9. #include <functional>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #define PHOENIX_LIMIT 15
  12. #include <boost/spirit/include/phoenix1_primitives.hpp>
  13. #include <boost/spirit/include/phoenix1_composite.hpp>
  14. #include <boost/spirit/include/phoenix1_binders.hpp>
  15. using namespace phoenix;
  16. using std::cout;
  17. using std::endl;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. struct print_ { // a typical STL style monomorphic functor
  20. typedef void result_type;
  21. void operator()() { cout << "got no args\n"; }
  22. void operator()(int n0) { cout << "got 1 arg " << n0 << " \n"; }
  23. void operator()(int n0, int n1) { cout << "got 2 args " << n0 << ", " << n1 << " \n"; }
  24. void foo0() const { cout << "print_::foo0\n"; }
  25. void foo1(int n0) { cout << "print_::foo1 " << n0 << " \n"; }
  26. void foo2(int n0, int n1) { cout << "print_::foo2 " << n0 << ", " << n1 << " \n"; }
  27. int x;
  28. };
  29. functor<print_> print = print_();
  30. member_function_ptr<void, print_, int> print_foo1 = &print_::foo1;
  31. member_function_ptr<void, print_, int, int> print_foo2 = &print_::foo2;
  32. member_var_ptr<int, print_> print_x = &print_::x;
  33. print_ p;
  34. bound_member<void, print_, int> bound_print_foo1(p,&print_::foo1);
  35. bound_member<void, print_, int, int> bound_print_foo2(&p,&print_::foo2);
  36. ///////////////////////////////////////////////////////////////////////////////
  37. void foo0() // a function w/ 0 args
  38. { cout << "foo0\n"; }
  39. void foo1(int n0) // a function w/ 1 arg
  40. { cout << "foo1 " << n0 << " \n"; }
  41. void foo2(int n0, int n1) // a function w/ 2 args
  42. { cout << "foo2 " << n0 << ", " << n1 << " \n"; }
  43. void foo3_(int n0, int n1, int n2) // a function w/ 3 args
  44. { cout << "foo3 " << n0 << ", " << n1 << ", " << n2 << " \n"; }
  45. function_ptr<void, int, int, int> foo3 = &foo3_;
  46. ///////////////////////////////////////////////////////////////////////////////
  47. int
  48. main()
  49. {
  50. int i50 = 50, i20 = 20, i100 = 100;
  51. ///////////////////////////////////////////////////////////////////////////////
  52. //
  53. // Binders
  54. //
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // Functor binders
  57. print()();
  58. print(111)();
  59. print(111, arg1)(i100);
  60. print(111, 222)();
  61. cout << bind(std::negate<int>())(arg1)(i20) << endl;
  62. cout << bind(std::plus<int>())(arg1, arg2)(i20, i50) << endl;
  63. // Function binders
  64. bind(&foo0)()();
  65. bind(&foo1)(111)();
  66. bind(&foo2)(111, arg1)(i100);
  67. bind(&foo2)(111, 222)();
  68. foo3(111, 222, 333)();
  69. foo3(arg1, arg2, arg3)(i20, i50, i100);
  70. foo3(111, arg1, arg2)(i50, i100);
  71. // Member function binders
  72. print_ printer;
  73. bind(&print_::foo0)(arg1)(printer);
  74. bind(&print_::foo1)(arg1, 111)(printer);
  75. print_foo1(arg1, 111)(printer);
  76. print_foo1(var(printer), 111)();
  77. print_foo2(var(printer), 111, 222)();
  78. print_foo2(var(printer), 111, arg1)(i100);
  79. // Member var binders
  80. printer.x = 3;
  81. BOOST_TEST(bind(&print_::x)(arg1)(printer) == 3);
  82. BOOST_TEST(print_x(arg1)(printer) == 3);
  83. BOOST_TEST(print_x(printer)() == 3);
  84. BOOST_TEST(0 != (print_x(var(printer))() = 4));
  85. BOOST_TEST(printer.x == 4);
  86. // Bound member functions
  87. bind(&printer,&print_::foo0)()();
  88. bind(printer,&print_::foo1)(111)();
  89. bound_print_foo1(111)();
  90. bound_print_foo1(111)();
  91. bound_print_foo2(111, 222)();
  92. bound_print_foo2(111, arg1)(i100);
  93. return boost::report_errors();
  94. }