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/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         using namespace boost::placeholders;
93         std::string generated;
94         std::back_insert_iterator<std::string> outiter(generated);
95 
96         //[karma_tutorial_attach_actions3
97         generate(outiter, '{' << int_[boost::bind(&read_function, _1)] << '}');
98         //]
99 
100         std::cout << "Simple function with Boost.Bind: " << generated << std::endl;
101     }
102 
103     { // example using member function with boost.bind
104         using namespace boost::placeholders;
105         std::string generated;
106         std::back_insert_iterator<std::string> outiter(generated);
107 
108         //[karma_tutorial_attach_actions4
109         reader r;
110         generate(outiter, '{' << int_[boost::bind(&reader::print, &r, _1)] << '}');
111         //]
112 
113         std::cout << "Member function: " << generated << std::endl;
114     }
115 
116     { // example using boost.lambda
117         namespace lambda = boost::lambda;
118         using namespace boost::spirit;
119 
120         std::string generated;
121         std::back_insert_iterator<std::string> outiter(generated);
122 
123         //[karma_tutorial_attach_actions5
124         std::stringstream strm("42");
125         generate(outiter, '{' << int_[strm >> lambda::_1] << '}');
126         //]
127 
128         std::cout << "Boost.Lambda: " << generated << std::endl;
129     }
130 
131     return 0;
132 }
133 
134