117 lines
3.3 KiB
C++
117 lines
3.3 KiB
C++
#include <iostream>
|
|
#include <array>
|
|
#include "refl/uclass.h"
|
|
#include "vertex.h"
|
|
#include "tstring.h"
|
|
#include <functional>
|
|
using namespace std;
|
|
using namespace refl;
|
|
struct vec3_parent {
|
|
virtual void norm(int x1, int& x2) {
|
|
x2 = x1 * x2;
|
|
cout << x2 << "vec3_parent::norm" << endl;
|
|
}
|
|
};
|
|
struct vec3 : public vec3_parent {
|
|
using UClass = class vec3_UClass;
|
|
float x = 1;
|
|
float y = 2;
|
|
float z = 3;
|
|
string name{ "hellohellohellohellohellohello" };
|
|
virtual float norm1(int x1 = 10) {
|
|
cout << x1 << "::norm1" << endl;
|
|
return x * y *z * x1;
|
|
}
|
|
static void norm2(int x1 = 10) {
|
|
cout << x1 << "::norm2" << endl;
|
|
}
|
|
void norm3(int x1 = 10) {
|
|
x1 = x * y * z;
|
|
cout << x1 << "::norm3" << endl;
|
|
}
|
|
};
|
|
struct vec3_UClass : public UClass {
|
|
public:
|
|
array<FieldPtr,6> FList;
|
|
int32_t AttrNum;
|
|
using UClass::UClass;
|
|
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;
|
|
}
|
|
}
|
|
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.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);
|
|
cls.FList[3] = cls.MakeMethodField(&vec3::norm, "norm");
|
|
cls.FList[4] = cls.MakeMethodField(&vec3::norm1, "norm1");
|
|
//cls.FList[5] = cls.MakeMethodField(&vec3::norm2, "norm2");
|
|
return cls;
|
|
}
|
|
};
|
|
template<typename ...Args>
|
|
void printt(void* ptr, std::vector<ArgsView>& ArgsList) {
|
|
using MethodType = void (*)(Args...);
|
|
MethodType fptr = (MethodType)ptr;
|
|
{
|
|
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*>();
|
|
}
|
|
*/
|
|
//fptr((++param)->val, (int)(++param)->val, (int&)(++param)->val);
|
|
//fptr(std::forward<Args>((Args)(++param)->val)...);
|
|
int x = 1;
|
|
x = x + 1;
|
|
}
|
|
int main() {
|
|
auto cls = &TypeInfo<vec3>::StaticClass;
|
|
auto cls2 = &TypeInfo<int>::StaticClass;
|
|
auto cls3 = &TypeInfo<int*>::StaticClass;
|
|
auto cls4 = &TypeInfo<void*>::StaticClass;
|
|
auto cls5 = &TypeInfo<decltype(&vec3::norm)>::StaticClass;
|
|
vec3 v;
|
|
int x = 1, y = 2;
|
|
std::vector<ArgsView> ArgsList;
|
|
ArgsList.emplace_back();
|
|
ArgsList.emplace_back(v);
|
|
ArgsList.emplace_back(x);
|
|
ArgsList.emplace_back(y);
|
|
auto ptr1 = &vec3::norm;
|
|
using MethodType = void (*)(void*, int, int&);
|
|
MethodType ptr2 = *(MethodType*)&ptr1;
|
|
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);
|
|
//ptr2(x, y);
|
|
cout << "hello world\n";
|
|
} |