-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflMgr.cpp
426 lines (384 loc) · 15.9 KB
/
ReflMgr.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "ReflMgr.h"
ReflMgr::Any::Any(ObjectPtr obj) : ObjectPtr(obj) {}
FieldInfo& FieldInfo::withRegister(std::function<ObjectPtr(void*)> getRegister) {
this->getRegister = getRegister;
return *this;
}
MethodInfo& MethodInfo::withRegister(std::function<FuncType> getRegister) {
this->getRegister = getRegister;
return *this;
}
ReflMgr& ReflMgr::Instance() {
static ReflMgr instance;
return instance;
}
#define ERROR std::cerr << errorMsgPrefix
void ReflMgr::SetErrorMsgPrefix(const std::string& msg) {
errorMsgPrefix = msg;
}
TypeID ReflMgr::GetType(std::string_view clsName) {
auto& instance = ReflMgr::Instance();
TypeID target = TypeID::getRaw(clsName);
if (!instance.HasClassInfo(target)) {
return target;
}
while (!instance.classInfo[target].aliasTo.isNull()) {
target = instance.classInfo[target].aliasTo;
}
return target;
}
template<typename T> T* ReflMgr::SafeGetList(TypeIDMap<T>& info, TypeID id) {
if (info.find(id) == info.end()) {
return nullptr;
}
return &info[id];
}
template ClassInfo* ReflMgr::SafeGetList(TypeIDMap<ClassInfo>& info, TypeID id);
template std::unordered_map<std::string, std::vector<MethodInfo>>* ReflMgr::SafeGetList(TypeIDMap<std::unordered_map<std::string, std::vector<MethodInfo>>>& info, TypeID id);
template std::unordered_map<std::string, FieldInfo>* ReflMgr::SafeGetList(TypeIDMap<std::unordered_map<std::string, FieldInfo>>& info, TypeID id);
template<typename T> T* ReflMgr::SafeGet(TypeIDMap<std::unordered_map<std::string, T>>& info, TypeID id, std::string_view name) {
auto* lst = SafeGetList(info, id);
if (lst == nullptr) {
return nullptr;
}
if (lst->find(std::string{name}) == lst->end()) {
return nullptr;
}
return &(*lst)[std::string{name}];
}
template std::vector<MethodInfo>* ReflMgr::SafeGet(TypeIDMap<std::unordered_map<std::string, std::vector<MethodInfo>>>& info, TypeID id, std::string_view name);
template FieldInfo* ReflMgr::SafeGet(TypeIDMap<std::unordered_map<std::string, FieldInfo>>& info, TypeID id, std::string_view name);
template<typename MethodInfoType>
void ReflMgr::CheckParams(MethodInfoType& info, const ArgsTypeList& lst, MethodInfoType** rec, bool showError) {
if (info.argsList.size() != lst.size()) {
return;
}
bool same = true;
bool generics = false;
for (int i = 0; i < info.argsList.size(); i++) {
if (lst[i].getHash() != info.argsList[i].getHash()) {
same = false;
}
bool canUseGenerics = info.argsList[i].getHash() == TypeID::get<ReflMgr::Any>().getHash();
if (canUseGenerics) {
generics = true;
}
if (!lst[i].canBeAppliedTo(info.argsList[i]) && !canUseGenerics) {
if (showError) {
ERROR << "Candidate: ";
ERROR << info.name << "(";
if (info.argsList.size() > 0) {
ERROR << info.argsList[0].getName();
}
for (int i = 1; i < info.argsList.size(); i++) {
ERROR << ", " << info.argsList[i].getName();
}
ERROR << "): " << std::endl;
ERROR << lst[i].getName() << " cannot apply to " << info.argsList[i].getName() << std::endl;
}
return;
}
}
if (same && !generics) {
rec[0] = &info;
} else if (!generics) {
rec[1] = &info;
} else {
rec[2] = &info;
}
}
template void ReflMgr::CheckParams(MethodInfo& info, const ArgsTypeList& lst, MethodInfo** rec, bool showError);
template void ReflMgr::CheckParams(MethodInfo const& info, const ArgsTypeList& lst, MethodInfo const** rec, bool showError);
const MethodInfo* ReflMgr::SafeGet(TypeID id, std::string_view name, const ArgsTypeList& args) {
auto* lst = SafeGet(methodInfo, id, name);
if (lst == nullptr) {
return nullptr;
}
const MethodInfo* rec[3] = { nullptr, nullptr, nullptr };
for (const auto& info : *lst) {
CheckParams(info, args, rec);
}
for (int i = 0; i < 3; i++) {
if (rec[i] != nullptr) {
return rec[i];
}
}
for (const auto& info : *lst) {
CheckParams(info, args, rec, true);
}
return nullptr;
}
template<typename Ret> Ret* ReflMgr::WalkThroughInherits(std::function<void*(void*)>* conv, TypeID id, std::function<Ret*(TypeID)> func) {
std::queue<std::pair<TypeID, std::function<void*(void*)>>> q;
q.push({id, *conv});
while (!q.empty()) {
auto type = q.front().first;
auto convFunc = q.front().second;
q.pop();
auto* ret = func(type);
if (ret != nullptr) {
*conv = convFunc;
return ret;
}
if (HasClassInfo(type)) {
ClassInfo& info = classInfo[type];
for (int i = 0; i < info.parents.size(); i++) {
q.push({info.parents[i], [convFunc, info, i](void* instance) { return info.cast[i](convFunc(instance)); }});
}
}
}
return nullptr;
}
bool MethodInfo::sameDeclareTo(const MethodInfo& other) const {
if (returnType != other.returnType || argsList.size() != other.argsList.size()) {
return false;
}
for (int i = 0; i < argsList.size(); i++) {
if (argsList[i] != other.argsList[i]) {
return false;
}
}
return true;
}
void ReflMgr::AddMethodInfo(TypeID type, const std::string& name, MethodInfo info, bool overridePrevious) {
std::vector<MethodInfo>& lst = methodInfo[type][name];
for (MethodInfo& data : lst) {
if (data.sameDeclareTo(info)) {
if (overridePrevious) {
data = info;
} else {
ERROR << "Error: same type of function already registered: " << info.returnType.getName() << " " << type.getName() << "::" << name << "(";
if (info.argsList.size() > 0) {
ERROR << info.argsList[0].getName();
}
for (int i = 1; i < info.argsList.size(); i++) {
ERROR << ", " << info.argsList[i].getName();
}
ERROR << ")" << std::endl;
}
}
}
methodInfo[type][name].push_back(info);
}
template FieldInfo* ReflMgr::WalkThroughInherits(std::function<void*(void*)>* conv, TypeID id, std::function<FieldInfo*(TypeID)> func);
template MethodInfo* ReflMgr::WalkThroughInherits(std::function<void*(void*)>* conv, TypeID id, std::function<MethodInfo*(TypeID)> func);
const FieldInfo* ReflMgr::SafeGetFieldWithInherit(std::function<void*(void*)>* conv, TypeID id, std::string_view name, bool showError) {
auto ret = WalkThroughInherits(conv, id, std::function([&](TypeID type) { return SafeGet(fieldInfo, type, name); }));
if (ret == nullptr && showError) {
ERROR << "Error: no matching field found: " << id.getName() << "::" << name << std::endl;
}
return ret;
}
const MethodInfo* ReflMgr::SafeGetMethodWithInherit(std::function<void*(void*)>* conv, TypeID id, std::string_view name, const ArgsTypeList& args, bool showError) {
auto ret = WalkThroughInherits(conv, id, std::function([&](TypeID type) { return SafeGet(type, name, args); }));
if (ret == nullptr && showError) {
ERROR << "Error: no matching method found: " << id.getName() << "::" << name << "(";
if (args.size() > 0) {
ERROR << args[0].getName();
for (int i = 1; i < args.size(); i++) {
ERROR << ", " << args[i].getName();
}
}
ERROR << ")" << std::endl;
}
return ret;
}
bool ReflMgr::HasClassInfo(TypeID type) {
return classInfo.find(type) != classInfo.end();
}
static inline std::string_view removeNameRefAndConst(std::string_view name) {
int start = 0, end = name.length();
if (name.length() > 6 && name.substr(0, 6) == "const ") {
start += 6;
}
if (name[name.length() - 1] == '&') {
end--;
}
return name.substr(start, std::max(0, end - start));
}
SharedObject ReflMgr::New(TypeID type, const std::vector<ObjectPtr>& args) {
auto& func = classInfo[TypeID::getRaw(removeNameRefAndConst(type.getName()))].newObject;
if (func == 0) {
ERROR << "Error: unable to init an unregistered class: " << type.getName() << std::endl;
return SharedObject::Null;
}
return func(args);
}
SharedObject ReflMgr::New(std::string_view typeName, const std::vector<ObjectPtr>& args) {
return New(ReflMgr::GetType(typeName), args);
}
ObjectPtr ReflMgr::RawGetField(TypeID type, void* instance, std::string_view member) {
std::function<void*(void*)> conv = [](void* orig) { return orig; };
auto* p = SafeGetFieldWithInherit(&conv, type, member);
if (p == nullptr) {
return ObjectPtr::Null;
}
return p->getRegister(conv(instance));
}
std::function<void(void*, std::vector<void*>, SharedObject& ret)> ReflMgr::GetInvokeFunc(TypeID type, std::string_view member, ArgsTypeList list) {
std::function<void*(void*)> conv = [](void* orig) { return orig; };
auto* info = SafeGetMethodWithInherit(&conv, type, member, list);
if (info == nullptr || info->name == "") {
return nullptr;
}
return [info, conv](void* instance, std::vector<void*> params, SharedObject& ret) {
info->getRegister(conv(instance), params, ret);
};
}
SharedObject ReflMgr::RawInvoke(TypeID type, void* instance, std::string_view member, ArgsTypeList list, std::vector<void*> params) {
std::function<void*(void*)> conv = [](void* orig) { return orig; };
auto* info = SafeGetMethodWithInherit(&conv, type, member, list);
if (info == nullptr || info->name == "") {
return SharedObject::Null;
}
auto ret = info->newRet();
info->getRegister(conv(instance), params, ret);
return ret;
}
void ReflMgr::AddAliasClass(std::string_view from, std::string_view to) {
classInfo[TypeID::getRaw(from)].aliasTo = TypeID::getRaw(to);
}
void ReflMgr::AddVirtualClass(std::string_view cls, std::function<SharedObject(const std::vector<ObjectPtr>&)> ctor, TagList tagList) {
auto type = TypeID::getRaw(cls);
classInfo[type].newObject = ctor;
classInfo[type].tags = tagList;
}
void ReflMgr::AddVirtualInheritance(std::string_view cls, std::string_view inherit) {
ClassInfo& info = classInfo[TypeID::getRaw(cls)];
info.parents.push_back(TypeID::getRaw(inherit));
info.cast.push_back([](void* derived) -> void* {
return derived;
});
}
static inline TagList nullTag;
TagList& ReflMgr::GetClassTag(TypeID cls) {
return classInfo[cls].tags;
}
TagList& ReflMgr::GetFieldTag(TypeID cls, std::string_view name) {
return fieldInfo[cls][std::string{name}].tags;
}
TagList& ReflMgr::GetMethodInfo(TypeID cls, std::string_view name) {
return methodInfo[cls][std::string{name}][0].tags;
}
TagList& ReflMgr::GetMethodInfo(TypeID cls, std::string_view name, const ArgsTypeList& args) {
auto& infos = methodInfo[cls][std::string{name}];
MethodInfo* rec[3] = { nullptr, nullptr, nullptr };
for (auto& info : infos) {
CheckParams(info, args, rec);
}
for (int i = 0; i < 3; i++) {
if (rec[i] != nullptr) {
return rec[i]->tags;
}
}
return nullTag;
}
void ReflMgr::RawAddField(TypeID cls, TypeID varType, std::string_view name, std::function<ObjectPtr(ObjectPtr)> func) {
FieldInfo info{ std::string{name} };
auto& field = fieldInfo[cls][std::string{name}];
field = info.withRegister([func, cls](void* ptr) { return func(ObjectPtr{cls, ptr}); });
field.varType = varType;
}
void ReflMgr::RawAddStaticField(TypeID cls, TypeID varType, std::string_view name, std::function<ObjectPtr()> func) {
FieldInfo info{ std::string{name} };
auto& field = fieldInfo[cls][std::string{name}];
field = info.withRegister([func](void* ptr) { return func(); });
field.varType = varType;
}
ObjectPtr ReflMgr::RawGetField(ObjectPtr instance, std::string_view name) {
return RawGetField(instance.GetType(), instance.GetRawPtr(), name);
}
void ReflMgr::RawAddMethod(TypeID cls, std::string_view name, TypeID returnType, const ArgsTypeList& argsList, std::function<SharedObject(ObjectPtr, const std::vector<ObjectPtr>&)> func) {
MethodInfo info{ std::string{name} };
info.returnType = returnType;
info.argsList = argsList;
info.newRet = []() { return SharedObject(); };
AddMethodInfo(cls, info.name, info.withRegister([func, argsList, cls](void* instance, const std::vector<void*>& params, SharedObject& ret) {
std::vector<ObjectPtr> args;
for (int i = 0; i < params.size(); i++) {
args.push_back(ObjectPtr{argsList[i], params[i]});
}
ret = func(ObjectPtr{cls, instance}, args);
}));
}
SharedObject ReflMgr::RawInvoke(ObjectPtr instance, std::string_view name, const std::vector<ObjectPtr>& params) {
ArgsTypeList list;
std::vector<void*> args;
for (auto param : params) {
args.push_back(param.GetRawPtr());
list.push_back(param.GetType());
}
return RawInvoke(instance.GetType(), instance.GetRawPtr(), name, list, args);
}
void ReflMgr::RawAddStaticMethod(TypeID cls, std::string_view name, TypeID returnType, const ArgsTypeList& argsList, std::function<SharedObject(const std::vector<ObjectPtr>&)> func) {
MethodInfo info{ std::string{name} };
info.returnType = returnType;
info.argsList = argsList;
info.newRet = []() { return SharedObject(); };
AddMethodInfo(cls, info.name, info.withRegister([func, argsList](void* instance, const std::vector<void*>& params, SharedObject& ret) {
std::vector<ObjectPtr> args;
for (int i = 0; i < params.size(); i++) {
args.push_back(ObjectPtr{argsList[i], params[i]});
}
ret = func(args);
}));
}
SharedObject ReflMgr::RawInvokeStatic(TypeID type, std::string_view name, const std::vector<ObjectPtr>& params) {
return InvokeStatic(type, name, params);
}
void ReflMgr::SelfExport() {
auto& mgr = Instance();
mgr.AddStaticMethod(TypeID::get<ReflMgr>(), &ReflMgr::Instance, "Instance");
mgr.AddMethod(&ReflMgr::RawAddField, "AddField");
mgr.AddMethod(&ReflMgr::RawAddStaticField, "AddStaticField");
mgr.AddMethod(MethodType<ObjectPtr, ReflMgr, ObjectPtr, std::string_view>::Type(&ReflMgr::RawGetField), "GetField");
mgr.AddMethod(&ReflMgr::RawAddMethod, "AddMethod");
mgr.AddMethod(MethodType<SharedObject, ReflMgr, ObjectPtr, std::string_view, const std::vector<ObjectPtr>&>::Type(&ReflMgr::RawInvoke), "Invoke");
mgr.AddMethod(&ReflMgr::RawAddStaticMethod, "AddStaticMethod");
mgr.AddMethod(&ReflMgr::RawInvokeStatic, "InvokeStatic");
}
void ReflMgr::IterateField(TypeID cls, std::function<void(const FieldInfo&)> callback) {
std::function<void*(void*)> conv = [](void* orig) { return orig; };
cls = GetType(cls.getName());
WalkThroughInherits(&conv, cls, std::function([&](TypeID type) -> FieldInfo* {
auto* lst = SafeGetList(fieldInfo, cls);
if (lst == nullptr) {
return nullptr;
}
for (auto& field : *lst) {
callback(field.second);
}
return nullptr;
}));
}
void ReflMgr::IterateMethod(TypeID cls, std::function<void(const MethodInfo&)> callback) {
std::function<void*(void*)> conv = [](void* orig) { return orig; };
cls = GetType(cls.getName());
WalkThroughInherits(&conv, cls, std::function([&](TypeID type) -> MethodInfo* {
auto* lst = SafeGetList(methodInfo, cls);
if (lst == nullptr) {
return nullptr;
}
for (auto& method : *lst) {
for (auto& overloads : method.second) {
callback(overloads);
}
}
return nullptr;
}));
}
bool ReflMgr::IsBaseClass(TypeID query, TypeID base) {
std::queue<TypeID> q;
q.push(query);
while (!q.empty()) {
if (q.front() == base) {
return true;
}
auto& info = classInfo[q.front()];
q.pop();
for (auto& id : info.parents) {
q.push(id);
}
}
return false;
}