| 12 | using Message = std::variant<Quit, Move, Write, ChangeColor>; |
| 13 | |
| 14 | int32_t main() |
| 15 | { |
| 16 | Message const msg = ChangeColor{0, 160, 255}; |
| 17 | |
| 18 | using namespace matchit; |
| 19 | Id<int32_t> x, y; |
| 20 | Id<std::string> text; |
| 21 | Id<int32_t> r, g, b; |
| 22 | match(msg)( |
| 23 | pattern | as<Quit>(_) = |
| 24 | [] |
| 25 | { |
| 26 | std::cout << "The Quit variant has no data to destructure." |
| 27 | << std::endl; |
| 28 | }, |
| 29 | pattern | as<Move>(ds(x, y)) = |
| 30 | [&] |
| 31 | { |
| 32 | std::cout << "Move in the x direction " << *x |
| 33 | << " and in the y direction " << *y << std::endl; |
| 34 | }, |
| 35 | pattern | as<Write>(text) = |
| 36 | [&] |
| 37 | { std::cout << "Text message: " << *text << std::endl; }, |
| 38 | pattern | as<ChangeColor>(ds(r, g, b)) = |
| 39 | [&] |
| 40 | { |
| 41 | std::cout << "Change the color to red " << *r << ", green " << *g |
| 42 | << ", and blue " << *b << std::endl; |
| 43 | }); |
| 44 | } |