forked from alenstarx/sigslot11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigslot11.h
99 lines (79 loc) · 2.19 KB
/
sigslot11.h
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
#ifndef SIGSLOT11_H
#define SIGSLOT11_H
#include <list>
namespace sigslot11 {
template<typename R> class Function;
template<typename R, typename... Args>
class Function<R(Args...)>
{
void *m_this_ptr;
R (*m_stub_ptr)(void *, Args...);
template<typename I, typename F>
Function(I&& this_ptr, F&& stub_ptr) :
m_this_ptr(std::forward<I>(this_ptr)),
m_stub_ptr(std::forward<F>(stub_ptr)) {}
public:
template<R (*func_ptr)(Args...)>
static inline Function bind()
{
return { nullptr, [] (void *, Args... args) {
return (*func_ptr)(args...); }
};
}
template<typename T, R (T::*method_ptr)(Args...)>
static inline Function bind(T* pointer)
{
return { pointer, [] (void *this_ptr, Args... args) {
return (static_cast<T*>(this_ptr)->*method_ptr)(args...); }
};
}
template<typename T, R (T::*method_ptr)(Args...) const>
static inline Function bind(T* pointer)
{
return { pointer, [] (void *this_ptr, Args... args) {
return (static_cast<const T*>(this_ptr)->*method_ptr)(args...); }
};
}
R operator() (Args&&... args) const
{
return (*m_stub_ptr)(m_this_ptr, args...);
}
};
template <typename R> class Signal;
template <typename R, typename... Args>
class Signal<R(Args...)>
{
typedef sigslot11::Function<R(Args...)> Function;
std::list<Function> slots;
public:
template <R (*func_ptr)(Args...)>
void connect()
{
auto fn = Function::template bind<func_ptr>();
slots.push_back(fn);
}
template <typename T, R (T::*meth_ptr)(Args...)>
void connect(T *instance)
{
auto fn = Function::template bind<T, meth_ptr>(instance);
slots.push_back(fn);
}
template <typename T, R (T::*meth_ptr)(Args...) const>
void connect(T *instance)
{
auto fn = Function::template bind<T, meth_ptr>(instance);
slots.push_back(fn);
}
void disconnect()
{
slots.clear();
}
void operator()(Args... var) const
{
for (auto const &slot : slots) {
slot(std::forward<Args>(var)...);
}
}
};
}
#endif