File: recursive_wrapper_test.cpp

package info (click to toggle)
mapbox-variant 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,648 kB
  • sloc: cpp: 31,068; ansic: 959; python: 424; makefile: 145; objc: 59; sh: 36
file content (124 lines) | stat: -rw-r--r-- 2,873 bytes parent folder | download | duplicates (4)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cstdlib>
#include <iostream>
#include <string>
#include <typeinfo>
#include <utility>

#include "auto_cpu_timer.hpp"

#include <mapbox/variant.hpp>

using namespace mapbox;

namespace test {

struct add;
struct sub;

template <typename OpTag>
struct binary_op;

typedef util::variant<int,
                      util::recursive_wrapper<binary_op<add>>,
                      util::recursive_wrapper<binary_op<sub>>>
    expression;

template <typename Op>
struct binary_op
{
    expression left; // variant instantiated here...
    expression right;

    binary_op(expression&& lhs, expression&& rhs)
        : left(std::move(lhs)), right(std::move(rhs))
    {
    }
};

struct print
{
    template <typename T>
    void operator()(T const& val) const
    {
        std::cerr << val << ":" << typeid(T).name() << std::endl;
    }
};

struct test
{
    template <typename T>
    std::string operator()(T const& obj) const
    {
        return std::string("TYPE_ID=") + typeid(obj).name();
    }
};

struct calculator
{
  public:
    int operator()(int value) const
    {
        return value;
    }

    int operator()(binary_op<add> const& binary) const
    {
        return util::apply_visitor(*this, binary.left) + util::apply_visitor(*this, binary.right);
    }

    int operator()(binary_op<sub> const& binary) const
    {
        return util::apply_visitor(*this, binary.left) - util::apply_visitor(*this, binary.right);
    }
};

struct to_string
{
  public:
    std::string operator()(int value) const
    {
        return std::to_string(value);
    }

    std::string operator()(binary_op<add> const& binary) const
    {
        return util::apply_visitor(*this, binary.left) + std::string("+") + util::apply_visitor(*this, binary.right);
    }

    std::string operator()(binary_op<sub> const& binary) const
    {
        return util::apply_visitor(*this, binary.left) + std::string("-") + util::apply_visitor(*this, binary.right);
    }
};

} // namespace test

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        std::cerr << "Usage" << argv[0] << " <num-iter>" << std::endl;
        return EXIT_FAILURE;
    }

    const std::size_t NUM_ITER = static_cast<std::size_t>(std::stol(argv[1]));

    test::expression sum(test::binary_op<test::add>(2, 3));
    test::expression result(test::binary_op<test::sub>(std::move(sum), 4));

    std::cerr << "TYPE OF RESULT-> " << util::apply_visitor(test::test(), result) << std::endl;

    int total = 0;
    {
        auto_cpu_timer t;
        for (std::size_t i = 0; i < NUM_ITER; ++i)
        {
            total += util::apply_visitor(test::calculator(), result);
        }
    }
    std::cerr << "total=" << total << std::endl;

    std::cerr << util::apply_visitor(test::to_string(), result) << "=" << util::apply_visitor(test::calculator(), result) << std::endl;

    return EXIT_SUCCESS;
}