actions.cpp 3.5 KB

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