-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_t.hpp
239 lines (188 loc) · 6.32 KB
/
signal_t.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
#ifndef signal_t_h
#define signal_t_h
#include <map>
#include <functional>
#include <vector>
#include <future>
#include <atomic>
#include <mutex>
#include <type_traits>
namespace signals{
namespace{
template <typename... slot_signature_t>
class signal_base;
template <typename return_t,typename... args_t>
class signal_base<return_t(args_t...)>{
public:
using return_type = return_t;
using result_type = std::vector<return_type>;
using slot_t = std::function<return_t(args_t...)>;
using slot_id_t = std::size_t;
signal_base():_current_id(0){}
signal_base(signal_base const& other):_slots(other._slots),_current_id(other._current_id){}
signal_base(signal_base&& other):_slots(),_current_id(0)
{
std::swap(_slots,other._slots);
std::swap(_current_id,other._current_id);
}
signal_base& operator=(signal_base other)
{
std::swap(_slots,other._slots);
std::swap(_current_id,other._current_id);
return *this;
}
signal_base& operator=(signal_base&& other)
{
if(this != &other)
{
std::swap(_slots,other._slots);
std::swap(_current_id,other._current_id);
}
return *this;
}
slot_id_t connect(slot_t const& slot)
{
slot_id_t out;
{
std::unique_lock<std::mutex> lk{_slots_mtx};
_slots.emplace(_current_id,slot);
out = _current_id++;
}
return out;
}
template<typename F,typename... a_t>
slot_id_t attach(F&& f, a_t&&... args)
{
return connect([&](args_t... a)
{
return f(args...,a...);
});
}
slot_t disconnect(slot_id_t const& id)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
if( ! ( id > _current_id -1) )
{
slot_t s =_slots.at(id);
_slots.erase(id);
return s;
}else return nullptr;
}
void disconnect_all()
{
std::unique_lock<std::mutex> lk{_slots_mtx};
_slots.clear();
}
template<typename thread_pool>
std::vector<std::future<return_t>> emit(thread_pool& pool, args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
std::vector<std::future<return_t>> out(_slots.size());
for (auto&& it:_slots)
{
out[it.first]=std::move(pool.push(it.second, p...));
}
return out;
}
template<typename thread_pool>
std::future<return_t> emit(thread_pool& pool, slot_id_t target,args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
return pool.push(_slots[target], p...);
}
protected:
std::map<slot_id_t, slot_t>_slots;
slot_id_t _current_id;
std::mutex _slots_mtx;
};
}
template <typename... slot_signature_t>
class signal_t;
template <typename... args_t>
class signal_t<void(args_t...)> : signal_base<void(args_t...)> {
public:
using base_type = signal_base<void(args_t...)>;
using return_type = typename base_type::return_type;
using result_type = typename base_type::result_type;
using slot_t = typename base_type::slot_t;
using slot_id_t = typename base_type::slot_id_t;
using base_type::base_type;
using base_type::operator=;
using base_type::connect;
using base_type::attach;
using base_type::disconnect;
using base_type::disconnect_all;
using base_type::emit;
//calls all slots synchronously
void emit(args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
for(auto&& it :_slots)
{
it.second(p...);
}
}
void emit(slot_id_t target, args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
_slots[target](p...);
}
void operator()(args_t...p)
{
emit(p...);
}
void operator()(slot_id_t target, args_t... p)
{
emit(target,p...);
}
protected:
using base_type::_slots;
using base_type::_current_id;
using base_type::_slots_mtx;
};
template <typename return_t,typename... args_t>
class signal_t<return_t(args_t...)> : signal_base<return_t(args_t...)>
{
public:
using base_type = signal_base<return_t(args_t...)>;
using return_type = typename base_type::return_type;
using result_type = typename base_type::result_type;
using slot_t = typename base_type::slot_t;
using slot_id_t = typename base_type::slot_id_t;
using base_type::base_type;
using base_type::operator=;
using base_type::connect;
using base_type::disconnect;
using base_type::disconnect_all;
using base_type::emit;
//calls all slots synchronously
result_type emit(args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
result_type out(_slots.size());
for(auto&& it :_slots)
{
out[it.first]=it.second(p...);
}
return out;
}
return_t emit(slot_id_t target, args_t... p)
{
std::unique_lock<std::mutex> lk{_slots_mtx};
return _slots[target](p...);
}
return_t operator()(args_t... p)
{
return emit(p...);
}
return_t operator()(slot_id_t target, args_t... p)
{
return emit(target,p...);
}
protected:
using base_type::_slots;
using base_type::_current_id;
using base_type::_slots_mtx;
};
}
#endif /* signal_t_h */