-
Notifications
You must be signed in to change notification settings - Fork 212
/
eventdispatcher.h
328 lines (268 loc) · 8.37 KB
/
eventdispatcher.h
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// eventpp library
// Copyright (C) 2018 Wang Qi (wqking)
// Github: https://github.com/wqking/eventpp
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef EVENTDISPATCHER_H_319010983013
#define EVENTDISPATCHER_H_319010983013
#include "callbacklist.h"
namespace eventpp {
namespace internal_ {
template <
typename EventType_,
typename Prototype_,
typename Policies_,
typename MixinRoot_
>
class EventDispatcherBase;
template <
typename EventType_,
typename Policies_,
typename MixinRoot_,
typename ReturnType, typename ...Args
>
class EventDispatcherBase <
EventType_,
ReturnType (Args...),
Policies_,
MixinRoot_
>
{
protected:
using ThisType = EventDispatcherBase<
EventType_,
ReturnType (Args...),
Policies_,
MixinRoot_
>;
using MixinRoot = typename std::conditional<
std::is_same<MixinRoot_, void>::value,
ThisType,
MixinRoot_
>::type;
using Policies = Policies_;
using Threading = typename SelectThreading<Policies_, HasTypeThreading<Policies_>::value>::Type;
using ArgumentPassingMode = typename SelectArgumentPassingMode<
Policies_,
HasTypeArgumentPassingMode<Policies_>::value,
ArgumentPassingAutoDetect
>::Type;
using Callback_ = typename SelectCallback<
Policies_,
HasTypeCallback<Policies_>::value,
std::function<ReturnType (Args...)>
>::Type;
using CallbackList_ = CallbackList<ReturnType (Args...), Policies_>;
using Prototype = ReturnType (Args...);
using Map = typename SelectMap<
EventType_,
CallbackList_,
Policies_,
HasTemplateMap<Policies_>::value
>::Type;
using Mixins = typename internal_::SelectMixins<
Policies_,
internal_::HasTypeMixins<Policies_>::value
>::Type;
public:
using Handle = typename CallbackList_::Handle;
using Callback = Callback_;
using Event = EventType_;
using Mutex = typename Threading::Mutex;
public:
EventDispatcherBase()
:
eventCallbackListMap(),
listenerMutex()
{
}
EventDispatcherBase(const EventDispatcherBase & other)
:
eventCallbackListMap(other.eventCallbackListMap),
listenerMutex()
{
}
EventDispatcherBase(EventDispatcherBase && other) noexcept
:
eventCallbackListMap(std::move(other.eventCallbackListMap)),
listenerMutex()
{
}
EventDispatcherBase & operator = (const EventDispatcherBase & other)
{
eventCallbackListMap = other.eventCallbackListMap;
return *this;
}
EventDispatcherBase & operator = (EventDispatcherBase && other) noexcept
{
eventCallbackListMap = std::move(other.eventCallbackListMap);
return *this;
}
void swap(EventDispatcherBase & other) noexcept {
using std::swap;
swap(eventCallbackListMap, other.eventCallbackListMap);
}
Handle appendListener(const Event & event, const Callback & callback)
{
std::lock_guard<Mutex> lockGuard(listenerMutex);
return eventCallbackListMap[event].append(callback);
}
Handle prependListener(const Event & event, const Callback & callback)
{
std::lock_guard<Mutex> lockGuard(listenerMutex);
return eventCallbackListMap[event].prepend(callback);
}
Handle insertListener(const Event & event, const Callback & callback, const Handle & before)
{
std::lock_guard<Mutex> lockGuard(listenerMutex);
return eventCallbackListMap[event].insert(callback, before);
}
bool removeListener(const Event & event, const Handle handle)
{
CallbackList_ * callableList = doFindCallableList(event);
if(callableList) {
return callableList->remove(handle);
}
return false;
}
bool hasAnyListener(const Event & event) const
{
const CallbackList_ * callableList = doFindCallableList(event);
if(callableList) {
return ! callableList->empty();
}
return false;
}
bool ownsHandle(const Event & event, const Handle & handle) const
{
const CallbackList_ * callableList = doFindCallableList(event);
if(callableList) {
return callableList->ownsHandle(handle);
}
return false;
}
template <typename Func>
void forEach(const Event & event, Func && func) const
{
const CallbackList_ * callableList = doFindCallableList(event);
if(callableList) {
callableList->forEach(std::forward<Func>(func));
}
}
template <typename Func>
bool forEachIf(const Event & event, Func && func) const
{
const CallbackList_ * callableList = doFindCallableList(event);
if (callableList) {
return callableList->forEachIf(std::forward<Func>(func));
}
return true;
}
void dispatch(Args ...args) const
{
static_assert(ArgumentPassingMode::canIncludeEventType, "Dispatching arguments count doesn't match required (Event type should be included).");
using GetEvent = typename SelectGetEvent<Policies_, EventType_, HasFunctionGetEvent<Policies_, Args...>::value>::Type;
// can't std::forward<Args>(args) in GetEvent::getEvent because the pass by value arguments will be moved to getEvent
// then the other std::forward<Args>(args) to directDispatch will get empty values.
directDispatch(
GetEvent::getEvent(args...),
std::forward<Args>(args)...
);
}
template <typename T>
void dispatch(T && first, Args ...args) const
{
static_assert(ArgumentPassingMode::canExcludeEventType, "Dispatching arguments count doesn't match required (Event type should NOT be included).");
using GetEvent = typename SelectGetEvent<Policies_, EventType_, HasFunctionGetEvent<Policies_, T &&, Args...>::value>::Type;
directDispatch(
GetEvent::getEvent(std::forward<T>(first), args...),
std::forward<Args>(args)...
);
}
// Bypass any getEvent policy. The first argument is the event type.
// Most used for internal purpose.
void directDispatch(const Event & e, Args ...args) const
{
if(! internal_::ForEachMixins<MixinRoot, Mixins, DoMixinBeforeDispatch>::forEach(
this, typename std::add_lvalue_reference<Args>::type(args)...)) {
return;
}
const CallbackList_ * callableList = doFindCallableList(e);
if(callableList) {
(*callableList)(std::forward<Args>(args)...);
}
}
protected:
const CallbackList_ * doFindCallableList(const Event & e) const
{
return doFindCallableListHelper(this, e);
}
CallbackList_ * doFindCallableList(const Event & e)
{
return doFindCallableListHelper(this, e);
}
private:
// template helper to avoid code duplication in doFindCallableList
template <typename T>
static auto doFindCallableListHelper(T * self, const Event & e)
-> typename std::conditional<std::is_const<T>::value, const CallbackList_ *, CallbackList_ *>::type
{
std::lock_guard<Mutex> lockGuard(self->listenerMutex);
auto it = self->eventCallbackListMap.find(e);
if(it != self->eventCallbackListMap.end()) {
return &it->second;
}
else {
return nullptr;
}
}
private:
// Mixin related
struct DoMixinBeforeDispatch
{
template <typename T, typename Self, typename ...A>
static auto forEach(const Self * self, A && ...args)
-> typename std::enable_if<HasFunctionMixinBeforeDispatch<T, A...>::value, bool>::type {
return static_cast<const T *>(self)->mixinBeforeDispatch(std::forward<A>(args)...);
}
template <typename T, typename Self, typename ...A>
static auto forEach(const Self * /*self*/, A && ... /*args*/)
-> typename std::enable_if<! HasFunctionMixinBeforeDispatch<T, A...>::value, bool>::type {
return true;
}
};
private:
Map eventCallbackListMap;
mutable Mutex listenerMutex;
};
} //namespace internal_
template <
typename Event_,
typename Prototype_,
typename Policies_ = DefaultPolicies
>
class EventDispatcher : public internal_::InheritMixins<
internal_::EventDispatcherBase<Event_, Prototype_, Policies_, void>,
typename internal_::SelectMixins<Policies_, internal_::HasTypeMixins<Policies_>::value >::Type
>::Type, public TagEventDispatcher
{
private:
using super = typename internal_::InheritMixins<
internal_::EventDispatcherBase<Event_, Prototype_, Policies_, void>,
typename internal_::SelectMixins<Policies_, internal_::HasTypeMixins<Policies_>::value >::Type
>::Type;
public:
using super::super;
friend void swap(EventDispatcher & first, EventDispatcher & second) noexcept {
first.swap(second);
}
};
} //namespace eventpp
#endif