-
Notifications
You must be signed in to change notification settings - Fork 0
/
v03.cpp
76 lines (63 loc) · 1.36 KB
/
v03.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
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "fluff.hpp"
namespace detail
{
template <typename Fn>
class DefaultCase
{
public:
explicit DefaultCase(Fn fn) : fn(fn) {}
Fn getFn() const { return fn; }
private:
Fn fn;
};
template <typename Result>
Result switchAnyImpl(boost::any& a) {
assert(false && "No case matched.");
std::terminate();
}
template <typename Result, typename Case, typename... Cases>
Result switchAnyImpl(boost::any& a, Case c, Cases... cases) {
return c.getFn()();
}
}
template <typename Result, typename... Cases>
Result switchAny(boost::any& a, Cases... cases)
{
return detail::switchAnyImpl<Result>(a, cases...);
}
template <typename Fn>
detail::DefaultCase<Fn> defaultCase(Fn fn)
{
return detail::DefaultCase<Fn>(fn);
}
template <typename Fn>
detail::DefaultCase<Fn> emptyCase(Fn fn)
{
return detail::DefaultCase<Fn>(fn);
}
TEST(SwitchAny, usesDefaultCase)
{
boost::any a;
int result = switchAny<int>(a,
defaultCase([] { return 1; })
);
EXPECT_EQ(1, result);
}
TEST(SwitchAny, usesEmptyCaseForEmpty)
{
boost::any a;
int result = switchAny<int>(a,
emptyCase([] { return 2; }),
defaultCase([] { return 1; })
);
EXPECT_EQ(2, result);
}
TEST(SwitchAny, skipsEmptyCaseForNonEmpty)
{
boost::any a = 1;
int result = switchAny<int>(a,
emptyCase([] { return 2; }),
defaultCase([] { return 1; })
);
EXPECT_EQ(1, result);
}