1 // member_pointer_test.cpp -- The Boost Lambda Library ------------------
2 //
3 // Copyright (C) 2000-2003 Jaakko Jarvi ([email protected])
4 // Copyright (C) 2000-2003 Gary Powell ([email protected])
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 // -----------------------------------------------------------------------
13
14
15 #include <boost/core/lightweight_test.hpp>
16
17
18 #include "boost/lambda/lambda.hpp"
19 #include "boost/lambda/bind.hpp"
20
21 #include <string>
22
23 using namespace boost::lambda;
24 using namespace std;
25
26
27 struct my_struct {
my_structmy_struct28 my_struct(int x) : mem(x) {};
29
30 int mem;
31
foocmy_struct32 int fooc() const { return mem; }
foomy_struct33 int foo() { return mem; }
foo1cmy_struct34 int foo1c(int y) const { return y + mem; }
foo1my_struct35 int foo1(int y) { return y + mem; }
foo2cmy_struct36 int foo2c(int y, int x) const { return y + x + mem; }
foo2my_struct37 int foo2(int y, int x) { return y + x + mem; }
foo3cmy_struct38 int foo3c(int y, int x, int z) const { return y + x + z + mem; }
foo3my_struct39 int foo3(int y, int x, int z ){ return y + x + z + mem; }
foo4cmy_struct40 int foo4c(int a1, int a2, int a3, int a4) const { return a1+a2+a3+a4+mem; }
foo4my_struct41 int foo4(int a1, int a2, int a3, int a4){ return a1+a2+a3+a4+mem; }
42
foo3defaultmy_struct43 int foo3default(int y = 1, int x = 2, int z = 3) { return y + x + z + mem; }
44 };
45
46 my_struct x(3);
47
pointer_to_data_member_tests()48 void pointer_to_data_member_tests() {
49
50 // int i = 0;
51 my_struct *y = &x;
52
53 BOOST_TEST_EQ((_1 ->* &my_struct::mem)(y), 3);
54
55 (_1 ->* &my_struct::mem)(y) = 4;
56 BOOST_TEST_EQ(x.mem, 4);
57
58 ((_1 ->* &my_struct::mem) = 5)(y);
59 BOOST_TEST_EQ(x.mem, 5);
60
61 // &my_struct::mem is a temporary, must be constified
62 ((y ->* _1) = 6)(make_const(&my_struct::mem));
63 BOOST_TEST_EQ(x.mem, 6);
64
65 ((_1 ->* _2) = 7)(y, make_const(&my_struct::mem));
66 BOOST_TEST_EQ(x.mem, 7);
67
68 }
69
pointer_to_member_function_tests()70 void pointer_to_member_function_tests() {
71
72 my_struct *y = new my_struct(1);
73 BOOST_TEST_EQ( (_1 ->* &my_struct::foo)(y)(), (y->mem));
74 BOOST_TEST_EQ( (_1 ->* &my_struct::fooc)(y)(), (y->mem));
75 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo))(), (y->mem));
76 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::fooc))(), (y->mem));
77 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo))(), (y->mem));
78 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::fooc))(), (y->mem));
79
80 BOOST_TEST_EQ( (_1 ->* &my_struct::foo1)(y)(1), (y->mem+1));
81 BOOST_TEST_EQ( (_1 ->* &my_struct::foo1c)(y)(1), (y->mem+1));
82 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo1))(1), (y->mem+1));
83 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo1c))(1), (y->mem+1));
84 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo1))(1), (y->mem+1));
85 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo1c))(1), (y->mem+1));
86
87 BOOST_TEST_EQ( (_1 ->* &my_struct::foo2)(y)(1,2), (y->mem+1+2));
88 BOOST_TEST_EQ( (_1 ->* &my_struct::foo2c)(y)(1,2), (y->mem+1+2));
89 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo2))(1,2), (y->mem+1+2));
90 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo2c))(1,2), (y->mem+1+2));
91 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo2))(1,2), (y->mem+1+2));
92 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo2c))(1,2), (y->mem+1+2));
93
94 BOOST_TEST_EQ( (_1 ->* &my_struct::foo3)(y)(1,2,3), (y->mem+1+2+3));
95 BOOST_TEST_EQ( (_1 ->* &my_struct::foo3c)(y)(1,2,3), (y->mem+1+2+3));
96 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo3))(1,2,3), (y->mem+1+2+3));
97 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo3c))(1,2,3), (y->mem+1+2+3));
98 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo3))(1,2,3), (y->mem+1+2+3));
99 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo3c))(1,2,3), (y->mem+1+2+3));
100
101 BOOST_TEST_EQ( (_1 ->* &my_struct::foo4)(y)(1,2,3,4), (y->mem+1+2+3+4));
102 BOOST_TEST_EQ( (_1 ->* &my_struct::foo4c)(y)(1,2,3,4), (y->mem+1+2+3+4));
103 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo4))(1,2,3,4), (y->mem+1+2+3+4));
104 BOOST_TEST_EQ( (y ->* _1)(make_const(&my_struct::foo4c))(1,2,3,4), (y->mem+1+2+3+4));
105 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo4))(1,2,3,4), (y->mem+1+2+3+4));
106 BOOST_TEST_EQ( (_1 ->* _2)(y, make_const(&my_struct::foo4c))(1,2,3,4), (y->mem+1+2+3+4));
107
108
109
110 // member functions with default values do not work (inherent language issue)
111 // BOOST_TEST_EQ( (_1 ->* &my_struct::foo3default)(y)(), (y->mem+1+2+3));
112
113 delete y;
114
115 }
116
117 class A {};
118 class B {};
119 class C {};
120 class D {};
121
122 // ->* can be overloaded to do anything
operator ->*(A,B)123 bool operator->*(A /*a*/, B /*b*/) {
124 return false;
125 }
126
operator ->*(B,A)127 bool operator->*(B /*b*/, A /*a*/) {
128 return true;
129 }
130
131 // let's provide specializations to take care of the return type deduction.
132 // Note, that you need to provide all four cases for non-const and const
133 // or use the plain_return_type_2 template.
134 namespace boost {
135 namespace lambda {
136
137 template <>
138 struct return_type_2<other_action<member_pointer_action>, B, A> {
139 typedef bool type;
140 };
141
142 template<>
143 struct return_type_2<other_action<member_pointer_action>, const B, A> {
144 typedef bool type;
145 };
146
147 template<>
148 struct return_type_2<other_action<member_pointer_action>, B, const A> {
149 typedef bool type;
150 };
151
152 template<>
153 struct return_type_2<other_action<member_pointer_action>, const B, const A> {
154 typedef bool type;
155 };
156
157
158
159
160 } // lambda
161 } // boost
162
test_overloaded_pointer_to_member()163 void test_overloaded_pointer_to_member()
164 {
165 A a; B b;
166
167 // this won't work, can't deduce the return type
168 // BOOST_TEST_EQ((_1->*_2)(a, b), false);
169
170 // ret<bool> gives the return type
171 BOOST_TEST_EQ(ret<bool>(_1->*_2)(a, b), false);
172 BOOST_TEST_EQ(ret<bool>(a->*_1)(b), false);
173 BOOST_TEST_EQ(ret<bool>(_1->*b)(a), false);
174 BOOST_TEST_EQ((ret<bool>((var(a))->*b))(), false);
175 BOOST_TEST_EQ((ret<bool>((var(a))->*var(b)))(), false);
176
177
178 // this is ok without ret<bool> due to the return_type_2 spcialization above
179 BOOST_TEST_EQ((_1->*_2)(b, a), true);
180 BOOST_TEST_EQ((b->*_1)(a), true);
181 BOOST_TEST_EQ((_1->*a)(b), true);
182 BOOST_TEST_EQ((var(b)->*a)(), true);
183 return;
184 }
185
186
main()187 int main()
188 {
189 pointer_to_data_member_tests();
190 pointer_to_member_function_tests();
191 test_overloaded_pointer_to_member();
192 return boost::report_errors();
193 }
194