-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDestructuring-Enums.cpp
44 lines (41 loc) · 1.09 KB
/
Destructuring-Enums.cpp
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
#include "matchit.h"
#include <iostream>
#include <optional>
#include <stack>
struct Quit
{
};
using Move = std::array<int32_t, 2>;
using Write = std::string;
using ChangeColor = std::array<int32_t, 3>;
using Message = std::variant<Quit, Move, Write, ChangeColor>;
int32_t main()
{
Message const msg = ChangeColor{0, 160, 255};
using namespace matchit;
Id<int32_t> x, y;
Id<std::string> text;
Id<int32_t> r, g, b;
match(msg)(
pattern | as<Quit>(_) =
[]
{
std::cout << "The Quit variant has no data to destructure."
<< std::endl;
},
pattern | as<Move>(ds(x, y)) =
[&]
{
std::cout << "Move in the x direction " << *x
<< " and in the y direction " << *y << std::endl;
},
pattern | as<Write>(text) =
[&]
{ std::cout << "Text message: " << *text << std::endl; },
pattern | as<ChangeColor>(ds(r, g, b)) =
[&]
{
std::cout << "Change the color to red " << *r << ", green " << *g
<< ", and blue " << *b << std::endl;
});
}