forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_slot.hpp
83 lines (75 loc) · 1.74 KB
/
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
#pragma once
template <typename Result, typename... Arguments>
class BaseSlot
{
public:
virtual ~BaseSlot() {}
virtual Result operator()(Arguments...) = 0;
};
template <typename T, typename Result, typename... Arguments>
class Slot: public BaseSlot<Result, Arguments...>
{
public:
typedef Result (T::*Method)(Arguments...);
Slot(T *object, Method method):
object_(object),
method_(method)
{}
virtual Result operator()(Arguments... arguments)
{
return (object_->*method_)(arguments...);
}
private:
T *object_;
Method method_;
};
template <typename Result, typename... Arguments>
class Signal
{
public:
Signal():
slot_(nullptr)
{}
~Signal()
{
delete slot_;
}
Result emit(Arguments... arguments)
{
if (slot_)
return slot_->operator()(arguments...);
return Result();
}
template <typename T>
void connect(T *object, Result (T::*method)(Arguments...))
{
delete slot_;
slot_ = new Slot<T, Result, Arguments...>(object, method);
};
void disconnect()
{
delete slot_;
slot_ = nullptr;
}
private:
BaseSlot<Result, Arguments...> *slot_;
};
template <typename T, typename Result, typename... Arguments>
void connect(Signal<Result, Arguments...> &signal, T *object, Result (T::*method)(Arguments...))
{
signal.connect(object, method);
}
template <typename Result, typename... Arguments>
void disconnect(Signal<Result, Arguments...> &signal)
{
signal.disconnect();
}
template <typename T>
struct RemovePointer;
template <typename T>
struct RemovePointer<T *>
{
typedef T type;
};
#define SIGNAL(o, s) (o)->s
#define SLOT(o, s) (o), &RemovePointer<decltype(o)>::type::s