zengine-old/engine/3rdparty/zlib/test/refl_01.cpp
2024-04-04 18:18:04 +08:00

70 lines
1.9 KiB
C++

#include <iostream>
#include <array>
#include "refl/uclass.h"
#include "vertex.h"
#include "tstring.h"
using namespace std;
using namespace refl;
struct vec3_parent {
};
struct vec3 : public vec3_parent {
using UClass = class vec3_UClass;
float x = 1;
float y = 2;
float z = 3;
string name{ "hellohellohellohellohellohello" };
float norm() {
return x * y * z;
}
};
struct vec3_UClass : public UClass {
array<FieldPtr,4> FList;
using UClass::UClass;
const FieldPtr* GetField(const Name& name)const override {
for (auto& field : FList) {
if (field.name == name) {
return &field;
}
}
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);
//offsetof(vec3, x) 很坑,相当坑
cls.FList[0] = cls.MakeMemberField<offsetof(vec3, x)>(&vec3::x, "x");
cls.FList[1] = cls.MakeMemberField<offsetof(vec3, y)>(&vec3::y, "y");
cls.FList[2] = cls.MakeMemberField<offsetof(vec3, z)>(&vec3::z, "z");
cls.FList[3] = cls.MakeMemberField<offsetof(vec3, name)>(&vec3::name, "name");
return cls;
}
};
int main() {
using uclass = TypeInfo<vec3>::UClass;
auto& res = TypeInfo<int>::StaticClass;
auto& res2 = TypeInfo<Name>::StaticClass;
auto& res3 = TypeInfo<vec3>::StaticClass;
vec3 v1;
auto res4 = TypeInfo<size_t>::StaticClass;
auto res5 = TypeInfo<string_view>::StaticClass;
auto t1 = res3.FList[0];
vec3_parent* v2 = res3.New<vec3_parent>();
vec3* v22 = (vec3*)v2;
auto v3 = res3.New();
int x1 = 110;
float x = *res.New(&x1);
v3.Get("x", x);
v3.Set("z", (float)33);
v2 = (vec3*)v3.ptr;
using T = int;
auto b1 = res3.IsChildOf<vec3_parent>();
constexpr auto cls = UClass_Auto<int>(type_name<T>().View(), sizeof(T));
cout << sizeof(Name) << "Type2: " << typeid(vec3).name() << endl;
delete v2;
cout << "hello world\n";
}