File: reference_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 (79 lines) | stat: -rw-r--r-- 2,005 bytes parent folder | download | duplicates (6)
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
#include <cstdlib>
#include <functional>
#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <vector>

#include <mapbox/variant.hpp>

using namespace mapbox;

namespace test {

struct point
{
  public:
    point(double x_, double y_)
        : x(x_), y(y_) {}
    double x;
    double y;
};

struct line_string : std::vector<point>
{
};
struct polygon : std::vector<line_string>
{
};
using variant = util::variant<std::reference_wrapper<const point>,
                              std::reference_wrapper<const line_string>,
                              std::reference_wrapper<const polygon>>;

struct print
{
    using result_type = void;
    void operator()(point const& pt) const
    {
        std::cerr << "Point(" << pt.x << "," << pt.y << ")" << std::endl;
    }
    void operator()(line_string const& line) const
    {
        std::cerr << "Line(";
        bool first = true;
        for (auto const& pt : line)
        {
            if (!first) std::cerr << ",";
            std::cerr << pt.x << " " << pt.y;
            if (first) first = false;
        }
        std::cerr << ")" << std::endl;
    }
    template <typename T>
    void operator()(T const& val) const
    {
        std::cerr << typeid(T).name() << std::endl;
    }
};
}

int main()
{
    std::cerr << sizeof(test::polygon) << std::endl;
    std::cerr << sizeof(test::variant) << std::endl;
    test::point pt(123, 456);
    test::variant var = std::cref(pt);
    util::apply_visitor(test::print(), var);
    test::line_string line;
    line.push_back(pt);
    line.push_back(pt);
    line.push_back(test::point(999, 333));
    var = std::cref(line);
    util::apply_visitor(test::print(), var);
    std::cerr << "Is line (cref) ? " << var.is<std::reference_wrapper<test::line_string const>>() << std::endl;
    auto const& line2 = var.get<test::line_string>(); // accessing underlying type of std::reference_wrapper<T>
    test::print printer;
    printer(line2);
    return EXIT_SUCCESS;
}