1 /*==============================================================================
2     Copyright (c) 2005 Peter Dimov
3     Copyright (c) 2005-2010 Joel de Guzman
4     Copyright (c) 2010 Thomas Heller
5     Copyright (c) 2015 John Fletcher
6 
7     Distributed under the Boost Software License, Version 1.0. (See accompanying
8     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 ==============================================================================*/
10 
11 #include <boost/config.hpp>
12 
13 #if defined(BOOST_MSVC)
14 #pragma warning(disable: 4786)  // identifier truncated in debug info
15 #pragma warning(disable: 4710)  // function not inlined
16 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
17 #pragma warning(disable: 4514)  // unreferenced inline removed
18 #endif
19 
20 #include <boost/function.hpp>
21 #include <boost/bind.hpp>
22 #include <boost/phoenix/core.hpp>
23 #include <boost/phoenix/bind.hpp>
24 #include <boost/detail/lightweight_test.hpp>
25 
26 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
27 #pragma warning(push, 3)
28 #endif
29 
30 #include <iostream>
31 #include <boost/function.hpp>
32 
33 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
34 #pragma warning(pop)
35 #endif
36 
f1(int x)37 int f1( int x )
38 {
39     return x;
40 }
41 
f2(int x,int y)42 int f2( int x, int y )
43 {
44     return x + y;
45 }
46 
47 struct X
48 {
49   mutable int n;
50 
XX51   X() : n(0) {}
52 
f0X53     int f0() { n += f1(17); return n; }
g0X54     int g0() const { n += g1(17); return n; }
55 
56 
f1X57     int f1(int a1) {  return a1; }
g1X58     int g1(int a1) const { return a1; }
59 };
60 
61 struct Y
62 {
63     int m;
64 };
65 
66 namespace phx = boost::phoenix;
67 using phx::placeholders::arg1;
68 using phx::placeholders::arg2;
69 using boost::phoenix::ref;
70 
member_test()71 void member_test()
72 {
73     Y y = { 17041 };
74     Y * py = &y;
75 
76     BOOST_TEST( boost::bind( &Y::m, _1 )( y ) == 17041 );
77     BOOST_TEST( boost::bind( &Y::m, _1 )( py ) == 17041 );
78 
79     BOOST_TEST( phx::bind( &Y::m, _1 )( y ) == 17041 );
80     BOOST_TEST( phx::bind( &Y::m, _1 )( py ) == 17041 );
81 
82     BOOST_TEST( phx::bind( &Y::m, arg1 )( y ) == 17041 );
83     BOOST_TEST( phx::bind( &Y::m, arg1 )( py ) == 17041 );
84 
85     BOOST_TEST( boost::bind( &Y::m, y )() == 17041 );
86     BOOST_TEST( boost::bind( &Y::m, py )() == 17041 );
87     //BOOST_TEST( boost::bind( &Y::m, ref(y) )() == 17041 );
88 
89     BOOST_TEST( phx::bind( &Y::m, y )() == 17041 );
90     BOOST_TEST( phx::bind( &Y::m, py )() == 17041 );
91     BOOST_TEST( phx::bind( &Y::m, ref(y) )() == 17041 );
92 
93     return;
94 }
95 
member_function_test()96 void member_function_test()
97 {
98 
99     X x;
100 
101     // 0
102 
103     BOOST_TEST(boost::bind(&X::f0, &x )() == 17);
104     //boost::bind(&X::f0, ref(x) )(); boost::bind does not work with phx::ref.
105 
106     BOOST_TEST(boost::bind(&X::g0, &x )() == 34);
107     BOOST_TEST(boost::bind(&X::g0, x )()  == 51);
108     //boost::bind(&X::g0, ref(x) )();
109 
110     BOOST_TEST(phx::bind(&X::f0, &x )()   == 51);
111     BOOST_TEST(phx::bind(&X::f0, ref(x) )() == 68);
112 
113     BOOST_TEST(phx::bind(&X::g0, &x )()   == 85);
114     BOOST_TEST(phx::bind(&X::g0, x )()    == 102);
115     BOOST_TEST(phx::bind(&X::g0, ref(x) )() == 102);
116 
117     return;
118 }
119 
main()120 int main()
121 {
122 
123   boost::function<int (int)> fun1_f1(boost::bind ( &f1, _1) );
124   boost::function<int (int)> fun2_f1(  phx::bind ( &f1, _1) );
125   boost::function<int (int)> fun3_f1(  phx::bind ( &f1, arg1) );
126 
127   BOOST_TEST( fun1_f1(1) == 1 );
128   BOOST_TEST( fun2_f1(2) == 2 );
129   BOOST_TEST( fun3_f1(3) == 3 );
130 
131   boost::function<int (int, int)> fun1_f2(boost::bind ( &f2, _1, _2) );
132   boost::function<int (int, int)> fun2_f2(  phx::bind ( &f2, _1, _2) );
133   boost::function<int (int, int)> fun3_f2(  phx::bind ( &f2, arg1, arg2) );
134 
135   BOOST_TEST( fun1_f2(1,2) == 3 );
136   BOOST_TEST( fun2_f2(2,3) == 5 );
137   BOOST_TEST( fun3_f2(3,4) == 7 );
138 
139   member_function_test();
140   member_test();
141   return boost::report_errors();
142 }
143