| 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 | #include <boost/spirit/include/karma.hpp> |
| 9 | #include <boost/lambda/lambda.hpp> |
| 10 | #include <boost/bind/bind.hpp> |
| 11 | |
| 12 | #include <iostream> |
| 13 | #include <sstream> |
| 14 | |
| 15 | // Presented are various ways to attach semantic actions |
| 16 | // * Using plain function pointer |
| 17 | // * Using simple function object |
| 18 | // * Using boost.bind |
| 19 | // * Using boost.lambda |
| 20 | |
| 21 | using boost::spirit::unused_type; |
| 22 | |
| 23 | //[karma_tutorial_semantic_action_functions |
| 24 | namespace client |
| 25 | { |
| 26 | namespace karma = boost::spirit::karma; |
| 27 | |
| 28 | // A plain function |
| 29 | void read_function(int& i) |
| 30 | { |
| 31 | i = 42; |
| 32 | } |
| 33 | |
| 34 | // A member function |
| 35 | struct reader |
| 36 | { |
| 37 | void print(int& i) const |
| 38 | { |
| 39 | i = 42; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | // A function object |
| 44 | struct read_action |
| 45 | { |
| 46 | void operator()(int& i, unused_type, unused_type) const |
| 47 | { |
| 48 | i = 42; |
| 49 | } |
| 50 | }; |
| 51 | } |
| 52 | //] |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | int main() |
| 56 | { |
| 57 | using boost::spirit::karma::int_; |
| 58 | using boost::spirit::karma::generate; |
| 59 | using client::read_function; |
| 60 | using client::reader; |
| 61 | using client::read_action; |
| 62 | |
| 63 | { // example using plain functions |
| 64 | using namespace boost::spirit; |
| 65 | |
| 66 | std::string generated; |
| 67 | std::back_insert_iterator<std::string> outiter(generated); |
| 68 | |
| 69 | //[karma_tutorial_attach_actions1 |
| 70 | generate(sink&: outiter, expr: '{' << int_[&read_function] << '}'); |
| 71 | //] |
| 72 | |
| 73 | std::cout << "Simple function: " << generated << std::endl; |
| 74 | } |
| 75 | |
| 76 | { // example using simple function object |
| 77 | using namespace boost::spirit; |
| 78 | |
| 79 | std::string generated; |
| 80 | std::back_insert_iterator<std::string> outiter(generated); |
| 81 | |
| 82 | //[karma_tutorial_attach_actions2 |
| 83 | generate(sink&: outiter, expr: '{' << int_[read_action()] << '}'); |
| 84 | //] |
| 85 | |
| 86 | std::cout << "Simple function object: " << generated << std::endl; |
| 87 | } |
| 88 | |
| 89 | { // example using plain function with boost.bind |
| 90 | using namespace boost::placeholders; |
| 91 | std::string generated; |
| 92 | std::back_insert_iterator<std::string> outiter(generated); |
| 93 | |
| 94 | //[karma_tutorial_attach_actions3 |
| 95 | generate(sink&: outiter, expr: '{' << int_[boost::bind(f: &read_function, a1: _1)] << '}'); |
| 96 | //] |
| 97 | |
| 98 | std::cout << "Simple function with Boost.Bind: " << generated << std::endl; |
| 99 | } |
| 100 | |
| 101 | { // example using member function with boost.bind |
| 102 | using namespace boost::placeholders; |
| 103 | std::string generated; |
| 104 | std::back_insert_iterator<std::string> outiter(generated); |
| 105 | |
| 106 | //[karma_tutorial_attach_actions4 |
| 107 | reader r; |
| 108 | generate(sink&: outiter, expr: '{' << int_[boost::bind(f: &reader::print, a1: &r, a2: _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(sink&: outiter, expr: '{' << int_[strm >> lambda::_1] << '}'); |
| 124 | //] |
| 125 | |
| 126 | std::cout << "Boost.Lambda: " << generated << std::endl; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |