]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
f67539c2 | 10 | #include <boost/bind/bind.hpp> |
7c673cae FG |
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 | |
27 | void print(int const& i) | |
28 | { | |
29 | std::cout << i << std::endl; | |
30 | } | |
31 | ||
32 | // A member function | |
33 | struct writer | |
34 | { | |
35 | 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 | { | |
44 | void operator()(int const& i, qi::unused_type, qi::unused_type) const | |
45 | { | |
46 | std::cout << i << std::endl; | |
47 | } | |
48 | }; | |
49 | } | |
50 | //] | |
51 | ||
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); | |
f67539c2 | 79 | using boost::placeholders::_1; |
7c673cae FG |
80 | //[tutorial_attach_actions3 |
81 | parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}'); | |
82 | //] | |
83 | } | |
84 | ||
85 | { // example using boost.bind with a member function | |
86 | ||
87 | char const *first = "{44}", *last = first + std::strlen(first); | |
f67539c2 | 88 | using boost::placeholders::_1; |
7c673cae FG |
89 | //[tutorial_attach_actions4 |
90 | writer w; | |
91 | parse(first, last, '{' >> int_[boost::bind(&writer::print, &w, _1)] >> '}'); | |
92 | //] | |
93 | } | |
94 | ||
95 | { // example using boost.lambda | |
96 | ||
97 | namespace lambda = boost::lambda; | |
98 | char const *first = "{45}", *last = first + std::strlen(first); | |
99 | using lambda::_1; | |
100 | //[tutorial_attach_actions5 | |
101 | parse(first, last, '{' >> int_[std::cout << _1 << '\n'] >> '}'); | |
102 | //] | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 |