dynamic_tests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <string>
  8. #include <cmath>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/operator.hpp>
  12. #include <boost/phoenix/scope/dynamic.hpp>
  13. struct my_dynamic : ::boost::phoenix::dynamic<int, std::string, double>
  14. {
  15. my_dynamic() : num(init<0>(this)), message(init<1>(this)), real(init<2>(this)) {}
  16. member1 num;
  17. member2 message;
  18. member3 real;
  19. };
  20. // You may also use the macro below to achieve the same as above:
  21. //
  22. //BOOST_PHOENIX_DYNAMIC(
  23. // my_dynamic,
  24. // (int, num)
  25. // (std::string, message)
  26. // (double, real)
  27. //);
  28. int
  29. main()
  30. {
  31. using namespace boost::phoenix;
  32. using namespace boost::phoenix::arg_names;
  33. my_dynamic clos;
  34. { // First stack frame
  35. dynamic_frame<my_dynamic::self_type> frame(clos);
  36. (clos.num = 123)();
  37. (clos.num += 456)();
  38. (clos.real = clos.num / 56.5)();
  39. (clos.message = "Hello " + std::string("World "))();
  40. { // Second stack frame
  41. dynamic_frame<my_dynamic::self_type> frame(clos);
  42. (clos.num = 987)();
  43. (clos.message = std::string("Abracadabra "))();
  44. (clos.real = clos.num * 1e30)();
  45. { // Third stack frame
  46. boost::phoenix::vector3<int, std::string, double> init = {-1, "Direct Init ", 3.14};
  47. dynamic_frame<my_dynamic::self_type> frame(clos, init);
  48. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  49. BOOST_TEST(clos.num() == -1);
  50. BOOST_TEST(clos.message() == "Direct Init ");
  51. }
  52. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  53. BOOST_TEST(clos.num() == 987);
  54. BOOST_TEST(clos.message() == "Abracadabra ");
  55. }
  56. (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
  57. BOOST_TEST(clos.num() == 123+456);
  58. BOOST_TEST(clos.message() == "Hello " + std::string("World "));
  59. }
  60. return boost::report_errors();
  61. }