| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2007 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 <iostream> |
| 8 | #include <string> |
| 9 | #include <cmath> |
| 10 | |
| 11 | #include <boost/detail/lightweight_test.hpp> |
| 12 | #include <boost/phoenix/core.hpp> |
| 13 | #include <boost/phoenix/operator.hpp> |
| 14 | #include <boost/phoenix/scope/dynamic.hpp> |
| 15 | |
| 16 | struct my_dynamic : ::boost::phoenix::dynamic<int, std::string, double> |
| 17 | { |
| 18 | my_dynamic() : num(init<0>(scope: this)), message(init<1>(scope: this)), real(init<2>(scope: this)) {} |
| 19 | |
| 20 | member1 num; |
| 21 | member2 message; |
| 22 | member3 real; |
| 23 | }; |
| 24 | |
| 25 | // You may also use the macro below to achieve the same as above: |
| 26 | // |
| 27 | //BOOST_PHOENIX_DYNAMIC( |
| 28 | // my_dynamic, |
| 29 | // (int, num) |
| 30 | // (std::string, message) |
| 31 | // (double, real) |
| 32 | //); |
| 33 | |
| 34 | int |
| 35 | main() |
| 36 | { |
| 37 | using namespace boost::phoenix; |
| 38 | using namespace boost::phoenix::arg_names; |
| 39 | |
| 40 | my_dynamic clos; |
| 41 | |
| 42 | { // First stack frame |
| 43 | dynamic_frame<my_dynamic::self_type> frame(clos); |
| 44 | (clos.num = 123)(); |
| 45 | (clos.num += 456)(); |
| 46 | (clos.real = clos.num / 56.5)(); |
| 47 | (clos.message = "Hello " + std::string("World " ))(); |
| 48 | |
| 49 | { // Second stack frame |
| 50 | dynamic_frame<my_dynamic::self_type> frame(clos); |
| 51 | (clos.num = 987)(); |
| 52 | (clos.message = std::string("Abracadabra " ))(); |
| 53 | (clos.real = clos.num * 1e30)(); |
| 54 | |
| 55 | { // Third stack frame |
| 56 | boost::phoenix::vector3<int, std::string, double> init = {.a0: -1, .a1: "Direct Init " , .a2: 3.14}; |
| 57 | dynamic_frame<my_dynamic::self_type> frame(clos, init); |
| 58 | |
| 59 | (std::cout << clos.message << clos.num << ", " << clos.real << '\n')(); |
| 60 | BOOST_TEST(clos.num() == -1); |
| 61 | BOOST_TEST(clos.message() == "Direct Init " ); |
| 62 | } |
| 63 | |
| 64 | (std::cout << clos.message << clos.num << ", " << clos.real << '\n')(); |
| 65 | BOOST_TEST(clos.num() == 987); |
| 66 | BOOST_TEST(clos.message() == "Abracadabra " ); |
| 67 | } |
| 68 | |
| 69 | (std::cout << clos.message << clos.num << ", " << clos.real << '\n')(); |
| 70 | BOOST_TEST(clos.num() == 123+456); |
| 71 | BOOST_TEST(clos.message() == "Hello " + std::string("World " )); |
| 72 | } |
| 73 | |
| 74 | return boost::report_errors(); |
| 75 | } |
| 76 | |