forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation.cpp
63 lines (54 loc) · 1.01 KB
/
mutation.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "matchit.h"
#include <variant>
#include <iostream>
class Circle {
public:
void setLineWidth(float w)
{
std::cout << "Calling Circle::setLineWidth with w = " << w << std::endl;
};
// other stuff
};
class Square {
public:
void setLineWidth(float)
{
// set line width
};
// other stuff
};
class Image {
// other stuff
};
auto operator==(Circle const&, Circle const&)
{
return true;
}
auto operator==(Square const&, Square const&)
{
return true;
}
using Visual = std::variant<Circle, Square, Image>;
using namespace matchit;
void setLineWidth(Visual &visual, float width) {
Id<Square&> sq;
Id<Circle&> cir;
match(visual)
(
pattern | as<Image>(_) = []{},
pattern | as<Square>(sq) = [&]
{
(*sq).setLineWidth(width);
},
pattern | as<Circle>(cir) = [&]
{
(*cir).setLineWidth(width);
}
);
}
int main()
{
Visual v = Circle{};
setLineWidth(v, 1);
return 0;
}