forked from NoAvailableAlias/nano-signal-slot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nano_signal_slot.hpp
172 lines (144 loc) · 4.5 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
172
#ifndef NANO_SIGNAL_SLOT_HPP
#define NANO_SIGNAL_SLOT_HPP
#include "nano_function.hpp"
#include "nano_observer.hpp"
namespace Nano
{
template <typename RT> class Signal;
template <typename RT, typename... Args>
class Signal<RT(Args...)> : private Observer
{
template <typename T>
void insert_sfinae(DelegateKey const& key, typename T::Observer* instance)
{
Observer::insert(key, instance);
instance->insert(key, this);
}
template <typename T>
void remove_sfinae(DelegateKey const& key, typename T::Observer* instance)
{
Observer::remove(key, instance);
instance->remove(key, this);
}
template <typename T>
void insert_sfinae(DelegateKey const& key, ...)
{
Observer::insert(key, this);
}
template <typename T>
void remove_sfinae(DelegateKey const& key, ...)
{
Observer::remove(key, this);
}
public:
using Delegate = Function<RT(Args...)>;
//-------------------------------------------------------------------CONNECT
template <typename L>
void connect(L* instance)
{
Observer::insert(Delegate::template bind (instance), this);
}
template <typename L>
void connect(L& instance)
{
connect(std::addressof(instance));
}
template <RT (* fun_ptr)(Args...)>
void connect()
{
Observer::insert(Delegate::template bind<fun_ptr>(), this);
}
template <typename T, RT (T::* mem_ptr)(Args...)>
void connect(T* instance)
{
insert_sfinae<T>(Delegate::template bind<T, mem_ptr>(instance), instance);
}
template <typename T, RT (T::* mem_ptr)(Args...) const>
void connect(T* instance)
{
insert_sfinae<T>(Delegate::template bind<T, mem_ptr>(instance), instance);
}
template <typename T, RT (T::* mem_ptr)(Args...)>
void connect(T& instance)
{
connect<T, mem_ptr>(std::addressof(instance));
}
template <typename T, RT (T::* mem_ptr)(Args...) const>
void connect(T& instance)
{
connect<T, mem_ptr>(std::addressof(instance));
}
//----------------------------------------------------------------DISCONNECT
template <typename L>
void disconnect(L* instance)
{
Observer::remove(Delegate::template bind (instance), this);
}
template <typename L>
void disconnect(L& instance)
{
disconnect(std::addressof(instance));
}
template <RT (* fun_ptr)(Args...)>
void disconnect()
{
Observer::remove(Delegate::template bind<fun_ptr>(), this);
}
template <typename T, RT (T::* mem_ptr)(Args...)>
void disconnect(T* instance)
{
remove_sfinae<T>(Delegate::template bind<T, mem_ptr>(instance), instance);
}
template <typename T, RT (T::* mem_ptr)(Args...) const>
void disconnect(T* instance)
{
remove_sfinae<T>(Delegate::template bind<T, 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));
}
//----------------------------------------------------EMIT / EMIT ACCUMULATE
#ifdef NANO_USE_DEPRECATED
/// Will not benefit from perfect forwarding
/// TODO [[deprecated]] when c++14 is comfortably supported
void operator() (Args... args)
{
emit(std::forward<Args>(args)...);
}
template <typename Accumulate>
void operator() (Args... args, Accumulate&& accumulate)
{
emit_accumulate<Accumulate>
(std::forward<Accumulate>(accumulate), std::forward<Args>(args)...);
}
#endif
template <typename... Uref>
void operator() (Uref&&... args)
{
Observer::onEach<Delegate>(std::forward<Uref>(args)...);
}
template <typename Accumulate, typename... Uref>
void emit_accumulate(Accumulate&& accumulate, Uref&&... args)
{
Observer::onEach_Accumulate<Delegate, Accumulate>
(std::forward<Accumulate>(accumulate), std::forward<Uref>(args)...);
}
//-------------------------------------------------------------------UTILITY
bool empty() const
{
return Observer::isEmpty();
}
void removeAll()
{
Observer::removeAll();
}
};
} // namespace Nano ------------------------------------------------------------
#endif // NANO_SIGNAL_SLOT_HPP