-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.hpp
217 lines (175 loc) · 4.74 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
#ifndef WAYROUND_I2P_20241223_224139_493806
#define WAYROUND_I2P_20241223_224139_493806
// todo: move this signal tools to ccutils
#include <deque>
#include <functional>
#include <memory>
namespace wayround_i2p::ccedit
{
template <class>
class Slot;
template <class RetT, class... Args>
class Slot<RetT(Args...)>;
template <class RetT, class... Args>
using Slot_shared = std::shared_ptr<Slot<RetT, Args...>>;
template <class RetT, class... Args>
using Slot_weak = std::weak_ptr<Slot<RetT, Args...>>;
template <class>
class SlotC;
template <class RetT, class... Args>
class SlotC<RetT(Args...)>;
template <class>
class Signal;
template <class RetT, class... Args>
class Signal<RetT(Args...)>;
template <class RetT, class... Args>
using Signal_shared = std::shared_ptr<Signal<RetT, Args...>>;
template <class RetT, class... Args>
using Signal_weak = std::weak_ptr<Signal<RetT, Args...>>;
template <class RetT, class... Args>
class Slot<RetT(Args...)>
{
public:
using slotted_function_type = std::function<RetT(Args...)>;
static Slot_shared<RetT(Args...)> create(slotted_function_type fun = nullptr)
{
auto ret = Slot_shared<RetT(Args...)>(new Slot(fun));
ret->own_ptr = ret;
return ret;
}
protected:
Slot(slotted_function_type fun = nullptr) :
fun(fun)
{
}
public:
~Slot()
{
}
void setFun(slotted_function_type fun)
{
this->fun = fun;
}
RetT on_emission(Args... args)
{
if (!fun)
{
throw "fun not set";
}
return fun(args...);
}
Slot_shared<RetT(Args...)> getOwnPtr()
{
return own_ptr.lock();
}
private:
Slot_weak<RetT(Args...)> own_ptr;
slotted_function_type fun;
};
template <class RetT, class... Args>
class SlotC<RetT(Args...)> : public Slot_shared<RetT(Args...)>
{
public:
SlotC(std::function<RetT(Args...)> fun = nullptr) :
Slot_shared<RetT(Args...)>(Slot<RetT(Args...)>::create(fun))
{
}
~SlotC()
{
}
};
template <class RetT, class... Args>
class Signal<RetT(Args...)>
{
public:
using slot_type = Slot<RetT(Args...)>;
using slot_type_shared = Slot_shared<RetT(Args...)>;
using slot_type_weak = Slot_weak<RetT(Args...)>;
// note: the point here is to remove second parameter from function if
// it's type is void. I don't know how better to do it, so here
// are some bdsm with std::conditionals.
using cb_on_result_function = std::conditional<
std::same_as<RetT, void>,
std::function<void(slot_type_shared slot)>,
std::function<
void(
slot_type_shared slot,
std::conditional<std::same_as<RetT, void>, int, RetT> result
)>>::type;
Signal(cb_on_result_function cb_on_result = nullptr) :
cb_on_result(cb_on_result)
{
}
~Signal()
{
}
void set_cb_on_result(cb_on_result_function cb_on_result)
{
this->cb_on_result = cb_on_result;
}
void connect(slot_type_shared slot_)
{
for (const auto &i : connected_slots)
{
if (i.lock() == slot_)
{
return;
}
}
connected_slots.push_back(slot_);
}
void disconnect(slot_type_shared slot_)
{
std::deque<slot_type_weak> new_connected_slots;
for (auto i : connected_slots)
{
if (i.lock() != slot_)
{
new_connected_slots.push_back(i);
}
}
connected_slots = new_connected_slots;
}
void emit(Args... args)
{
std::deque<slot_type_weak> new_connected_slots;
std::deque<slot_type_shared> shared_connected_slots;
for (auto i : connected_slots)
{
auto x = i.lock();
if (x)
{
shared_connected_slots.push_back(x);
}
}
for (auto i : shared_connected_slots)
{
if constexpr (std::same_as<RetT, void>)
{
i->on_emission(args...);
if (cb_on_result)
{
cb_on_result(i);
}
}
else
{
auto ret = i->on_emission(args...);
if (cb_on_result)
{
cb_on_result(i, ret);
}
}
}
for (auto i : shared_connected_slots)
{
new_connected_slots.push_back(i);
}
connected_slots = new_connected_slots;
}
private:
std::deque<slot_type_weak> connected_slots;
cb_on_result_function cb_on_result;
};
} // namespace wayround_i2p::ccedit
#endif