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