1 /*=============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/config/warning_disable.hpp>
8 #include <boost/spirit/include/qi.hpp>
9 #include <boost/lambda/lambda.hpp>
10 #include <boost/bind.hpp>
11 
12 #include <iostream>
13 
14 // Presented are various ways to attach semantic actions
15 //  * Using plain function pointer
16 //  * Using simple function object
17 //  * Using boost.bind with a plain function
18 //  * Using boost.bind with a member function
19 //  * Using boost.lambda
20 
21 //[tutorial_semantic_action_functions
22 namespace client
23 {
24     namespace qi = boost::spirit::qi;
25 
26     // A plain function
print(int const & i)27     void print(int const& i)
28     {
29         std::cout << i << std::endl;
30     }
31 
32     // A member function
33     struct writer
34     {
printclient::writer35         void print(int const& i) const
36         {
37             std::cout << i << std::endl;
38         }
39     };
40 
41     // A function object
42     struct print_action
43     {
operator ()client::print_action44         void operator()(int const& i, qi::unused_type, qi::unused_type) const
45         {
46             std::cout << i << std::endl;
47         }
48     };
49 }
50 //]
51 
main()52 int main()
53 {
54     using boost::spirit::qi::int_;
55     using boost::spirit::qi::parse;
56     using client::print;
57     using client::writer;
58     using client::print_action;
59 
60     { // example using plain function
61 
62         char const *first = "{42}", *last = first + std::strlen(first);
63         //[tutorial_attach_actions1
64         parse(first, last, '{' >> int_[&print] >> '}');
65         //]
66     }
67 
68     { // example using simple function object
69 
70         char const *first = "{43}", *last = first + std::strlen(first);
71         //[tutorial_attach_actions2
72         parse(first, last, '{' >> int_[print_action()] >> '}');
73         //]
74     }
75 
76     { // example using boost.bind with a plain function
77 
78         char const *first = "{44}", *last = first + std::strlen(first);
79         //[tutorial_attach_actions3
80         parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}');
81         //]
82     }
83 
84     { // example using boost.bind with a member function
85 
86         char const *first = "{44}", *last = first + std::strlen(first);
87         //[tutorial_attach_actions4
88         writer w;
89         parse(first, last, '{' >> int_[boost::bind(&writer::print, &w, _1)] >> '}');
90         //]
91     }
92 
93     { // example using boost.lambda
94 
95         namespace lambda = boost::lambda;
96         char const *first = "{45}", *last = first + std::strlen(first);
97         using lambda::_1;
98         //[tutorial_attach_actions5
99         parse(first, last, '{' >> int_[std::cout << _1 << '\n'] >> '}');
100         //]
101     }
102 
103     return 0;
104 }
105 
106 
107 
108 
109