-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_event.cpp
155 lines (131 loc) · 4.64 KB
/
test_event.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "solid/utility/event.hpp"
#include <set>
#include <vector>
using namespace std;
using namespace solid;
enum struct GlobalEvents {
First,
Second,
Third,
};
enum struct AlphaEvents {
First,
Second,
Third,
};
enum struct BetaEvents {
First,
Second,
Third,
};
using GlobalEventCategory = EventCategory<GlobalEvents>;
using AlphaEventCategory = EventCategory<AlphaEvents>;
using BetaEventCategory = EventCategory<BetaEvents>;
template <>
const GlobalEventCategory solid::category<GlobalEvents>{
"::global_event_category",
[](const GlobalEvents _evt) {
switch (_evt) {
case GlobalEvents::First:
return "first";
case GlobalEvents::Second:
return "second";
case GlobalEvents::Third:
return "third";
default:
return "unknown";
}
}};
template <>
const AlphaEventCategory solid::category<AlphaEvents>{
"::alpha_event_category",
[](const AlphaEvents _evt) {
switch (_evt) {
case AlphaEvents::First:
return "first";
case AlphaEvents::Second:
return "second";
case AlphaEvents::Third:
return "third";
default:
return "unknown";
}
}};
template <>
const BetaEventCategory solid::category<BetaEvents>{
"::beta_event_category",
[](const BetaEvents _evt) {
switch (_evt) {
case BetaEvents::First:
return "first";
case BetaEvents::Second:
return "second";
case BetaEvents::Third:
return "third";
default:
return "unknown";
}
}};
class Base {
public:
virtual ~Base() {}
virtual void handleEvent(EventBase&& _revt) = 0;
};
class Object : public Base {
private:
/*virtual*/ void handleEvent(EventBase&& _revt) override;
};
void Object::handleEvent(EventBase&& _revt)
{
static const EventHandler<void, Object&> event_handler = {
[](EventBase& _revt, Object& _robj) { cout << "handle_invalid_event on " << &_robj << " for " << _revt << endl; },
{{make_event(GlobalEvents::First),
[](EventBase& _revt, Object& _robj) { cout << "handle_global_first on " << &_robj << " for " << _revt << endl; }},
{make_event(AlphaEvents::First),
[](EventBase& _revt, Object& _robj) { cout << "handle_alpha_first on " << &_robj << " for " << _revt << endl; }},
{make_event(AlphaEvents::Second),
[](EventBase& _revt, Object& _robj) { cout << "handle_alpha_second on " << &_robj << " for " << _revt << endl; }},
{make_event(BetaEvents::First),
[](EventBase& _revt, Object& _robj) { cout << "handle_beta_first on " << &_robj << " for " << _revt << endl; }},
{make_event(BetaEvents::Third),
[](EventBase& _revt, Object& _robj) { cout << "handle_beta_third on " << &_robj << " for " << _revt << endl; }},
{generic_event<GenericEventE::Message>,
[](EventBase& _revt, Object& _robj) { cout << "handle_generic_message on " << &_robj << " for " << _revt << " data = " << *_revt.cast<std::string>() << endl; }}}};
event_handler.handle(_revt, *this);
}
int test_event(int /*argc*/, char* /*argv*/[])
{
Object obj;
Base& rbase(obj);
rbase.handleEvent(make_event(GlobalEvents::First));
rbase.handleEvent(make_event(AlphaEvents::Second));
rbase.handleEvent(make_event(BetaEvents::Third));
rbase.handleEvent(make_event(BetaEvents::Second));
rbase.handleEvent(make_event(GenericEventE::Message, std::string("Some text")));
auto tuple_event = make_event(BetaEvents::Second, std::make_tuple(to_string(10), vector<int>{1, 2, 3, 4}, set<std::string>{"a", "b", "c"}));
auto check_event = [](EventBase& _event) {
solid_check(_event.get_if<string>());
solid_check(*_event.get_if<string>() == to_string(10));
auto* pvec = _event.get_if<vector<int>>();
solid_check(pvec && pvec->size() == 4 && pvec->at(3) == 4);
auto* pset = _event.get_if<set<string>>();
solid_check(pset && pset->size() == 3 && pset->find("b") != pset->end());
};
check_event(tuple_event);
Event<8> tuple_event8(tuple_event);
check_event(tuple_event8);
check_event(tuple_event);
{
Event<8> tuple_event8(std::move(tuple_event));
check_event(tuple_event8);
solid_check(!tuple_event);
}
tuple_event = tuple_event8;
check_event(tuple_event8);
check_event(tuple_event);
Event<10> tuple_event10;
tuple_event10 = std::move(tuple_event);
solid_check(!tuple_event);
check_event(tuple_event10);
return 0;
}