bind_interoperation_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*==============================================================================
  2. Copyright (c) 2005 Peter Dimov
  3. Copyright (c) 2005-2010 Joel de Guzman
  4. Copyright (c) 2010 Thomas Heller
  5. Copyright (c) 2015 John Fletcher
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ==============================================================================*/
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_MSVC)
  11. #pragma warning(disable: 4786) // identifier truncated in debug info
  12. #pragma warning(disable: 4710) // function not inlined
  13. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  14. #pragma warning(disable: 4514) // unreferenced inline removed
  15. #endif
  16. #include <boost/function.hpp>
  17. #include <boost/bind.hpp>
  18. #include <boost/phoenix/core.hpp>
  19. #include <boost/phoenix/bind.hpp>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  22. #pragma warning(push, 3)
  23. #endif
  24. #include <iostream>
  25. #include <boost/function.hpp>
  26. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  27. #pragma warning(pop)
  28. #endif
  29. int f1( int x )
  30. {
  31. return x;
  32. }
  33. int f2( int x, int y )
  34. {
  35. return x + y;
  36. }
  37. struct X
  38. {
  39. mutable int n;
  40. X() : n(0) {}
  41. int f0() { n += f1(17); return n; }
  42. int g0() const { n += g1(17); return n; }
  43. int f1(int a1) { return a1; }
  44. int g1(int a1) const { return a1; }
  45. };
  46. struct Y
  47. {
  48. int m;
  49. };
  50. namespace phx = boost::phoenix;
  51. using phx::placeholders::arg1;
  52. using phx::placeholders::arg2;
  53. using boost::phoenix::ref;
  54. void member_test()
  55. {
  56. Y y = { 17041 };
  57. Y * py = &y;
  58. BOOST_TEST( boost::bind( &Y::m, _1 )( y ) == 17041 );
  59. BOOST_TEST( boost::bind( &Y::m, _1 )( py ) == 17041 );
  60. BOOST_TEST( phx::bind( &Y::m, _1 )( y ) == 17041 );
  61. BOOST_TEST( phx::bind( &Y::m, _1 )( py ) == 17041 );
  62. BOOST_TEST( phx::bind( &Y::m, arg1 )( y ) == 17041 );
  63. BOOST_TEST( phx::bind( &Y::m, arg1 )( py ) == 17041 );
  64. BOOST_TEST( boost::bind( &Y::m, y )() == 17041 );
  65. BOOST_TEST( boost::bind( &Y::m, py )() == 17041 );
  66. //BOOST_TEST( boost::bind( &Y::m, ref(y) )() == 17041 );
  67. BOOST_TEST( phx::bind( &Y::m, y )() == 17041 );
  68. BOOST_TEST( phx::bind( &Y::m, py )() == 17041 );
  69. BOOST_TEST( phx::bind( &Y::m, ref(y) )() == 17041 );
  70. return;
  71. }
  72. void member_function_test()
  73. {
  74. X x;
  75. // 0
  76. BOOST_TEST(boost::bind(&X::f0, &x )() == 17);
  77. //boost::bind(&X::f0, ref(x) )(); boost::bind does not work with phx::ref.
  78. BOOST_TEST(boost::bind(&X::g0, &x )() == 34);
  79. BOOST_TEST(boost::bind(&X::g0, x )() == 51);
  80. //boost::bind(&X::g0, ref(x) )();
  81. BOOST_TEST(phx::bind(&X::f0, &x )() == 51);
  82. BOOST_TEST(phx::bind(&X::f0, ref(x) )() == 68);
  83. BOOST_TEST(phx::bind(&X::g0, &x )() == 85);
  84. BOOST_TEST(phx::bind(&X::g0, x )() == 102);
  85. BOOST_TEST(phx::bind(&X::g0, ref(x) )() == 102);
  86. return;
  87. }
  88. int main()
  89. {
  90. boost::function<int (int)> fun1_f1(boost::bind ( &f1, _1) );
  91. boost::function<int (int)> fun2_f1( phx::bind ( &f1, _1) );
  92. boost::function<int (int)> fun3_f1( phx::bind ( &f1, arg1) );
  93. BOOST_TEST( fun1_f1(1) == 1 );
  94. BOOST_TEST( fun2_f1(2) == 2 );
  95. BOOST_TEST( fun3_f1(3) == 3 );
  96. boost::function<int (int, int)> fun1_f2(boost::bind ( &f2, _1, _2) );
  97. boost::function<int (int, int)> fun2_f2( phx::bind ( &f2, _1, _2) );
  98. boost::function<int (int, int)> fun3_f2( phx::bind ( &f2, arg1, arg2) );
  99. BOOST_TEST( fun1_f2(1,2) == 3 );
  100. BOOST_TEST( fun2_f2(2,3) == 5 );
  101. BOOST_TEST( fun3_f2(3,4) == 7 );
  102. member_function_test();
  103. member_test();
  104. return boost::report_errors();
  105. }