member_pointer_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // member_pointer_test.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. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // For more information, see www.boost.org
  11. // -----------------------------------------------------------------------
  12. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. #include "boost/lambda/lambda.hpp"
  14. #include "boost/lambda/bind.hpp"
  15. #include <string>
  16. using namespace boost::lambda;
  17. using namespace std;
  18. struct my_struct {
  19. my_struct(int x) : mem(x) {};
  20. int mem;
  21. int fooc() const { return mem; }
  22. int foo() { return mem; }
  23. int foo1c(int y) const { return y + mem; }
  24. int foo1(int y) { return y + mem; }
  25. int foo2c(int y, int x) const { return y + x + mem; }
  26. int foo2(int y, int x) { return y + x + mem; }
  27. int foo3c(int y, int x, int z) const { return y + x + z + mem; }
  28. int foo3(int y, int x, int z ){ return y + x + z + mem; }
  29. int foo4c(int a1, int a2, int a3, int a4) const { return a1+a2+a3+a4+mem; }
  30. int foo4(int a1, int a2, int a3, int a4){ return a1+a2+a3+a4+mem; }
  31. int foo3default(int y = 1, int x = 2, int z = 3) { return y + x + z + mem; }
  32. };
  33. my_struct x(3);
  34. void pointer_to_data_member_tests() {
  35. // int i = 0;
  36. my_struct *y = &x;
  37. BOOST_CHECK((_1 ->* &my_struct::mem)(y) == 3);
  38. (_1 ->* &my_struct::mem)(y) = 4;
  39. BOOST_CHECK(x.mem == 4);
  40. ((_1 ->* &my_struct::mem) = 5)(y);
  41. BOOST_CHECK(x.mem == 5);
  42. // &my_struct::mem is a temporary, must be constified
  43. ((y ->* _1) = 6)(make_const(&my_struct::mem));
  44. BOOST_CHECK(x.mem == 6);
  45. ((_1 ->* _2) = 7)(y, make_const(&my_struct::mem));
  46. BOOST_CHECK(x.mem == 7);
  47. }
  48. void pointer_to_member_function_tests() {
  49. my_struct *y = new my_struct(1);
  50. BOOST_CHECK( (_1 ->* &my_struct::foo)(y)() == (y->mem));
  51. BOOST_CHECK( (_1 ->* &my_struct::fooc)(y)() == (y->mem));
  52. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo))() == (y->mem));
  53. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::fooc))() == (y->mem));
  54. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo))() == (y->mem));
  55. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::fooc))() == (y->mem));
  56. BOOST_CHECK( (_1 ->* &my_struct::foo1)(y)(1) == (y->mem+1));
  57. BOOST_CHECK( (_1 ->* &my_struct::foo1c)(y)(1) == (y->mem+1));
  58. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo1))(1) == (y->mem+1));
  59. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo1c))(1) == (y->mem+1));
  60. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo1))(1) == (y->mem+1));
  61. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo1c))(1) == (y->mem+1));
  62. BOOST_CHECK( (_1 ->* &my_struct::foo2)(y)(1,2) == (y->mem+1+2));
  63. BOOST_CHECK( (_1 ->* &my_struct::foo2c)(y)(1,2) == (y->mem+1+2));
  64. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
  65. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));
  66. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
  67. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));
  68. BOOST_CHECK( (_1 ->* &my_struct::foo3)(y)(1,2,3) == (y->mem+1+2+3));
  69. BOOST_CHECK( (_1 ->* &my_struct::foo3c)(y)(1,2,3) == (y->mem+1+2+3));
  70. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
  71. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));
  72. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
  73. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));
  74. BOOST_CHECK( (_1 ->* &my_struct::foo4)(y)(1,2,3,4) == (y->mem+1+2+3+4));
  75. BOOST_CHECK( (_1 ->* &my_struct::foo4c)(y)(1,2,3,4) == (y->mem+1+2+3+4));
  76. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
  77. BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));
  78. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
  79. BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));
  80. // member functions with default values do not work (inherent language issue)
  81. // BOOST_CHECK( (_1 ->* &my_struct::foo3default)(y)() == (y->mem+1+2+3));
  82. }
  83. class A {};
  84. class B {};
  85. class C {};
  86. class D {};
  87. // ->* can be overloaded to do anything
  88. bool operator->*(A /*a*/, B /*b*/) {
  89. return false;
  90. }
  91. bool operator->*(B /*b*/, A /*a*/) {
  92. return true;
  93. }
  94. // let's provide specializations to take care of the return type deduction.
  95. // Note, that you need to provide all four cases for non-const and const
  96. // or use the plain_return_type_2 template.
  97. namespace boost {
  98. namespace lambda {
  99. template <>
  100. struct return_type_2<other_action<member_pointer_action>, B, A> {
  101. typedef bool type;
  102. };
  103. template<>
  104. struct return_type_2<other_action<member_pointer_action>, const B, A> {
  105. typedef bool type;
  106. };
  107. template<>
  108. struct return_type_2<other_action<member_pointer_action>, B, const A> {
  109. typedef bool type;
  110. };
  111. template<>
  112. struct return_type_2<other_action<member_pointer_action>, const B, const A> {
  113. typedef bool type;
  114. };
  115. } // lambda
  116. } // boost
  117. void test_overloaded_pointer_to_member()
  118. {
  119. A a; B b;
  120. // this won't work, can't deduce the return type
  121. // BOOST_CHECK((_1->*_2)(a, b) == false);
  122. // ret<bool> gives the return type
  123. BOOST_CHECK(ret<bool>(_1->*_2)(a, b) == false);
  124. BOOST_CHECK(ret<bool>(a->*_1)(b) == false);
  125. BOOST_CHECK(ret<bool>(_1->*b)(a) == false);
  126. BOOST_CHECK((ret<bool>((var(a))->*b))() == false);
  127. BOOST_CHECK((ret<bool>((var(a))->*var(b)))() == false);
  128. // this is ok without ret<bool> due to the return_type_2 spcialization above
  129. BOOST_CHECK((_1->*_2)(b, a) == true);
  130. BOOST_CHECK((b->*_1)(a) == true);
  131. BOOST_CHECK((_1->*a)(b) == true);
  132. BOOST_CHECK((var(b)->*a)() == true);
  133. return;
  134. }
  135. int test_main(int, char *[]) {
  136. pointer_to_data_member_tests();
  137. pointer_to_member_function_tests();
  138. test_overloaded_pointer_to_member();
  139. return 0;
  140. }