-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signal.hpp
46 lines (37 loc) · 1.14 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
#include <list>
#include <functional>
template<typename... Args>
class Signal
{
private:
std::list<std::function<void(Args...)> > _calls;
public:
virtual ~Signal()
{
_calls.clear();
}
void connect(std::function<void(Args...)> fct)
{
_calls.push_back(fct);
}
template<typename Object>
void connect(Object* object, void (Object::*method)(Args...))
{
_calls.push_back([object,method](Args... args){(*object.*method)(args...);});
}
template<typename Object>
void connect(Object* object, void (Object::*method)(Args...) const)
{
_calls.push_back([object,method](Args... args){(*object.*method)(args...);});
}
template<typename Object>
void connect(const Object* object, void (Object::*method)(Args...) const)
{
_calls.push_back([object,method](Args... args){(*object.*method)(args...);});
}
void emit(Args... args)
{
for(auto call : _calls)
call(args...);
}
};