forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_event.cpp
137 lines (118 loc) · 3.25 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
#include "solid/utility/event.hpp"
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>;
const GlobalEventCategory global_event_category{
"::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";
}
}
};
const AlphaEventCategory alpha_event_category{
"::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";
}
}
};
const BetaEventCategory beta_event_category{
"::alpha_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(Event &&_revt) = 0;
};
class Object: public Base{
private:
/*virtual*/ void handleEvent(Event &&_revt)override;
};
void Object::handleEvent(Event &&_revt){
static const EventHandler<void, Object&> event_handler = {
[](Event &_revt, Object &_robj){cout<<"handle_invalid_event on "<<&_robj<<" for "<<_revt<<endl;},
{
{
global_event_category.event(GlobalEvents::First),
[](Event &_revt, Object &_robj){cout<<"handle_global_first on "<<&_robj<<" for "<<_revt<<endl;}
},
{
alpha_event_category.event(AlphaEvents::First),
[](Event &_revt, Object &_robj){cout<<"handle_alpha_first on "<<&_robj<<" for "<<_revt<<endl;}
},
{
alpha_event_category.event(AlphaEvents::Second),
[](Event &_revt, Object &_robj){cout<<"handle_alpha_second on "<<&_robj<<" for "<<_revt<<endl;}
},
{
beta_event_category.event(BetaEvents::First),
[](Event &_revt, Object &_robj){cout<<"handle_beta_first on "<<&_robj<<" for "<<_revt<<endl;}
},
{
beta_event_category.event(BetaEvents::Third),
[](Event &_revt, Object &_robj){cout<<"handle_beta_third on "<<&_robj<<" for "<<_revt<<endl;}
},
{
generic_event_category.event(GenericEvents::Message),
[](Event &_revt, Object &_robj){cout<<"handle_generic_message on "<<&_robj<<" for "<<_revt<<" any value = "<<*_revt.any().cast<std::string>()<<endl;}
}
}
};
event_handler.handle(_revt, *this);
}
int test_event(int argc, char *argv[]){
cout<<"Event::any_size = "<<Event::any_size<<endl;
Object obj;
Base &rbase(obj);
rbase.handleEvent(global_event_category.event(GlobalEvents::First));
rbase.handleEvent(alpha_event_category.event(AlphaEvents::Second));
rbase.handleEvent(beta_event_category.event(BetaEvents::Third));
rbase.handleEvent(beta_event_category.event(BetaEvents::Second));
rbase.handleEvent(generic_event_category.event(GenericEvents::Message, std::string("Some text")));
return 0;
}