-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathClosed-Class-Hierarchy.cpp
61 lines (54 loc) · 1.2 KB
/
Closed-Class-Hierarchy.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
#include "matchit.h"
#include <iostream>
struct Shape
{
enum class Kind
{
Circle,
Rectangle
} kind;
};
struct Circle : Shape
{
constexpr static Kind k = Kind::Circle;
Circle(int radius) : Shape{Shape::Kind::Circle}, radius(radius) {}
int radius;
};
struct Rectangle : Shape
{
constexpr static Kind k = Kind::Rectangle;
Rectangle(int width, int height)
: Shape{Shape::Kind::Rectangle}, width(width), height(height) {}
int width, height;
};
bool operator==(Circle const &lhs, Circle const &rhs)
{
return lhs.radius == rhs.radius;
}
bool operator==(Rectangle const &lhs, Rectangle const &rhs)
{
return lhs.width == rhs.width && lhs.height == rhs.height;
}
template <typename T>
auto get_if(Shape const *shape)
{
return static_cast<T const *>(shape->kind == T::k ? shape : nullptr);
}
double get_area(const Shape &shape)
{
using namespace matchit;
Id<Circle> c;
Id<Rectangle> r;
return match(shape)(
pattern |
as<Circle>(c) = [&]
{ return 3.14 * (*c).radius * (*c).radius; },
pattern | as<Rectangle>(r) = [&]
{ return (*r).width * (*r).height; });
}
int32_t main()
{
Rectangle r = {5, 7};
std::cout << get_area(r) << std::endl;
return 0;
}