summaryrefslogtreecommitdiff
path: root/libs/lambda/test/member_pointer_test.cpp
blob: e8121d564530aaffe4766fb148dc76a9620df48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//  member_pointer_test.cpp  -- The Boost Lambda Library ------------------
//
// Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org

// -----------------------------------------------------------------------


#include <boost/test/minimal.hpp>    // see "Header Implementation Option"


#include "boost/lambda/lambda.hpp"
#include "boost/lambda/bind.hpp"

#include <string>

using namespace boost::lambda;
using namespace std;


struct my_struct {
my_struct(int x) : mem(x) {};

  int mem;

  int fooc() const { return mem; }
  int foo() { return mem; }
  int foo1c(int y) const { return y + mem; }
  int foo1(int y)  { return y + mem; }
  int foo2c(int y, int x) const { return y + x + mem; }
  int foo2(int y, int x) { return y + x + mem; }
  int foo3c(int y, int x, int z) const { return y + x + z + mem; }
  int foo3(int y, int x, int z ){ return y + x + z + mem; }
  int foo4c(int a1, int a2, int a3, int a4) const { return a1+a2+a3+a4+mem; }
  int foo4(int a1, int a2, int a3, int a4){ return a1+a2+a3+a4+mem; }

  int foo3default(int y = 1, int x = 2, int z = 3) { return y + x + z + mem; }
};

my_struct x(3);

void pointer_to_data_member_tests() {

  //  int i = 0;
  my_struct *y = &x;

  BOOST_CHECK((_1 ->* &my_struct::mem)(y) == 3);

  (_1 ->* &my_struct::mem)(y) = 4;
  BOOST_CHECK(x.mem == 4);

  ((_1 ->* &my_struct::mem) = 5)(y);
  BOOST_CHECK(x.mem == 5);

  // &my_struct::mem is a temporary, must be constified
  ((y ->* _1) = 6)(make_const(&my_struct::mem));
  BOOST_CHECK(x.mem == 6);

  ((_1 ->* _2) = 7)(y, make_const(&my_struct::mem));
  BOOST_CHECK(x.mem == 7);

}

void pointer_to_member_function_tests() {  

  my_struct *y = new my_struct(1);
  BOOST_CHECK( (_1 ->* &my_struct::foo)(y)() == (y->mem));
  BOOST_CHECK( (_1 ->* &my_struct::fooc)(y)() == (y->mem));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo))() == (y->mem));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::fooc))() == (y->mem));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo))() == (y->mem));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::fooc))() == (y->mem));

  BOOST_CHECK( (_1 ->* &my_struct::foo1)(y)(1) == (y->mem+1));
  BOOST_CHECK( (_1 ->* &my_struct::foo1c)(y)(1) == (y->mem+1));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo1))(1) == (y->mem+1));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo1c))(1) == (y->mem+1));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo1))(1) == (y->mem+1));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo1c))(1) == (y->mem+1));

  BOOST_CHECK( (_1 ->* &my_struct::foo2)(y)(1,2) == (y->mem+1+2));
  BOOST_CHECK( (_1 ->* &my_struct::foo2c)(y)(1,2) == (y->mem+1+2));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));

  BOOST_CHECK( (_1 ->* &my_struct::foo3)(y)(1,2,3) == (y->mem+1+2+3));
  BOOST_CHECK( (_1 ->* &my_struct::foo3c)(y)(1,2,3) == (y->mem+1+2+3));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));

  BOOST_CHECK( (_1 ->* &my_struct::foo4)(y)(1,2,3,4) == (y->mem+1+2+3+4));
  BOOST_CHECK( (_1 ->* &my_struct::foo4c)(y)(1,2,3,4) == (y->mem+1+2+3+4));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
  BOOST_CHECK( (y ->* _1)(make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
  BOOST_CHECK( (_1 ->* _2)(y, make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));



  // member functions with default values do not work (inherent language issue)
  //  BOOST_CHECK( (_1 ->* &my_struct::foo3default)(y)() == (y->mem+1+2+3));

}

class A {};
class B {};
class C {};
class D {};

// ->* can be overloaded to do anything
bool operator->*(A /*a*/, B /*b*/) {
  return false; 
}

bool operator->*(B /*b*/, A /*a*/) {
  return true; 
}

// let's provide specializations to take care of the return type deduction.
// Note, that you need to provide all four cases for non-const and const
// or use the plain_return_type_2 template.
namespace boost {
namespace lambda {

template <>
struct return_type_2<other_action<member_pointer_action>, B, A> {
  typedef bool type;
};

template<>
struct return_type_2<other_action<member_pointer_action>, const B, A> {
  typedef bool type;
};

template<>
struct return_type_2<other_action<member_pointer_action>, B, const A> {
  typedef bool type;
};

template<>
struct return_type_2<other_action<member_pointer_action>, const B, const A> {
  typedef bool type;
};




} // lambda
} // boost

void test_overloaded_pointer_to_member()
{
  A a; B b;

  // this won't work, can't deduce the return type
  //  BOOST_CHECK((_1->*_2)(a, b) == false); 

  // ret<bool> gives the return type
  BOOST_CHECK(ret<bool>(_1->*_2)(a, b) == false);
  BOOST_CHECK(ret<bool>(a->*_1)(b) == false);
  BOOST_CHECK(ret<bool>(_1->*b)(a) == false);
  BOOST_CHECK((ret<bool>((var(a))->*b))() == false);
  BOOST_CHECK((ret<bool>((var(a))->*var(b)))() == false);


  // this is ok without ret<bool> due to the return_type_2 spcialization above
  BOOST_CHECK((_1->*_2)(b, a) == true);
  BOOST_CHECK((b->*_1)(a) == true);
  BOOST_CHECK((_1->*a)(b) == true);
  BOOST_CHECK((var(b)->*a)() == true);
  return;
}


int test_main(int, char *[]) {

  pointer_to_data_member_tests();
  pointer_to_member_function_tests();
  test_overloaded_pointer_to_member();
  return 0;
}