-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_signal.cpp
253 lines (185 loc) · 7.6 KB
/
simple_signal.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
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
// This is an advanced example of making simple signal with macroes and template meta programming
// Please Note that this code has copied from Adrien Hamelin's blog
// https://aboutcpp.wordpress.com/2015/01/16/an-update-on-a-simple-signal-system/
#include <_regex.h>
// signal.h --------------------------------------------------------------------
//#pragma once
// #ifndef SIGNAL_SIGNAL_H_
// #define SIGNAL_SIGNAL_H_
#include <functional>
#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
template<typename EmitterType, int type, typename... Args>
class signal_can_be_sent : public std::false_type {};
// verification is done only at Object level, not at All or Type
#define DECLARE_SIGNAL(...) \
template <> \
class signal_can_be_sent<__VA_ARGS__> : public std::true_type {};
namespace Signal {
#define CLASS_DECLARATION(name, ...) \
protected: \
static std::vector<std::pair<unsigned int, std::function<void (__VA_ARGS__)> > > name; \
\
public: \
static ID connect(std::function<void (__VA_ARGS__)> function); \
static void disconnect(ID i);
#define CONNECT_IMPL(name) \
if (name.empty()) \
name.emplace_back(0u, std::move(function)); \
else \
name.emplace_back(name.back().first + 1u, std::move(function)); \
\
return name.back().first;
#define DISCONNECT_IMPL(name) \
auto const end = name.cend(); \
\
for (auto iter = name.cbegin(); iter != end; ++iter) { \
if (iter->first == i) { \
name.erase(iter); \
return; \
} \
} \
std::cout << "Signal was not connected " << i << std::endl;
using ID = unsigned int;
// catch every signal
class All {
CLASS_DECLARATION(m_allReceivers, int)
};
std::vector<std::pair<unsigned int, std::function<void(int)> > > All::m_allReceivers;
// catch every signal of given type
template<__unused int type>
class Type : public All {
CLASS_DECLARATION(m_typeReceivers, int)
};
template<int type>
std::vector<std::pair<unsigned int, std::function<void(int)> > > Type<type>::m_typeReceivers;
template<typename EmitterType, int type, typename... Args>
class Object : public Type<type> {
static_assert(signal_can_be_sent<EmitterType, type, Args...>::value, "The signal has not been declared.");
private:
static std::unordered_map<EmitterType const *,
std::vector<std::pair<unsigned int, std::function<void(int, EmitterType *, Args...)> > > >
m_objectReceivers;
public:
// connect the function to receive Type events from the EmitterType
// returns the id used to be able to disconnect
static ID connect(std::function<void(int, EmitterType *, Args...)> function);
static ID connect(EmitterType const *instance, std::function<void(int, EmitterType *, Args...)> function);
static void disconnect(ID i);
static void disconnect(EmitterType const *instance, ID i);
static void send(EmitterType *instance, Args const &... args);
};
template<typename EmitterType, int type, typename... Args>
std::unordered_map<EmitterType const *,
std::vector<std::pair<unsigned int, std::function<void(int, EmitterType *, Args...)> > > >
Object<EmitterType, type, Args...>::m_objectReceivers;
template<int type, typename EmitterType, typename... Args>
inline void send(EmitterType *emitter, Args const &... args) {
Object<EmitterType, type, Args...>::send(emitter, args...);
}
//// Public /////////////////////////////////////////////////////////////////
ID All::connect(std::function<void(int)> function) {
CONNECT_IMPL (m_allReceivers);
}
void All::disconnect(ID i) {
DISCONNECT_IMPL (m_allReceivers);
}
template<int type>
ID Type<type>::connect(std::function<void(int)> function) {
CONNECT_IMPL (m_typeReceivers);
}
template<int type>
void Type<type>::disconnect(ID i) {
DISCONNECT_IMPL (m_typeReceivers);
}
template<typename EmitterType, int type, typename... Args>
inline ID Object<EmitterType, type, Args...>::connect(std::function<void(int, EmitterType *, Args...)> function) {
return connect(nullptr, std::move(function));
}
template<typename EmitterType, int type, typename... Args>
ID Object<EmitterType, type, Args...>::connect(EmitterType const *instance,
std::function<void(int, EmitterType *, Args...)> function) {
auto &vector = m_objectReceivers[instance];
CONNECT_IMPL(vector)
}
template<typename EmitterType, int type, typename... Args>
inline void Object<EmitterType, type, Args...>::disconnect(ID i) {
disconnect(nullptr, i);
}
template<typename EmitterType, int type, typename... Args>
inline void Object<EmitterType, type, Args...>::disconnect(EmitterType const *instance, ID i) {
auto const iter = m_objectReceivers.find(instance);
if (iter != m_objectReceivers.end()) {
auto &vector = iter->second;
DISCONNECT_IMPL(vector);
}
}
template<typename EmitterType, int type, typename... Args>
void Object<EmitterType, type, Args...>::send(EmitterType *instance, Args const &... args) {
for (auto const &function: All::m_allReceivers)
function.second(type);
for (auto const &function: Type<type>::m_typeReceivers)
function.second(type);
auto const end = m_objectReceivers.cend();
// dispatch the signal for all those that do not care about the instance
auto iter = m_objectReceivers.find(nullptr);
if (iter != end) {
for (auto const &function: iter->second)
function.second(type, instance, args...);
}
// then for those that do
iter = m_objectReceivers.find(instance);
if (iter != end) {
for (auto const &function: iter->second)
function.second(type, instance, args...);
}
}
//// Protected //////////////////////////////////////////////////////////////
//// Private ////////////////////////////////////////////////////////////////
//// Functions //////////////////////////////////////////////////////////////
}
// #endif // SIGNAL_SIGNAL_H_
// signal.h --------------------------------------------------------------------
enum : int {
EXEMPLE_SIGNAL = 0,
EXEMPLE_SIGNAL_2,
NEVER_SENT_EXEMPLE
};
class Exemple {};
DECLARE_SIGNAL(Exemple, EXEMPLE_SIGNAL, int)
DECLARE_SIGNAL(Exemple, EXEMPLE_SIGNAL_2, float)
int main() {
Exemple ex1;
Exemple ex2;
Signal::Object<Exemple, EXEMPLE_SIGNAL, int>::connect(&ex1, [](int type, Exemple *, int value) {
std::cout << "1 - Received object int " << type << " " << value << std::endl;
});
Signal::Object<Exemple, EXEMPLE_SIGNAL_2, float>::connect(&ex1, [](int type, Exemple *, float value) {
std::cout << "2 - Received object float " << type << " " << value << std::endl;
});
Signal::Object<Exemple, EXEMPLE_SIGNAL, int>::connect([](int type, Exemple *, int value) {
std::cout << "3 - Received object float " << type << " " << value << std::endl;
});
Signal::Type<EXEMPLE_SIGNAL>::connect([](int type) {
std::cout << "4 - Received type " << type << std::endl;
});
Signal::All::connect([](int type) {
std::cout << "5 - Received all " << type << std::endl;
});
// uncomment for error
// Signal::Object<Exemple, EXEMPLE_SIGNAL, float>::connect(&ex1, [] (int type, Exemple *, float value) {
// std::cout << "Never received " << type << " " << value << std::endl;
// });
Signal::Type<NEVER_SENT_EXEMPLE>::connect([](int type) {
std::cout << "Never received type " << type << std::endl;
});
Signal::send<EXEMPLE_SIGNAL>(&ex1, 42); // received by all except 2
std::cout << std::endl;
Signal::send<EXEMPLE_SIGNAL>(&ex2, 43); // received by 3, 4 and 5
std::cout << std::endl;
Signal::send<EXEMPLE_SIGNAL_2>(&ex1, 44.4f); // received by 2 and 5
// Signal::send<EXEMPLE_SIGNAL_2>(&ex1, 44); // error
return 0;
}