-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.hpp
208 lines (171 loc) · 6.36 KB
/
event.hpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
The MIT License (MIT)
Copyright (c) 2012-2014 Erik Soma
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef EVENT_HPP
#define EVENT_HPP
// standard library
#include <functional>
#include <list>
#include <memory>
#include <set>
/*
Events allow for multiple functions to be executed in response to an
Event having been fired. Events can be fired at any time, causing all
of their bound functions to immediatley execute. Functions can be bound
and unbound at any time, even "during" the firing of an Event. Events are
typed such that the arguments must match the arguments defined for the
Event.
*/
template <typename... Args>
class Event
{
public:
typedef std::function<void(Args...)> Function;
private:
typedef std::list<std::shared_ptr<Function>> FunctionList;
typedef std::list<std::weak_ptr<Function>> WeakFunctionList;
public:
/*
An object that has ownership of the bind to an Event. When the Bind
is destroyed it will automatically unbind the function that was
bound. This is useful for automatically cleaning up Events with
RAII.
*/
class Bind
{
public:
/*
Destructor
=============================================================*/
~Bind()
{
if (this->is_valid)
{
assert(
this->event.binds.find(this) !=
this->event.binds.end()
);
this->event.binds.erase(this);
this->event.bound_functions.erase(
this->bound_function_iterator
);
}
}
private:
friend class Event<Args...>;
/*
Constructor
=============================================================*/
Bind(
Event& event,
typename FunctionList::iterator bound_function_iterator
):
event(event),
bound_function_iterator(bound_function_iterator),
is_valid(true)
{
}
void invalidate()
{
assert(this->is_valid);
this->is_valid = false;
}
Event& event;
typename FunctionList::iterator bound_function_iterator;
bool is_valid;
};
/*
Constructor
=====================================================================*/
Event()
{
}
/*
Destructor
=====================================================================*/
~Event()
{
// Invalidate any remaining Binds.
for(auto bind: this->binds)
{
bind->invalidate();
}
}
/*
permanent_bind
Permanently binds a function to the Event. Useful for instances in
which the bound function will never become invalid within the
lifetime of the Event.
=====================================================================*/
void permanent_bind(const Function& function)
{
this->bound_functions.emplace(
this->bound_functions.end(),
std::make_shared<Function>(function)
);
}
/*
bind
Binds a function to the Event for the duration of the Bind instance
returned.
=====================================================================*/
std::shared_ptr<Bind> bind(const Function& function)
{
this->bound_functions.emplace(
this->bound_functions.end(),
std::make_shared<Function>(function)
);
auto bound_function_iterator = this->bound_functions.end();
--bound_function_iterator;
std::shared_ptr<Bind> bind(new Bind(
*this,
bound_function_iterator
));
assert(this->binds.find(bind.get()) == this->binds.end());
this->binds.insert(bind.get());
return bind;
}
/*
fire
Executes all bound functions using the arguments provided.
*/
void fire(Args... args)
{
WeakFunctionList weak_functions;
for(auto& shared_ptr: this->bound_functions)
{
weak_functions.emplace(
weak_functions.end(),
shared_ptr
);
}
for(auto& weak_ptr: weak_functions)
{
if (auto shared_ptr = weak_ptr.lock())
{
(*shared_ptr)(args...);
}
}
}
private:
friend class Bind;
FunctionList bound_functions;
std::set<Bind*> binds;
};
#endif