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

source code of boost/libs/lambda/test/member_pointer_test.cpp