-
Notifications
You must be signed in to change notification settings - Fork 61
/
nano_signal_slot.hpp
171 lines (147 loc) · 4.65 KB
/
nano_signal_slot.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
#pragma once
#include "nano_function.hpp"
#include "nano_observer.hpp"
namespace Nano
{
template <typename RT, typename MT_Policy = ST_Policy>
class Signal;
template <typename RT, typename MT_Policy, typename... Args>
class Signal<RT(Args...), MT_Policy> final : public Observer<MT_Policy>
{
using observer = Observer<MT_Policy>;
using function = Function<RT(Args...)>;
template <typename T>
void insert_sfinae(Delegate_Key const& key, typename T::Observer* instance)
{
observer::insert(key, instance);
instance->insert(key, this);
}
template <typename T>
void remove_sfinae(Delegate_Key const& key, typename T::Observer* instance)
{
observer::remove(key);
instance->remove(key);
}
template <typename T>
void insert_sfinae(Delegate_Key const& key, ...)
{
observer::insert(key, this);
}
template <typename T>
void remove_sfinae(Delegate_Key const& key, ...)
{
observer::remove(key);
}
public:
Signal() noexcept = default;
~Signal() noexcept = default;
Signal(Signal const&) noexcept = delete;
Signal& operator= (Signal const&) noexcept = delete;
Signal(Signal&&) noexcept = default;
Signal& operator=(Signal&&) noexcept = default;
//-------------------------------------------------------------------CONNECT
template <typename L>
void connect(L* instance)
{
observer::insert(function::template bind(instance), this);
}
template <typename L>
void connect(L& instance)
{
connect(std::addressof(instance));
}
template <RT(*fun_ptr)(Args...)>
void connect()
{
observer::insert(function::template bind<fun_ptr>(), this);
}
template <typename T, RT(T::*mem_ptr)(Args...)>
void connect(T* instance)
{
insert_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <typename T, RT(T::*mem_ptr)(Args...) const>
void connect(T* instance)
{
insert_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <typename T, RT(T::*mem_ptr)(Args...)>
void connect(T& instance)
{
connect<mem_ptr, T>(std::addressof(instance));
}
template <typename T, RT(T::*mem_ptr)(Args...) const>
void connect(T& instance)
{
connect<mem_ptr, T>(std::addressof(instance));
}
template <auto mem_ptr, typename T>
void connect(T* instance)
{
insert_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <auto mem_ptr, typename T>
void connect(T& instance)
{
connect<mem_ptr, T>(std::addressof(instance));
}
//----------------------------------------------------------------DISCONNECT
template <typename L>
void disconnect(L* instance)
{
observer::remove(function::template bind(instance));
}
template <typename L>
void disconnect(L& instance)
{
disconnect(std::addressof(instance));
}
template <RT(*fun_ptr)(Args...)>
void disconnect()
{
observer::remove(function::template bind<fun_ptr>());
}
template <typename T, RT(T::*mem_ptr)(Args...)>
void disconnect(T* instance)
{
remove_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <typename T, RT(T::*mem_ptr)(Args...) const>
void disconnect(T* instance)
{
remove_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <typename T, RT(T::*mem_ptr)(Args...)>
void disconnect(T& instance)
{
disconnect<T, mem_ptr>(std::addressof(instance));
}
template <typename T, RT(T::*mem_ptr)(Args...) const>
void disconnect(T& instance)
{
disconnect<T, mem_ptr>(std::addressof(instance));
}
template <auto mem_ptr, typename T>
void disconnect(T* instance)
{
remove_sfinae<T>(function::template bind<mem_ptr>(instance), instance);
}
template <auto mem_ptr, typename T>
void disconnect(T& instance)
{
disconnect<mem_ptr, T>(std::addressof(instance));
}
//----------------------------------------------------FIRE / FIRE ACCUMULATE
template <typename... Uref>
void fire(Uref&&... args)
{
observer::template for_each<function>(std::forward<Uref>(args)...);
}
template <typename Accumulate, typename... Uref>
void fire_accumulate(Accumulate&& accumulate, Uref&&... args)
{
observer::template for_each_accumulate<function, Accumulate>
(std::forward<Accumulate>(accumulate), std::forward<Uref>(args)...);
}
};
} // namespace Nano ------------------------------------------------------------