]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lambda/test/member_pointer_test.cpp
1 // member_pointer_test.cpp -- The Boost Lambda Library ------------------
3 // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
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)
10 // For more information, see www.boost.org
12 // -----------------------------------------------------------------------
15 #include <boost/test/minimal.hpp> // see "Header Implementation Option"
18 #include "boost/lambda/lambda.hpp"
19 #include "boost/lambda/bind.hpp"
23 using namespace boost::lambda
;
28 my_struct(int x
) : mem(x
) {};
32 int fooc() const { return mem
; }
33 int foo() { return mem
; }
34 int foo1c(int y
) const { return y
+ mem
; }
35 int foo1(int y
) { return y
+ mem
; }
36 int foo2c(int y
, int x
) const { return y
+ x
+ mem
; }
37 int foo2(int y
, int x
) { return y
+ x
+ mem
; }
38 int foo3c(int y
, int x
, int z
) const { return y
+ x
+ z
+ mem
; }
39 int foo3(int y
, int x
, int z
){ return y
+ x
+ z
+ mem
; }
40 int foo4c(int a1
, int a2
, int a3
, int a4
) const { return a1
+a2
+a3
+a4
+mem
; }
41 int foo4(int a1
, int a2
, int a3
, int a4
){ return a1
+a2
+a3
+a4
+mem
; }
43 int foo3default(int y
= 1, int x
= 2, int z
= 3) { return y
+ x
+ z
+ mem
; }
48 void pointer_to_data_member_tests() {
53 BOOST_CHECK((_1
->* &my_struct::mem
)(y
) == 3);
55 (_1
->* &my_struct::mem
)(y
) = 4;
56 BOOST_CHECK(x
.mem
== 4);
58 ((_1
->* &my_struct::mem
) = 5)(y
);
59 BOOST_CHECK(x
.mem
== 5);
61 // &my_struct::mem is a temporary, must be constified
62 ((y
->* _1
) = 6)(make_const(&my_struct::mem
));
63 BOOST_CHECK(x
.mem
== 6);
65 ((_1
->* _2
) = 7)(y
, make_const(&my_struct::mem
));
66 BOOST_CHECK(x
.mem
== 7);
70 void pointer_to_member_function_tests() {
72 my_struct
*y
= new my_struct(1);
73 BOOST_CHECK( (_1
->* &my_struct::foo
)(y
)() == (y
->mem
));
74 BOOST_CHECK( (_1
->* &my_struct::fooc
)(y
)() == (y
->mem
));
75 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo
))() == (y
->mem
));
76 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::fooc
))() == (y
->mem
));
77 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo
))() == (y
->mem
));
78 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::fooc
))() == (y
->mem
));
80 BOOST_CHECK( (_1
->* &my_struct::foo1
)(y
)(1) == (y
->mem
+1));
81 BOOST_CHECK( (_1
->* &my_struct::foo1c
)(y
)(1) == (y
->mem
+1));
82 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo1
))(1) == (y
->mem
+1));
83 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo1c
))(1) == (y
->mem
+1));
84 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo1
))(1) == (y
->mem
+1));
85 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo1c
))(1) == (y
->mem
+1));
87 BOOST_CHECK( (_1
->* &my_struct::foo2
)(y
)(1,2) == (y
->mem
+1+2));
88 BOOST_CHECK( (_1
->* &my_struct::foo2c
)(y
)(1,2) == (y
->mem
+1+2));
89 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo2
))(1,2) == (y
->mem
+1+2));
90 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo2c
))(1,2) == (y
->mem
+1+2));
91 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo2
))(1,2) == (y
->mem
+1+2));
92 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo2c
))(1,2) == (y
->mem
+1+2));
94 BOOST_CHECK( (_1
->* &my_struct::foo3
)(y
)(1,2,3) == (y
->mem
+1+2+3));
95 BOOST_CHECK( (_1
->* &my_struct::foo3c
)(y
)(1,2,3) == (y
->mem
+1+2+3));
96 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo3
))(1,2,3) == (y
->mem
+1+2+3));
97 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo3c
))(1,2,3) == (y
->mem
+1+2+3));
98 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo3
))(1,2,3) == (y
->mem
+1+2+3));
99 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo3c
))(1,2,3) == (y
->mem
+1+2+3));
101 BOOST_CHECK( (_1
->* &my_struct::foo4
)(y
)(1,2,3,4) == (y
->mem
+1+2+3+4));
102 BOOST_CHECK( (_1
->* &my_struct::foo4c
)(y
)(1,2,3,4) == (y
->mem
+1+2+3+4));
103 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo4
))(1,2,3,4) == (y
->mem
+1+2+3+4));
104 BOOST_CHECK( (y
->* _1
)(make_const(&my_struct::foo4c
))(1,2,3,4) == (y
->mem
+1+2+3+4));
105 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo4
))(1,2,3,4) == (y
->mem
+1+2+3+4));
106 BOOST_CHECK( (_1
->* _2
)(y
, make_const(&my_struct::foo4c
))(1,2,3,4) == (y
->mem
+1+2+3+4));
110 // member functions with default values do not work (inherent language issue)
111 // BOOST_CHECK( (_1 ->* &my_struct::foo3default)(y)() == (y->mem+1+2+3));
120 // ->* can be overloaded to do anything
121 bool operator->*(A
/*a*/, B
/*b*/) {
125 bool operator->*(B
/*b*/, A
/*a*/) {
129 // let's provide specializations to take care of the return type deduction.
130 // Note, that you need to provide all four cases for non-const and const
131 // or use the plain_return_type_2 template.
136 struct return_type_2
<other_action
<member_pointer_action
>, B
, A
> {
141 struct return_type_2
<other_action
<member_pointer_action
>, const B
, A
> {
146 struct return_type_2
<other_action
<member_pointer_action
>, B
, const A
> {
151 struct return_type_2
<other_action
<member_pointer_action
>, const B
, const A
> {
161 void test_overloaded_pointer_to_member()
165 // this won't work, can't deduce the return type
166 // BOOST_CHECK((_1->*_2)(a, b) == false);
168 // ret<bool> gives the return type
169 BOOST_CHECK(ret
<bool>(_1
->*_2
)(a
, b
) == false);
170 BOOST_CHECK(ret
<bool>(a
->*_1
)(b
) == false);
171 BOOST_CHECK(ret
<bool>(_1
->*b
)(a
) == false);
172 BOOST_CHECK((ret
<bool>((var(a
))->*b
))() == false);
173 BOOST_CHECK((ret
<bool>((var(a
))->*var(b
)))() == false);
176 // this is ok without ret<bool> due to the return_type_2 spcialization above
177 BOOST_CHECK((_1
->*_2
)(b
, a
) == true);
178 BOOST_CHECK((b
->*_1
)(a
) == true);
179 BOOST_CHECK((_1
->*a
)(b
) == true);
180 BOOST_CHECK((var(b
)->*a
)() == true);
185 int test_main(int, char *[]) {
187 pointer_to_data_member_tests();
188 pointer_to_member_function_tests();
189 test_overloaded_pointer_to_member();