File: lambda_overload_test.cpp

package info (click to toggle)
mapbox-variant 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,644 kB
  • sloc: cpp: 31,068; ansic: 959; python: 424; makefile: 144; objc: 59; sh: 36
file content (282 lines) | stat: -rw-r--r-- 6,343 bytes parent folder | download | duplicates (3)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <iostream>
#include <vector>

#include <mapbox/variant.hpp>
#include <mapbox/variant_visitor.hpp>

#if __cplusplus >= 201402L
#define HAS_CPP14_SUPPORT
#endif

using namespace mapbox::util;

template <typename T>
struct tag
{
    static void dump(const char* prefix)
    {
        std::cout << prefix << ": " << typeid(tag<T>).name() << std::endl;
    }
};

template <typename Left, typename Right>
using Either = mapbox::util::variant<Left, Right>;

struct Response
{
};

struct Error
{
};

void test_lambda_overloads()
{
    Either<Error, Response> rv;

    rv = Response{};

    auto visitor = make_visitor([](Response) { std::cout << "Response\n"; }, //
                                [](Error) { std::cout << "Error\n"; });      //
    apply_visitor(visitor, rv);
}

void test_lambda_overloads_capture()
{
    Either<Error, Response> rv;

    rv = Error{};

    int ok = 0;
    int err = 0;

    auto visitor = make_visitor([&](Response) { ok += 1; }, //
                                [&](Error) { err += 1; });  //
    apply_visitor(visitor, rv);

    std::cout << "Got " << ok << " ok, " << err << " err" << std::endl;
}

void test_singleton_variant()
{

    variant<int> singleton;
    apply_visitor(make_visitor([](int) {}), singleton);
}

// See #180
struct test_call_nonconst_member_visitor
{
    template <typename T>
    void operator() (T & obj) const
    {
        tag<decltype(obj)>::dump("test_call_nonconst_member: visitor");
        obj.foo();
    }
};

void test_call_nonconst_member()
{
    struct object
    {
        void foo() { val = 42;}
        int val = 0;
    };

    variant<object> v = object{};
    apply_visitor(test_call_nonconst_member_visitor{}, v);

#ifdef HAS_CPP14_SUPPORT
    apply_visitor([](auto& obj)
                  {
                      tag<decltype(obj)>::dump("test_call_nonconst_member: lambda");
                      obj.foo();
                  }, v);
#endif
}

void test_lambda_overloads_sfinae()
#ifdef HAS_CPP14_SUPPORT
{
    variant<int, float, std::vector<int>> var;

    auto visitor = make_visitor([](auto range) -> decltype(std::begin(range), void()) {
                                    for (auto each : range)
                                        std::cout << each << ' '; },
                                [](auto x) -> decltype(std::cout << x, void()) {
                                    std::cout << x << std::endl;
                                });

    var = 1;
    apply_visitor(visitor, var);

    var = 2.f;
    apply_visitor(visitor, var);

    var = std::vector<int>{4, 5, 6};
    apply_visitor(visitor, var);
}
#else
{
}
#endif

void test_match_singleton()
{
    variant<int> singleton = 5;
    singleton.match([](int) {});
    
    auto lambda = [](int) {};
    singleton.match(lambda);
}

void test_match_overloads()
{
    Either<Error, Response> rv;

    rv = Response{};

    rv.match([](Response) { std::cout << "Response\n"; }, //
             [](Error) { std::cout << "Error\n"; });      //
}

void test_match_overloads_capture()
{
    Either<Error, Response> rv;

    rv = Error{};

    int ok = 0;
    int err = 0;

    rv.match([&](Response) { ok += 1; }, //
             [&](Error) { err += 1; });  //

    std::cout << "Got " << ok << " ok, " << err << " err" << std::endl;
}

struct MovableOnly
{
    MovableOnly() = default;
    
    MovableOnly(MovableOnly&&) = default;
    MovableOnly& operator=(MovableOnly&&) = default;
};

struct MovableCopyable
{
    MovableCopyable() = default;
    
    MovableCopyable(MovableCopyable&&) = default;
    MovableCopyable& operator=(MovableCopyable&&) = default;
    
    MovableCopyable(const MovableCopyable&) = default;
    MovableCopyable& operator=(const MovableCopyable&) = default;
};

void test_match_overloads_init_capture()
#ifdef HAS_CPP14_SUPPORT
{
    Either<Error, Response> rv;

    rv = Error{};

    rv.match([p = MovableOnly{}](auto&&) {});
    {
        auto lambda = [p = MovableCopyable{}](auto&&) {};
        rv.match(lambda);
    
        rv.match([p = MovableOnly{}](Response) { std::cout << "Response\n"; },
                 [p = MovableOnly{}](Error) { std::cout << "Error\n"; });
    }    
    {
        auto lambda = [](Error) { std::cout << "Error\n"; };
        rv.match([p = MovableOnly{}](Response) { std::cout << "Response\n"; },
                 lambda);
        rv.match(lambda,
                 [p = MovableOnly{}](Response) { std::cout << "Response\n"; });
    }
}
#else
{
}
#endif

// See #140
void test_match_overloads_otherwise()
#ifdef HAS_CPP14_SUPPORT
{

    struct Center
    {
    };
    struct Indent
    {
    };
    struct Justify
    {
    };
    struct None
    {
    };

    using Properties = mapbox::util::variant<Center, Indent, Justify, None>;

    Properties props = Justify{};

    props.match([&](Center) { std::cout << "Center\n"; },     //
                [&](Indent) { std::cout << "Indent\n"; },     //
                [&](auto&&) { std::cout << "Otherwise\n"; }); //
}
#else
{
}
#endif

template <typename>
struct Moveable
{
    Moveable() = default; // Default constructible

    Moveable(const Moveable&) = delete;            // Disable copy ctor
    Moveable& operator=(const Moveable&) = delete; // Disable copy assign op

    Moveable(Moveable&&) = default;            // Enable move ctor
    Moveable& operator=(Moveable&&) = default; // Enable move assign op
};

void test_match_move_out_of_variant()
{
    // Distinguishable at type level
    using T1 = Moveable<struct Tag1>;
    using T2 = Moveable<struct Tag2>;
    using T3 = mapbox::util::recursive_wrapper<int>;

    mapbox::util::variant<T1, T2> v = T1{};

    std::move(v).match([](T1&&) {},  // Consume T1 by value
                       [](T2&&) {}); // Consume T2 by value

    mapbox::util::variant<T3, T2> w = T2{};

    std::move(w).match([](int&&) {}, // Consume unwrapped int
                       [](T2&&) {}); // Consume T2 by value
}

int main()
{
    test_lambda_overloads();
    test_singleton_variant();
    test_call_nonconst_member();
    test_lambda_overloads_capture();
    test_lambda_overloads_sfinae();

    test_match_singleton();
    test_match_overloads();
    test_match_overloads_capture();
    test_match_overloads_init_capture();
    test_match_overloads_otherwise();
    test_match_move_out_of_variant();
}

#undef HAS_CPP14_SUPPORT