closures.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*=============================================================================
  2. Phoenix V1.2.1
  3. Copyright (c) 2001-2003 Joel de Guzman
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <vector>
  9. #include <algorithm>
  10. #include <iostream>
  11. #include <cassert>
  12. #define PHOENIX_LIMIT 15
  13. #include <boost/spirit/include/phoenix1_operators.hpp>
  14. #include <boost/spirit/include/phoenix1_primitives.hpp>
  15. #include <boost/spirit/include/phoenix1_composite.hpp>
  16. #include <boost/spirit/include/phoenix1_special_ops.hpp>
  17. #include <boost/spirit/include/phoenix1_statements.hpp>
  18. #include <boost/spirit/include/phoenix1_functions.hpp>
  19. #include <boost/spirit/include/phoenix1_closures.hpp>
  20. //////////////////////////////////
  21. using namespace phoenix;
  22. //////////////////////////////////
  23. int
  24. main()
  25. {
  26. struct my_closure : closure<int, std::string, double> {
  27. member1 num;
  28. member2 message;
  29. member3 real;
  30. };
  31. my_closure clos;
  32. { // First stack frame
  33. closure_frame<my_closure::self_t> frame(clos);
  34. (clos.num = 123)();
  35. (clos.num += 456)();
  36. (clos.real = clos.num / 56.5)();
  37. (clos.message = "Hello " + std::string("World "))();
  38. { // Second stack frame
  39. closure_frame<my_closure::self_t> frame(clos);
  40. (clos.num = 987)();
  41. (clos.message = "Abracadabra ")();
  42. (clos.real = clos.num * 1e30)();
  43. { // Third stack frame
  44. tuple<int, char const*, double> init(-1, "Direct Init ", 3.14);
  45. closure_frame<my_closure::self_t> frame(clos, init);
  46. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  47. }
  48. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  49. }
  50. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  51. }
  52. return 0;
  53. }