• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Phoenix V1.2.1
3     Copyright (c) 2001-2003 Joel de Guzman
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #include <iostream>
10 #include <functional>
11 #include <boost/detail/lightweight_test.hpp>
12 
13 #define PHOENIX_LIMIT 15
14 #include <boost/spirit/include/phoenix1_primitives.hpp>
15 #include <boost/spirit/include/phoenix1_composite.hpp>
16 #include <boost/spirit/include/phoenix1_binders.hpp>
17 
18 using namespace phoenix;
19 using std::cout;
20 using std::endl;
21 
22     ///////////////////////////////////////////////////////////////////////////////
23     struct print_ { // a typical STL style monomorphic functor
24 
25         typedef void result_type;
operator ()print_26         void operator()()               { cout << "got no args\n"; }
operator ()print_27         void operator()(int n0)         { cout << "got 1 arg " << n0 << " \n"; }
operator ()print_28         void operator()(int n0, int n1) { cout << "got 2 args " << n0 << ", " << n1 << " \n"; }
29 
foo0print_30         void foo0() const               { cout << "print_::foo0\n"; }
foo1print_31         void foo1(int n0)               { cout << "print_::foo1 " << n0 << " \n"; }
foo2print_32         void foo2(int n0, int n1)       { cout << "print_::foo2 " << n0 << ", " << n1 << " \n"; }
33 
34         int x;
35     };
36 
37     functor<print_> print = print_();
38     member_function_ptr<void, print_, int> print_foo1 = &print_::foo1;
39     member_function_ptr<void, print_, int, int> print_foo2 = &print_::foo2;
40     member_var_ptr<int, print_> print_x = &print_::x;
41     print_ p;
42     bound_member<void, print_, int> bound_print_foo1(p,&print_::foo1);
43     bound_member<void, print_, int, int> bound_print_foo2(&p,&print_::foo2);
44 
45     ///////////////////////////////////////////////////////////////////////////////
foo0()46     void foo0()                         //  a function w/ 0 args
47     { cout << "foo0\n"; }
48 
foo1(int n0)49     void foo1(int n0)                   //  a function w/ 1 arg
50     { cout << "foo1 " << n0 << " \n"; }
51 
foo2(int n0,int n1)52     void foo2(int n0, int n1)           //  a function w/ 2 args
53     { cout << "foo2 " << n0 << ", " << n1 << " \n"; }
54 
foo3_(int n0,int n1,int n2)55     void foo3_(int n0, int n1, int n2)  //  a function w/ 3 args
56     { cout << "foo3 " << n0 << ", " << n1 << ", " << n2 << " \n"; }
57 
58     function_ptr<void, int, int, int> foo3 = &foo3_;
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 int
main()62 main()
63 {
64     int     i50 = 50, i20 = 20, i100 = 100;
65 
66 ///////////////////////////////////////////////////////////////////////////////
67 //
68 //  Binders
69 //
70 ///////////////////////////////////////////////////////////////////////////////
71 
72 //  Functor binders
73 
74     print()();
75     print(111)();
76     print(111, arg1)(i100);
77     print(111, 222)();
78     cout << bind(std::negate<int>())(arg1)(i20) << endl;
79     cout << bind(std::plus<int>())(arg1, arg2)(i20, i50) << endl;
80 
81 //  Function binders
82 
83     bind(&foo0)()();
84     bind(&foo1)(111)();
85     bind(&foo2)(111, arg1)(i100);
86     bind(&foo2)(111, 222)();
87 
88     foo3(111, 222, 333)();
89     foo3(arg1, arg2, arg3)(i20, i50, i100);
90     foo3(111, arg1, arg2)(i50, i100);
91 
92 //  Member function binders
93 
94     print_ printer;
95     bind(&print_::foo0)(arg1)(printer);
96 
97     bind(&print_::foo1)(arg1, 111)(printer);
98     print_foo1(arg1, 111)(printer);
99     print_foo1(var(printer), 111)();
100     print_foo2(var(printer), 111, 222)();
101     print_foo2(var(printer), 111, arg1)(i100);
102 
103 //  Member var binders
104 
105     printer.x = 3;
106     BOOST_TEST(bind(&print_::x)(arg1)(printer) == 3);
107     BOOST_TEST(print_x(arg1)(printer) == 3);
108     BOOST_TEST(print_x(printer)() == 3);
109     BOOST_TEST(0 != (print_x(var(printer))() = 4));
110     BOOST_TEST(printer.x == 4);
111 
112 //  Bound member functions
113 
114     bind(&printer,&print_::foo0)()();
115 
116     bind(printer,&print_::foo1)(111)();
117     bound_print_foo1(111)();
118     bound_print_foo1(111)();
119     bound_print_foo2(111, 222)();
120     bound_print_foo2(111, arg1)(i100);
121 
122     return boost::report_errors();
123 }
124