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/spirit/include/qi.hpp>
8#include <boost/lambda/lambda.hpp>
9#include <boost/bind/bind.hpp>
10
11#include <iostream>
12
13// Presented are various ways to attach semantic actions
14// * Using plain function pointer
15// * Using simple function object
16// * Using boost.bind with a plain function
17// * Using boost.bind with a member function
18// * Using boost.lambda
19
20//[tutorial_semantic_action_functions
21namespace client
22{
23 namespace qi = boost::spirit::qi;
24
25 // A plain function
26 void print(int const& i)
27 {
28 std::cout << i << std::endl;
29 }
30
31 // A member function
32 struct writer
33 {
34 void print(int const& i) const
35 {
36 std::cout << i << std::endl;
37 }
38 };
39
40 // A function object
41 struct print_action
42 {
43 void operator()(int const& i, qi::unused_type, qi::unused_type) const
44 {
45 std::cout << i << std::endl;
46 }
47 };
48}
49//]
50
51int main()
52{
53 using boost::spirit::qi::int_;
54 using boost::spirit::qi::parse;
55 using client::print;
56 using client::writer;
57 using client::print_action;
58
59 { // example using plain function
60
61 char const *first = "{42}", *last = first + std::strlen(s: first);
62 //[tutorial_attach_actions1
63 parse(first, last, expr: '{' >> int_[&print] >> '}');
64 //]
65 }
66
67 { // example using simple function object
68
69 char const *first = "{43}", *last = first + std::strlen(s: first);
70 //[tutorial_attach_actions2
71 parse(first, last, expr: '{' >> int_[print_action()] >> '}');
72 //]
73 }
74
75 { // example using boost.bind with a plain function
76
77 char const *first = "{44}", *last = first + std::strlen(s: first);
78 using boost::placeholders::_1;
79 //[tutorial_attach_actions3
80 parse(first, last, expr: '{' >> int_[boost::bind(f: &print, a1: _1)] >> '}');
81 //]
82 }
83
84 { // example using boost.bind with a member function
85
86 char const *first = "{44}", *last = first + std::strlen(s: first);
87 using boost::placeholders::_1;
88 //[tutorial_attach_actions4
89 writer w;
90 parse(first, last, expr: '{' >> int_[boost::bind(f: &writer::print, a1: &w, a2: _1)] >> '}');
91 //]
92 }
93
94 { // example using boost.lambda
95
96 namespace lambda = boost::lambda;
97 char const *first = "{45}", *last = first + std::strlen(s: first);
98 using lambda::_1;
99 //[tutorial_attach_actions5
100 parse(first, last, expr: '{' >> int_[std::cout << _1 << '\n'] >> '}');
101 //]
102 }
103
104 return 0;
105}
106
107
108
109
110

source code of boost/libs/spirit/example/qi/actions.cpp