bind_mf2_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_mf2_test.cpp - test for member functions w/ the type<> syntax
  10. //
  11. // Copyright (c) 2005, 2008 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/bind.hpp>
  18. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  19. #pragma warning(push, 3)
  20. #endif
  21. #include <iostream>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. #pragma warning(pop)
  24. #endif
  25. #include <boost/detail/lightweight_test.hpp>
  26. struct X
  27. {
  28. mutable unsigned int hash;
  29. X(): hash(0) {}
  30. int f0() { f1(17); return 0; }
  31. int g0() const { g1(17); return 0; }
  32. int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; }
  33. int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; }
  34. int f2(int a1, int a2) { f1(a1); f1(a2); return 0; }
  35. int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; }
  36. int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; }
  37. int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; }
  38. int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; }
  39. int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; }
  40. int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; }
  41. int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; }
  42. int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; }
  43. int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; }
  44. int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; }
  45. int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; }
  46. int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; }
  47. int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; }
  48. };
  49. void member_function_test()
  50. {
  51. using namespace boost;
  52. X x;
  53. // 0
  54. bind( type<void>(), &X::f0, &x )();
  55. bind( type<void>(), &X::f0, ref(x) )();
  56. bind( type<void>(), &X::g0, &x )();
  57. bind( type<void>(), &X::g0, x )();
  58. bind( type<void>(), &X::g0, ref(x) )();
  59. // 1
  60. bind( type<void>(), &X::f1, &x, 1 )();
  61. bind( type<void>(), &X::f1, ref(x), 1 )();
  62. bind( type<void>(), &X::g1, &x, 1 )();
  63. bind( type<void>(), &X::g1, x, 1 )();
  64. bind( type<void>(), &X::g1, ref(x), 1 )();
  65. // 2
  66. bind( type<void>(), &X::f2, &x, 1, 2 )();
  67. bind( type<void>(), &X::f2, ref(x), 1, 2 )();
  68. bind( type<void>(), &X::g2, &x, 1, 2 )();
  69. bind( type<void>(), &X::g2, x, 1, 2 )();
  70. bind( type<void>(), &X::g2, ref(x), 1, 2 )();
  71. // 3
  72. bind( type<void>(), &X::f3, &x, 1, 2, 3 )();
  73. bind( type<void>(), &X::f3, ref(x), 1, 2, 3 )();
  74. bind( type<void>(), &X::g3, &x, 1, 2, 3 )();
  75. bind( type<void>(), &X::g3, x, 1, 2, 3 )();
  76. bind( type<void>(), &X::g3, ref(x), 1, 2, 3 )();
  77. // 4
  78. bind( type<void>(), &X::f4, &x, 1, 2, 3, 4 )();
  79. bind( type<void>(), &X::f4, ref(x), 1, 2, 3, 4 )();
  80. bind( type<void>(), &X::g4, &x, 1, 2, 3, 4 )();
  81. bind( type<void>(), &X::g4, x, 1, 2, 3, 4 )();
  82. bind( type<void>(), &X::g4, ref(x), 1, 2, 3, 4 )();
  83. // 5
  84. bind( type<void>(), &X::f5, &x, 1, 2, 3, 4, 5 )();
  85. bind( type<void>(), &X::f5, ref(x), 1, 2, 3, 4, 5 )();
  86. bind( type<void>(), &X::g5, &x, 1, 2, 3, 4, 5 )();
  87. bind( type<void>(), &X::g5, x, 1, 2, 3, 4, 5 )();
  88. bind( type<void>(), &X::g5, ref(x), 1, 2, 3, 4, 5 )();
  89. // 6
  90. bind( type<void>(), &X::f6, &x, 1, 2, 3, 4, 5, 6 )();
  91. bind( type<void>(), &X::f6, ref(x), 1, 2, 3, 4, 5, 6 )();
  92. bind( type<void>(), &X::g6, &x, 1, 2, 3, 4, 5, 6 )();
  93. bind( type<void>(), &X::g6, x, 1, 2, 3, 4, 5, 6 )();
  94. bind( type<void>(), &X::g6, ref(x), 1, 2, 3, 4, 5, 6 )();
  95. // 7
  96. bind( type<void>(), &X::f7, &x, 1, 2, 3, 4, 5, 6, 7)();
  97. bind( type<void>(), &X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
  98. bind( type<void>(), &X::g7, &x, 1, 2, 3, 4, 5, 6, 7)();
  99. bind( type<void>(), &X::g7, x, 1, 2, 3, 4, 5, 6, 7)();
  100. bind( type<void>(), &X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
  101. // 8
  102. bind( type<void>(), &X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8 )();
  103. bind( type<void>(), &X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 )();
  104. bind( type<void>(), &X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8 )();
  105. bind( type<void>(), &X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8 )();
  106. bind( type<void>(), &X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 )();
  107. BOOST_TEST( x.hash == 23558 );
  108. }
  109. int main()
  110. {
  111. member_function_test();
  112. return boost::report_errors();
  113. }