-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignal1.h
142 lines (137 loc) · 2.89 KB
/
signal1.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
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
/*********************************************************
File Name:signal.h
Author: Abby Cin
Mail: [email protected]
Created Time: Sun 17 Jul 2016 07:34:00 PM CST
**********************************************************/
#ifndef SIGNAL_H_
#define SIGNAL_H_
#include <utility>
#include <list>
namespace nm
{
namespace
{
template<typename>
struct sig_trait;
template<typename Res, typename... Args>
struct sig_trait<Res(Args...)> {
using mFunc = Res (*)(void *, Args...);
using Func = Res (*)(Args...);
sig_trait(const sig_trait &) = delete;
sig_trait &operator=(const sig_trait &) = delete;
sig_trait(sig_trait &&rhs)
{
obj = rhs.obj;
callable = rhs.callable;
mcallable = rhs.mcallable;
}
sig_trait(void *o, mFunc fp)
{
obj = o;
mcallable = fp;
callable = nullptr;
}
sig_trait(void *o, Func fp)
{
obj = o;
callable = fp;
mcallable = nullptr;
}
template<typename Obj, Res (Obj::*mfp)(Args...)>
static sig_trait connect(Obj *o)
{
return { o, [](void *obj, Args... args) {
return (static_cast<Obj *>(obj)->*mfp)(
std::forward<Args>(args)...);
} };
}
template<typename F>
static sig_trait connect(F &&fp)
{
return { nullptr, fp };
}
void *obj;
mFunc mcallable;
Func callable;
template<typename... Paras>
Res call(Paras &&...paras) const
{
if (obj)
return mcallable(obj,
std::forward<Paras>(paras)...);
return callable(std::forward<Paras>(paras)...);
}
};
}
namespace signal
{
namespace
{
using std::list;
}
template<typename>
class Signal;
template<typename Res, typename... Paras>
class Signal<Res(Paras...)> {
public:
using data_type = Res(Paras...);
using handle_type =
typename list<sig_trait<data_type>>::iterator;
Signal()
{
}
Signal(const Signal &) = delete;
Signal &operator=(const Signal &) = delete;
~Signal()
{
}
template<typename Obj, Res (Obj::*mfp)(Paras...)>
handle_type &connect(Obj *o)
{ // sig_trait<data_type>::template connect<Obj, mfp>(o); RVO
// optimization
objs.push_back(
sig_trait<data_type>::template connect<Obj,
mfp>(o));
return --objs.end();
}
template<typename F>
handle_type &connect(F &&fp)
{ // sig_trait<data_type>::connect(fp); RVO optimization
objs.push_back(sig_trait<data_type>::connect(fp));
return --objs.end();
}
void disconnect(handle_type &handle)
{
objs.erase(handle);
}
template<typename F>
void disconnect(F &&fp)
{
for (auto iter = objs.begin(); iter != objs.end();) {
if (fp == iter->callable)
iter = objs.erase(iter);
else
++iter;
}
}
template<typename... Args>
void emit(Args &&...args) const
{
for (auto &x : objs)
x.call(std::forward<Args>(args)...);
}
std::size_t size() const
{
return objs.size();
}
bool empty() const
{
return objs.empty();
}
private:
list<sig_trait<data_type>> objs;
};
}
}
#endif