-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delegate.h
185 lines (147 loc) · 5.06 KB
/
Delegate.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef DELEGATE_H
#define DELEGATE_H
#include <thread>
#include <future>
#include <chrono>
#include <random>
#include <functional>
#include <array>
#include <set>
namespace Event {
struct UUID {
public:
UUID() : data({0}) {
}
static UUID New();
std::string toString() const;
std::array<uint32_t, 4> GetData() const {
return data;
}
bool operator==(const UUID& other) const;
bool operator<(const UUID& other) const;
std::ostream& operator<<(std::ostream& os) const;
private:
inline static std::set<UUID> uuids;
std::array<uint32_t, 4> data;
};
// Delegate class
template<typename T>
class Delegate;
template<typename C, typename R, typename... Args>
Delegate<R (C::*)(Args...)> MakeDelegate(R (C::*memberFunc)(Args...), std::shared_ptr<C> instance) {
return Delegate<R (C::*)(Args...)>(memberFunc, instance);
}
template<typename C, typename R, typename... Args>
Delegate<R (C::*)(Args...)> MakeDelegate(R (C::*memberFunc)(Args...), C&instance) {
return Delegate<R (C::*)(Args...)>(memberFunc, std::make_shared<C>(instance));
}
template<typename C, typename R, typename... Args>
Delegate<R (C::*)(Args...)> MakeDelegate(R (C::*memberFunc)(Args...)) {
return Delegate<R (C::*)(Args...)>(memberFunc);
}
template<typename R, typename... Args>
Delegate<R(Args...)> MakeDelegate(R (*func)(Args...)) {
return Delegate<R(Args...)>(func);
}
// Specialization for member functions
template<typename R, typename C, typename... Args>
class Delegate<R (C::*)(Args...)> {
public:
using FunctionType = std::function<R(C&, Args...)>;
using InstancePtr = std::shared_ptr<C>;
Delegate(FunctionType func, const InstancePtr&instance) : pDelegate(func), pInstance(instance) {
pID = UUID::New();
}
// 转换构造函数,允许从成员函数对象初始化
Delegate(R (C::*funcPtr)(Args...), const InstancePtr&instance) {
pDelegate = [funcPtr](C&instance, Args... args) -> R {
return (instance.*funcPtr)(std::forward<Args>(args)...);
};
pID = UUID::New();
pInstance = instance; // 当使用成员函数对象初始化时,实例指针应该被传入
}
//不传入Instance进行构造
Delegate(R (C::*func)(Args...), C* instance) : pDelegate([instance,func](Args... args) {
return (instance->*func)(std::forward<Args>(args)...);
}), pInstance(instance) {
pID = UUID::New();
}
Delegate(): pDelegate(nullptr), pInstance(nullptr) {
pID = UUID::New();
}
void SetInstance(const InstancePtr&instance) {
pInstance = instance;
}
bool operator==(const Delegate&rhs) const {
return pID == rhs.pID;
}
R operator()(Args... args) const {
if (pInstance) {
return pDelegate(*pInstance, std::forward<Args>(args)...);
}
throw std::runtime_error("Instance is null");
}
R operator()(std::shared_ptr<C> instance, Args... args) const {
return pDelegate(*instance, std::forward<Args>(args)...);
}
R operator()(C&instance, Args... args) const {
return pDelegate(instance, std::forward<Args>(args)...);
}
//转为普通委托
operator Delegate<R(Args...)>() const {
auto lambda = [&](Args... args) {
return pDelegate(*pInstance, std::forward<Args>(args)...);
};
return Delegate<R(Args...)>(lambda);
}
Delegate& operator=(const Delegate&rhs) {
pDelegate = rhs.pDelegate;
pInstance = rhs.pInstance;
pID = rhs.pID;
return *this;
}
UUID ID() const {
return pID;
}
private:
FunctionType pDelegate;
UUID pID;
InstancePtr pInstance; // Pointer to the instance
};
// Specialization for free functions
template<typename R, typename... Args>
class Delegate<R(Args...)> {
public:
using FunctionType = std::function<R(Args...)>;
Delegate(FunctionType func) : pDelegate(func) {
pID = UUID::New();
}
Delegate() {
pID = UUID::New();
}
bool operator==(const Delegate&rhs) const {
return pID == rhs.pID;
}
R operator()(Args... args) const {
return pDelegate(std::forward<Args>(args)...);
}
Delegate& operator=(const Delegate&rhs) {
pDelegate = rhs.pDelegate;
pID = rhs.pID;
return *this;
}
Delegate& operator=(FunctionType func) {
pDelegate = func;
return *this;
}
UUID ID() const {
return pID;
}
private:
FunctionType pDelegate;
UUID pID;
};
inline static void DoNothing() {
}
}
#endif //DELEGATE_H