zengine-old/engine/3rdparty/zlib/test/refl_01.cpp

117 lines
3.3 KiB
C++
Raw Normal View History

2024-04-03 17:58:02 +08:00
#include <iostream>
#include <array>
#include "refl/uclass.h"
#include "vertex.h"
2024-04-04 18:18:04 +08:00
#include "tstring.h"
2024-04-05 16:03:58 +08:00
#include <functional>
2024-04-03 17:58:02 +08:00
using namespace std;
using namespace refl;
2024-04-04 18:18:04 +08:00
struct vec3_parent {
2024-04-07 21:15:34 +08:00
virtual void norm(int x1, int& x2) {
x2 = x1 * x2;
cout << x2 << "vec3_parent::norm" << endl;
}
2024-04-04 18:18:04 +08:00
};
struct vec3 : public vec3_parent {
2024-04-03 17:58:02 +08:00
using UClass = class vec3_UClass;
2024-04-04 18:18:04 +08:00
float x = 1;
float y = 2;
float z = 3;
string name{ "hellohellohellohellohellohello" };
2024-04-06 23:50:17 +08:00
virtual float norm1(int x1 = 10) {
2024-04-05 16:03:58 +08:00
cout << x1 << "::norm1" << endl;
return x * y *z * x1;
}
2024-04-06 23:50:17 +08:00
static void norm2(int x1 = 10) {
2024-04-05 16:03:58 +08:00
cout << x1 << "::norm2" << endl;
}
2024-04-06 23:50:17 +08:00
void norm3(int x1 = 10) {
2024-04-05 16:03:58 +08:00
x1 = x * y * z;
cout << x1 << "::norm3" << endl;
2024-04-03 17:58:02 +08:00
}
};
struct vec3_UClass : public UClass {
2024-04-05 16:03:58 +08:00
public:
array<FieldPtr,6> FList;
2024-04-07 21:15:34 +08:00
int32_t AttrNum;
2024-04-04 18:18:04 +08:00
using UClass::UClass;
2024-04-07 21:15:34 +08:00
const FieldPtr* GetField(const Name& name, bool isMethod)const override {
// 指定开始位置的迭代器
auto start = FList.begin();
if (isMethod) {
start += AttrNum;
}
auto end = FList.end();
for (auto it = start; it != end; ++it) {
if (it->name == name) {
return &*it;
2024-04-04 18:18:04 +08:00
}
}
return nullptr;
}
void InitObject(void* ptr)const override {
vec3 obj{};
std::construct_at((vec3*)ptr, obj);
}
static vec3_UClass BuildClass() {
vec3_UClass cls(type_name<vec3>().View(), sizeof(vec3), &TypeInfo<vec3_parent>::StaticClass);
2024-04-03 17:58:02 +08:00
//offsetof(vec3, x) 很坑,相当坑
2024-04-07 21:15:34 +08:00
cls.AttrNum = 3;
cls.FList[0] = cls.MakeMemberField<offsetof(vec3, x)>("x", &TypeInfo<float>::StaticClass);
cls.FList[1] = cls.MakeMemberField<offsetof(vec3, y)>("y", &TypeInfo<float>::StaticClass);
cls.FList[2] = cls.MakeMemberField<offsetof(vec3, z)>("z", &TypeInfo<float>::StaticClass);
2024-04-06 23:50:17 +08:00
cls.FList[3] = cls.MakeMethodField(&vec3::norm, "norm");
2024-04-07 21:15:34 +08:00
cls.FList[4] = cls.MakeMethodField(&vec3::norm1, "norm1");
2024-04-06 23:50:17 +08:00
//cls.FList[5] = cls.MakeMethodField(&vec3::norm2, "norm2");
2024-04-03 17:58:02 +08:00
return cls;
}
};
2024-04-06 23:50:17 +08:00
template<typename ...Args>
void printt(void* ptr, std::vector<ArgsView>& ArgsList) {
using MethodType = void (*)(Args...);
MethodType fptr = (MethodType)ptr;
2024-04-07 21:15:34 +08:00
{
auto param = ArgsList.rbegin();
fptr(param++->cast_to<Args>()...);
//fptr((Args)param++->val...);
}
/* {
auto param = ArgsList.begin();
auto val1 = (++param);
auto val2 = (++param);
auto val3 = (++param);
fptr(val1->cast_to<void *>(), val1->cast_to<void*>(), val1->cast_to<void*>();
}
*/
2024-04-06 23:50:17 +08:00
//fptr((++param)->val, (int)(++param)->val, (int&)(++param)->val);
2024-04-07 21:15:34 +08:00
//fptr(std::forward<Args>((Args)(++param)->val)...);
2024-04-06 23:50:17 +08:00
int x = 1;
x = x + 1;
}
2024-04-03 17:58:02 +08:00
int main() {
2024-04-06 23:50:17 +08:00
auto cls = &TypeInfo<vec3>::StaticClass;
2024-04-07 21:15:34 +08:00
auto cls2 = &TypeInfo<int>::StaticClass;
auto cls3 = &TypeInfo<int*>::StaticClass;
auto cls4 = &TypeInfo<void*>::StaticClass;
auto cls5 = &TypeInfo<decltype(&vec3::norm)>::StaticClass;
2024-04-06 23:50:17 +08:00
vec3 v;
int x = 1, y = 2;
std::vector<ArgsView> ArgsList;
ArgsList.emplace_back();
2024-04-07 21:15:34 +08:00
ArgsList.emplace_back(v);
ArgsList.emplace_back(x);
ArgsList.emplace_back(y);
2024-04-05 16:03:58 +08:00
auto ptr1 = &vec3::norm;
2024-04-07 21:15:34 +08:00
using MethodType = void (*)(void*, int, int&);
2024-04-06 23:50:17 +08:00
MethodType ptr2 = *(MethodType*)&ptr1;
2024-04-07 21:15:34 +08:00
using R = int;
using RetType = void*;
auto ttt = sizeof(void*);
RetType ret;
//ptr2(&v, x , y);
auto ov = cls->New((void*)& v);
ov.Invoke("norm", x, y);
printt<const void*, int , int&>(*(void**)&ptr1, ArgsList);
2024-04-06 23:50:17 +08:00
//ptr2(x, y);
2024-04-07 21:15:34 +08:00
cout << "hello world\n";
2024-04-03 17:58:02 +08:00
}