-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.hpp
357 lines (308 loc) · 9.59 KB
/
Signal.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#pragma once
#include <functional>
#include <map>
#include "NoCopy.hpp"
#include "VariadicTools.hpp"
namespace handy
{
namespace arch
{
using SignalConnectionId_t = int;
struct ISignalBase
{
virtual void disconnect(SignalConnectionId_t connection) = 0;
};
template <typename Ret_t, typename... Args_t>
//template <typename Ret_t, typename FirstArg_t, typename... MoreArgs_t>
class Signal; // Not used, but must exist to allow the specialised versions below.
struct SignalScopedConnection : public util::NoCopy
{
public:
SignalScopedConnection(ISignalBase& signal, SignalConnectionId_t id)
: signal(signal), id(id), doDisconnection(true) {}
SignalScopedConnection(SignalScopedConnection&& from)
: signal(from.signal), id(from.id), doDisconnection(true) {from.doDisconnection = false;}
~SignalScopedConnection()
{
// After being moved, the object being moved from's destructor will still
// be called and, without this flag, would still disconnect the signal.
if (doDisconnection)
{
signal.disconnect(id);
}
}
private:
ISignalBase& signal;
SignalConnectionId_t id;
bool doDisconnection;
};
// Single param version
template <typename Ret_t>
class Signal <Ret_t ()> : public handy::util::NoCopy, public ISignalBase
{
public:
using Callee_t = std::function<Ret_t()>;
Signal()
{
index = 0;
}
// Connect, and return the connection ID. You are responsible
// for disconnecting later using said ID, before the callback
// goes out of scope. Otherwise said callback shall still run
// which, seeing as it no longer exists, shall likely end poorly.
SignalConnectionId_t connect(Callee_t callback)
{
auto id = makeConnId();
callees[id] = callback;
return id;
}
// Make the connection and return a SignalScopedConnection.
// Said object will, upon its destruction, effect the disconnection of
// the connection automatically. This means that calling this function
// and not then assigning the result shall have no useful effect.
//TODO: Force assignment of return value??
SignalScopedConnection connectScoped(Callee_t callback)
{
auto id = makeConnId();
callees[id] = callback;
//SignalScopedConnection sig(*this, id);
return std::move(SignalScopedConnection(*this, id));
}
// Remove the callback with the specified ID, if it exists.
void disconnect(SignalConnectionId_t connectionId)
{
callees.erase(connectionId);
}
void call()
{
// Call all callbacks, provided they have a valid
// target (checked with operator bool() in std::function)
for (auto& callee : callees)
{
if (callee.second)
{
callee.second();
}
}
}
private:
SignalConnectionId_t makeConnId()
{
index++;
return index;
}
int index;
std::map<SignalConnectionId_t, Callee_t> callees;
};
// Yes the no param and n>=1 param versions are completely seperate I'm afraid
template <typename Ret_t, typename... Args_t>
class Signal <Ret_t (Args_t...)> : public handy::util::NoCopy, public ISignalBase
{
public:
using Callee_t = std::function<Ret_t(Args_t...)>;
using FirstArg_t = typename meta::VariadicTemplateInfo<Args_t...>::firstType;
Signal()
{
index = 0;
}
// Connect, and return the connection ID. You are responsible
// for disconnecting later using said ID, before the callback
// goes out of scope. Otherwise said callback shall still run
// which, seeing as it no longer exists, shall likely end poorly.
SignalConnectionId_t connect(Callee_t callback)
{
auto id = makeConnId();
callees[id] = callback;
return id;
}
// For signals with at least one argument, request callback only if the first argument is equal to "condition"
SignalConnectionId_t connectConditional(Callee_t callback, FirstArg_t condition)
{
ConditionalCallee cc = {callback, condition};
auto id = makeConnId();
conditionalCallees[id] = cc;
return id;
}
// Make the connection and return a SignalScopedConnection.
// Said object will, upon its destruction, effect the disconnection of
// the connection automatically. This means that calling this function
// and not then assigning the result shall have no useful effect.
SignalScopedConnection connectScoped(Callee_t callback)
{
auto id = makeConnId();
callees[id] = callback;
//SignalScopedConnection sig(*this, id);
return std::move(SignalScopedConnection(*this, id));
}
// Make a connection that is both conditional and scoped.
SignalScopedConnection connectConditionalScoped(Callee_t callback, FirstArg_t condition)
{
ConditionalCallee cc = {callback, condition};
auto id = makeConnId();
conditionalCallees[id] = cc;
return std::move(SignalScopedConnection(*this, id));
}
// Remove the callback with the specified ID, if it exists.
void disconnect(SignalConnectionId_t connectionId)
{
callees.erase(connectionId);
}
// template <class FA_t, class... MA_t>
// void call(FA_t firstArg, MA_t... moreArgs)
// {
// // Call all callbacks, provided they have a valid
// // target (checked with operator bool() in std::function)
// for (auto& callee : callees)
// {
// if (callee.second)
// {
// callee.second(firstArg, moreArgs...);
// }
// }
//
// for (auto& condCallee : conditionalCallees)
// {
// if (condCallee.second.callee)
// {
// if (condCallee.second.condition == firstArg)
// {
// condCallee.second.callee(firstArg, moreArgs...);
// }
// }
// }
// }
void call(Args_t... args)
{
// Call all callbacks, provided they have a valid
// target (checked with operator bool() in std::function)
for (auto& callee : callees)
{
if (callee.second)
{
callee.second(args...);
}
}
auto firstArg = meta::GetFirstVariadicValue(args...);
for (auto& condCallee : conditionalCallees)
{
if (condCallee.second.condition == firstArg)
{
if (condCallee.second.callee)
{
condCallee.second.callee(args...);
}
}
}
}
private:
SignalConnectionId_t makeConnId()
{
index++;
return index;
}
int index;
std::map<SignalConnectionId_t, Callee_t> callees;
struct ConditionalCallee
{
Callee_t callee;
typename meta::VariadicTemplateInfo<Args_t...>::firstType condition;
};
std::map<SignalConnectionId_t, ConditionalCallee> conditionalCallees;
};
// template <typename Ret_t, typename FirstArg_t, typename... MoreArgs_t>
// class Signal <Ret_t (FirstArg_t, MoreArgs_t...)> : public handy::util::NoCopy, public ISignalBase
// {
// public:
//
// using Callee_t = std::function<Ret_t(FirstArg_t, MoreArgs_t...)>;
// //using FirstArg_t = typename meta::VariadicTemplateInfo<Args_t...>::firstType;
//
// Signal()
// {
// index = 0;
// }
//
// // Connect, and return the connection ID. You are responsible
// // for disconnecting later using said ID, before the callback
// // goes out of scope. Otherwise said callback shall still run
// // which, seeing as it no longer exists, shall likely end poorly.
// SignalConnectionId_t connect(Callee_t callback)
// {
// auto id = makeConnId();
// callees[id] = callback;
// return id;
// }
//
// // For signals with at least one argument, request callback only if the first argument is equal to "condition"
// SignalConnectionId_t connectConditional(Callee_t callback, FirstArg_t condition)
// {
// ConditionalCallee cc = {callback, condition};
// auto id = makeConnId();
// conditionalCallees[id] = cc;
// return id;
// }
//
// // Make the connection and return a SignalScopedConnection.
// // Said object will, upon its destruction, effect the disconnection of
// // the connection automatically. This means that calling this function
// // and not then assigning the result shall have no useful effect.
// SignalScopedConnection connectScoped(Callee_t callback)
// {
// auto id = makeConnId();
// callees[id] = callback;
//
// //SignalScopedConnection sig(*this, id);
// return std::move(SignalScopedConnection(*this, id));
// }
//
// // Remove the callback with the specified ID, if it exists.
// void disconnect(SignalConnectionId_t connectionId)
// {
// callees.erase(connectionId);
// }
//
//// template <class FA_t, class... MA_t>
//// void call(FA_t firstArg, MA_t... moreArgs)
//// {
//// // Call all callbacks, provided they have a valid
//// // target (checked with operator bool() in std::function)
//// for (auto& callee : callees)
//// {
//// if (callee.second)
//// {
//// callee.second(firstArg, moreArgs...);
//// }
//// }
////
//// for (auto& condCallee : conditionalCallees)
//// {
//// if (condCallee.second.callee)
//// {
//// if (condCallee.second.condition == firstArg)
//// {
//// condCallee.second.callee(firstArg, moreArgs...);
//// }
//// }
//// }
//// }
//
// SignalConnectionId_t makeConnId()
// {
// index++;
// return index;
// }
//
// private:
// int index;
// std::map<SignalConnectionId_t, Callee_t> callees;
//
// struct ConditionalCallee
// {
// Callee_t callee;
// FirstArg_t condition;
// };
// std::map<SignalConnectionId_t, ConditionalCallee> conditionalCallees;
//
// };
}
}