File: dynamic_tests.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (75 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*=============================================================================
    Copyright (c) 2001-2007 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying 
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <iostream>
#include <string>
#include <cmath>

#include <boost/detail/lightweight_test.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/scope/dynamic.hpp>

struct my_dynamic : ::boost::phoenix::dynamic<int, std::string, double>
{
    my_dynamic() : num(init<0>(this)), message(init<1>(this)), real(init<2>(this)) {}

    member1 num;
    member2 message;
    member3 real;
};

//  You may also use the macro below to achieve the same as above:
//
//BOOST_PHOENIX_DYNAMIC(
//    my_dynamic,
//        (int, num)
//        (std::string, message)
//        (double, real)
//);

int
main()
{
    using namespace boost::phoenix;
    using namespace boost::phoenix::arg_names;

    my_dynamic clos;

    {   //  First stack frame
        dynamic_frame<my_dynamic::self_type> frame(clos);
        (clos.num = 123)();
        (clos.num += 456)();
        (clos.real = clos.num / 56.5)();
        (clos.message = "Hello " + std::string("World "))();

        {   //  Second stack frame
            dynamic_frame<my_dynamic::self_type> frame(clos);
            (clos.num = 987)();
            (clos.message = std::string("Abracadabra "))();
            (clos.real = clos.num * 1e30)();

            {   //  Third stack frame
                boost::phoenix::vector3<int, std::string, double> init = {-1, "Direct Init ", 3.14};
                dynamic_frame<my_dynamic::self_type> frame(clos, init);

                (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
                BOOST_TEST(clos.num() == -1);
                BOOST_TEST(clos.message() == "Direct Init ");
            }

            (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
            BOOST_TEST(clos.num() == 987);
            BOOST_TEST(clos.message() == "Abracadabra ");
        }

        (std::cout << clos.message << clos.num << ", " << clos.real << '\n')();
        BOOST_TEST(clos.num() == 123+456);
        BOOST_TEST(clos.message() == "Hello " + std::string("World "));
    }

    return boost::report_errors();
}