1 /*=============================================================================
2     Copyright (c) 2001-2010 Hartmut Kaiser
3     Copyright (c) 2001-2010 Joel de Guzman
4 
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/spirit/include/karma.hpp>
11 #include <boost/lambda/lambda.hpp>
12 #include <boost/bind.hpp>
13 
14 #include <iostream>
15 #include <sstream>
16 
17 // Presented are various ways to attach semantic actions
18 //  * Using plain function pointer
19 //  * Using simple function object
20 //  * Using boost.bind
21 //  * Using boost.lambda
22 
23 using boost::spirit::unused_type;
24 
25 //[karma_tutorial_semantic_action_functions
26 namespace client
27 {
28     namespace karma = boost::spirit::karma;
29 
30     // A plain function
read_function(int & i)31     void read_function(int& i)
32     {
33         i = 42;
34     }
35 
36     // A member function
37     struct reader
38     {
printclient::reader39         void print(int& i) const
40         {
41             i = 42;
42         }
43     };
44 
45     // A function object
46     struct read_action
47     {
operator ()client::read_action48         void operator()(int& i, unused_type, unused_type) const
49         {
50             i = 42;
51         }
52     };
53 }
54 //]
55 
56 ///////////////////////////////////////////////////////////////////////////////
main()57 int main()
58 {
59     using boost::spirit::karma::int_;
60     using boost::spirit::karma::generate;
61     using client::read_function;
62     using client::reader;
63     using client::read_action;
64 
65     { // example using plain functions
66         using namespace boost::spirit;
67 
68         std::string generated;
69         std::back_insert_iterator<std::string> outiter(generated);
70 
71         //[karma_tutorial_attach_actions1
72         generate(outiter, '{' << int_[&read_function] << '}');
73         //]
74 
75         std::cout << "Simple function: " << generated << std::endl;
76     }
77 
78     { // example using simple function object
79         using namespace boost::spirit;
80 
81         std::string generated;
82         std::back_insert_iterator<std::string> outiter(generated);
83 
84         //[karma_tutorial_attach_actions2
85         generate(outiter, '{' << int_[read_action()] << '}');
86         //]
87 
88         std::cout << "Simple function object: " << generated << std::endl;
89     }
90 
91     { // example using plain function with boost.bind
92         std::string generated;
93         std::back_insert_iterator<std::string> outiter(generated);
94 
95         //[karma_tutorial_attach_actions3
96         generate(outiter, '{' << int_[boost::bind(&read_function, _1)] << '}');
97         //]
98 
99         std::cout << "Simple function with Boost.Bind: " << generated << std::endl;
100     }
101 
102     { // example using member function with boost.bind
103         std::string generated;
104         std::back_insert_iterator<std::string> outiter(generated);
105 
106         //[karma_tutorial_attach_actions4
107         reader r;
108         generate(outiter, '{' << int_[boost::bind(&reader::print, &r, _1)] << '}');
109         //]
110 
111         std::cout << "Member function: " << generated << std::endl;
112     }
113 
114     { // example using boost.lambda
115         namespace lambda = boost::lambda;
116         using namespace boost::spirit;
117 
118         std::string generated;
119         std::back_insert_iterator<std::string> outiter(generated);
120 
121         //[karma_tutorial_attach_actions5
122         std::stringstream strm("42");
123         generate(outiter, '{' << int_[strm >> lambda::_1] << '}');
124         //]
125 
126         std::cout << "Boost.Lambda: " << generated << std::endl;
127     }
128 
129     return 0;
130 }
131 
132