update refl

This commit is contained in:
ouczbs 2024-04-08 21:01:50 +08:00
parent c6ca4f12ec
commit deba7160c2
12 changed files with 525 additions and 386 deletions

View File

@ -4,18 +4,18 @@ namespace refl {
template <class T> template <class T>
concept _ReflCheck_Ctor = requires { T(); }; concept _ReflCheck_Ctor = requires { T(); };
template <class T> template <class T>
concept _ReflCheck_UClass = requires{{T::UClass::BuildClass()} -> std::same_as<typename T::UClass>; }; concept _ReflCheck_UClass = requires{{T::BuildClass()} -> std::same_as<typename T::MyUClass>; };
template <class T> template <class T>
concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor<T> && !_ReflCheck_UClass<T>; concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor<T> && !_ReflCheck_UClass<T>;
//类型接口 //类型接口
template<typename T> template<typename T>
struct TypeInfoImpl; struct TypeInfoImpl;
template<_ReflCheck_UClass T> template<_ReflCheck_UClass T>
struct TypeInfoImpl<T> { struct TypeInfoImpl<T> {
using UClass = typename T::UClass; using MyUClass = typename T::MyUClass;
inline static UClass StaticClass = UClass::BuildClass(); inline static MyUClass StaticClass = T::BuildClass();
}; };
template<typename T> template<typename T>

View File

@ -0,0 +1,124 @@
#pragma once
#include "type.h"
#include "field.h"
#include "view.h"
namespace refl {
struct vtable_uclass
{
//object
void (*InitObject)(void*);
//class
const FieldPtr* (*GetField)(const UClass*, const Name&, bool);
//function
std::vector<const UClass*>(*GetParams)(const UClass*);
//function
void (*Call)(const FieldPtr&, std::vector<ArgsView>&);
};
class UClass{
using enum FieldFlag;
public:
Name name;
uint32_t size;
//uint32_t flag;
const UClass* parent;
vtable_uclass vtable{};
public:
constexpr UClass(std::string_view name, uint32_t size, const UClass* parent = nullptr)
:name(name), size(size), parent(parent){}
ObjectView New(void* ptr = nullptr) const{
if (ptr == nullptr) {
ptr = malloc(size);
InitObject(ptr);
}
return { ptr , this };
}
template<typename T>
T* New(T* ptr = nullptr) const{
if (!IsChildOf<T>()) {
return nullptr;
}
if (ptr == nullptr) {
ptr = (T*)malloc(size);
}
InitObject(ptr);
return ptr;
}
bool IsChildOf(const UClass* cls) const {
const UClass* _parent = parent;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
template<typename T>
bool IsChildOf(bool bthis = false) const {
constexpr UClass* cls = &TypeInfo<T>::StaticClass;
if (bthis) {
return cls == this;
}
const UClass* _parent = this;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
public:
template<_ReflCheck_Ctor T>
static void InitObject(void* ptr) {
T obj{};
std::construct_at((T*)ptr, obj);
}
//unsafe if called by other, need check class && size
void InitObject(void* ptr)const {
if (vtable.InitObject) {
vtable.InitObject(ptr);
}
else {
memset(ptr, 0, size);
}
}
const FieldPtr* GetField(const Name& name, bool isMethod)const {
if (vtable.GetField) {
return vtable.GetField(this, name, isMethod);
}
return nullptr;
}
std::vector<const UClass*> GetParams() const {
if (vtable.GetParams) {
return vtable.GetParams(this);
}
return{};
}
void Call(const FieldPtr& field, std::vector<ArgsView>& ArgsList)const{
if (vtable.Call) {
return vtable.Call(field, ArgsList);
}
}
public:
template<Offset offset>
constexpr static FieldPtr MakeMemberField(Name name, const UClass* cls) {
FieldPtr::Data member = { offset };
constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG;
return { name, cls, {member}, flag };
}
template<typename R, typename ...Args>
static FieldPtr MakeMethodField(R (* ptr)(Args...), Name name) {
FieldPtr::Data member = { *(Method*)&ptr};
constexpr uint32_t flag = FIELD_METHOD_FLAG;
return { name, &TypeInfo<R(*)(typename RealTypeImpl<Args>::type...)>::StaticClass, {member},flag };
}
template<typename T, typename R, typename ...Args>
static FieldPtr MakeMethodField(R(T::* ptr)(Args...), Name name) {
FieldPtr::Data member = { *(Method*)&ptr };
constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG;
return { name, &TypeInfo<R(*)(const void*, typename RealTypeImpl<Args>::type...)>::StaticClass, {member},flag };
}
};
}

View File

@ -0,0 +1,113 @@
#pragma once
#include "uclass.h"
namespace refl {
template<_ReflCheck_Ctor T>
class UClass_Auto : public UClass {
public:
using UClass::UClass;
using UClassType = UClass_Auto<T>;
public:
constexpr static UClassType BuildClass() {
UClassType cls(type_name<T>().View(), sizeof(T));
if constexpr (!std::is_trivially_copyable_v<T>) {
cls.vtable.InitObject = &UClass::InitObject<T>;
}
return cls;
}
};
/*
* 模板优化
* 成员参数转化为const void*
* 引用参数转化为指针
*/
template<typename R, typename... Args>
class UMethod_Auto : public UClass {
using UClass::UClass;
using MethodType = R(*)(Args...);
using UClassType = UMethod_Auto<R, Args...>;
public:
std::array<const UClass*, sizeof...(Args) + 1> UList{};
static std::vector<const UClass*> GetParams(const UClass* cls) {
auto& UList = static_cast<const UClassType*>(cls)->UList;
return { UList.data(), UList.data() + UList.size() };
}
static void Call(const FieldPtr& field, std::vector<ArgsView>& ArgsList) {
if constexpr (std::is_same_v<R, void>) {
MethodType fptr = (MethodType)std::get<Method>(field.data);
auto param = ArgsList.rbegin();
fptr(param++->cast_to<Args>()...);
}
else {
MethodType fptr = (MethodType)std::get<Method>(field.data);
auto param = ArgsList.rbegin();
auto ret = ArgsList.begin();
if (ret->cls == &TypeInfo<R>::StaticClass) {
*(R*)ret->val = fptr(param++->cast_to<Args>()...);
}
else {
fptr(param++->cast_to<Args>()...);
}
}
}
protected:
constexpr void BuildUList() {
if constexpr (!std::is_same_v<R, void>) {
UList[0] = &TypeInfo<R>::StaticClass;
}
if constexpr (sizeof...(Args) > 0) {
auto ptr = &UList[1];
(..., (*ptr = &TypeInfo<typename std::remove_pointer_t<Args>*>::StaticClass, ptr++));
}
}
public:
//为了简化判断cls 对象 统统指向 T*
constexpr static UClassType BuildClass() {
UClassType cls(type_name<MethodType>().View(), sizeof(MethodType));
cls.vtable.GetParams = &UClassType::GetParams;
cls.vtable.Call = &UClassType::Call;
cls.BuildUList();
return cls;
}
};
class UClass_Custom : public UClass {
public:
using UClass::UClass;
std::vector<FieldPtr> FList{};
int32_t AttrNum{ 0 };
static const FieldPtr* GetField(const UClass* _cls,const Name& name, bool isMethod){
auto cls = static_cast<const UClass_Custom*>(_cls);
auto& FList = cls->FList;
// 指定开始位置的迭代器
auto start = FList.begin();
if (isMethod) {
start += cls->AttrNum;
}
auto end = FList.end();
for (auto it = start; it != end; ++it) {
if (it->name == name) {
return &*it;
}
}
return nullptr;
}
void BuildClass() {
vtable.GetField = &UClass_Custom::GetField;
}
};
//基础类型的偏特化
template<_ReflCheck_Ctor_NoUClass T>
struct TypeInfoImpl<T> {
using UClass = UClass_Auto<T>;
inline constexpr static UClass StaticClass = UClass::BuildClass();
};
// 函数指针类型的偏特化
template<typename R, typename... Args>
struct TypeInfoImpl<R(*)(Args...)> {
using UClass = UMethod_Auto<R, Args...>;
inline constexpr static UClass StaticClass = UClass::BuildClass();
};
}

View File

@ -35,7 +35,7 @@ namespace refl {
ArgsView(T&& v): val(&v), cls(&TypeInfo<typename std::remove_reference_t<T>*>::StaticClass){} ArgsView(T&& v): val(&v), cls(&TypeInfo<typename std::remove_reference_t<T>*>::StaticClass){}
template<typename T> template<typename T>
inline T cast_to() { constexpr inline T cast_to() {
if constexpr (std::is_pointer_v<T>) { if constexpr (std::is_pointer_v<T>) {
return (T)val; return (T)val;
} }

View File

@ -0,0 +1,126 @@
#pragma once
#include "uclass.h"
namespace refl {
template<typename T>
inline bool ObjectView::Get(const Name& name, T& t)
{
if (cache && cache->name == name) {
_cache_get: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
t = *(T*)(ptr + std::get<uint32_t>(cache->data));
}
return isChild;
}
auto field = cls->GetField(name, false);
if (field) {
cache = field;
goto _cache_get;
}
if (cls->parent) {
return Parent().Get(name, t);
}
return false;
}
template<typename T>
inline bool ObjectView::Set(const Name& name, const T& t)
{
if (cache && cache->name == name) {
_cache_set: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
*(T*)(ptr + std::get<uint32_t>(cache->data)) = t;
}
return isChild;
}
auto field = cls->GetField(name, false);
if (field) {
cache = field;
goto _cache_set;
}
if (cls->parent) {
return Parent().Set(name, t);
}
return false;
}
template<typename ...Args>
bool ObjectView::Invoke(const Name& name, Args&&... args)
{
auto field = cls->GetField(name, true);
if (!field) {
if (cls->parent) {
return Parent().Invoke(name, args...);
}
return false;
}
constexpr int inputSize = sizeof...(Args);
auto params = field->type->GetParams();
int paramsSize = params.size();
bool member = field->flag & FIELD_MEMBER_FLAG;
if (inputSize + member >= paramsSize) {
return false;
}
std::vector<ArgsView> ArgsList;
ArgsList.reserve(paramsSize);
ArgsList.emplace_back();
if (member) {
ArgsList.emplace_back(ptr, &TypeInfo<const void*>::StaticClass);
}
(..., (ArgsList.emplace_back(args)));
for (int i = member + 1; i < paramsSize; i++) {
if (ArgsList[i].cls != params[i] && !ArgsList[i].ConvertTo(params[i])) {
return false;
}
}
field->type->Call(*field, ArgsList);
return true;
}
template<_ReflCheck_Ctor R, typename ...Args>
R ObjectView::Invoke(const Name& name, Args&&... args)
{
auto field = cls->GetField(name, true);
if (!field) {
if (cls->parent) {
return Parent().Invoke<R>(name, args...);
}
return {};
}
R ret{};
constexpr int inputSize = sizeof...(Args);
auto params = field->type->GetParams();
int paramsSize = params.size();
bool member = field->flag & FIELD_MEMBER_FLAG;
if (inputSize + member >= paramsSize) {
return false;
}
std::vector<ArgsView> ArgsList;
ArgsList.reserve(paramsSize);
ArgsList.emplace_back(ret);
if (member) {
ArgsList.emplace_back(ptr, &TypeInfo<const void*>::StaticClass);
}
(..., (ArgsList.emplace_back(args)));
for (int i = 0; i < paramsSize; i++) {
if (ArgsList[i].cls != params[i] && !ArgsList[i].ConvertTo(params[i])) {
return ret;
}
}
field->type->Call(*field, ArgsList);
return ret;
}
ObjectView ObjectView::Parent() {
return { ptr, cls ? cls->parent : nullptr };
}
/*平凡对象的转化要支持吗
* 比如同大小的安全转化
* 对象从小到大
* 对象从大到小
*/
bool ArgsView::ConvertTo(const UClass* toClass) {
//子类转父类
if (cls->IsChildOf(toClass)) {
cls = toClass;
return true;
}
return false;
}
}

View File

@ -0,0 +1,2 @@
#include "detail/uclass.inl"
#include "detail/view.inl"

View File

@ -1,297 +0,0 @@
#pragma once
#include "type.h"
#include "field.h"
#include "view.h"
namespace refl {
class UClass{
using enum FieldFlag;
public:
Name name;
uint32_t size;
//uint32_t flag;
const UClass* parent;
public:
constexpr UClass(std::string_view name, uint32_t size, const UClass* parent = nullptr)
:name(name), size(size), parent(parent){}
ObjectView New(void* ptr = nullptr) const{
if (ptr == nullptr) {
ptr = malloc(size);
InitObject(ptr);
}
return { ptr , this };
}
template<typename T>
T* New(T* ptr = nullptr) const{
if (!IsChildOf<T>()) {
return nullptr;
}
if (ptr == nullptr) {
ptr = (T*)malloc(size);
}
InitObject(ptr);
return ptr;
}
bool IsChildOf(const UClass* cls) const {
const UClass* _parent = parent;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
template<typename T>
bool IsChildOf(bool bthis = false) const {
constexpr UClass* cls = &TypeInfo<T>::StaticClass;
if (bthis) {
return cls == this;
}
const UClass* _parent = this;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
public:
//unsafe if called by other, need check class && size
virtual void InitObject(void* ptr)const {
memset(ptr, 0, size);
}
virtual const FieldPtr* GetField(const Name& name, bool isMethod)const {
return nullptr;
}
virtual std::vector<const UClass*> GetParams() const {
return{};
}
virtual void Call(const FieldPtr& field, std::vector<ArgsView>& ArgsList)const{}
protected:
template<Offset offset>
FieldPtr MakeMemberField(Name name, const UClass* cls) {
FieldPtr::Data member = { offset };
constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG;
return { name, cls, {member}, flag };
}
template<typename R, typename ...Args>
FieldPtr MakeMethodField(R (* ptr)(Args...), Name name) {
FieldPtr::Data member = { *(Method*)&ptr};
constexpr uint32_t flag = FIELD_METHOD_FLAG;
return { name, &TypeInfo<R(*)(typename RealTypeImpl<Args>::type...)>::StaticClass, {member},flag };
}
template<typename T, typename R, typename ...Args>
FieldPtr MakeMethodField(R(T::* ptr)(Args...), Name name) {
FieldPtr::Data member = { *(Method*)&ptr };
constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG;
return { name, &TypeInfo<R(*)(const void*, typename RealTypeImpl<Args>::type...)>::StaticClass, {member},flag };
}
};
template<_ReflCheck_Ctor T>
class UClass_Auto : public UClass{
public:
using UClass::UClass;
protected:
void InitObject(void* ptr)const override {
if constexpr (!std::is_trivially_copyable_v<T>){
T obj{};
std::construct_at((T*)ptr, obj);
}
else {
memset(ptr, 0, size);
}
}
public:
constexpr static UClass_Auto<T> BuildClass() {
auto cls = UClass_Auto<T>(type_name<T>().View(), sizeof(T));
return cls;
}
};
template<_ReflCheck_Ctor_NoUClass T>
struct TypeInfoImpl<T>{
using UClass = UClass_Auto<T>;
inline constexpr static UClass StaticClass = UClass::BuildClass();
};
/*
*
* const void*
*
*/
template<typename R, typename... Args>
class UMethod_Auto : public UClass {
using UClass::UClass;
using MethodType = R(*)(Args...);
public:
std::array<const UClass*, sizeof...(Args) + 1> UList{};
std::vector<const UClass*> GetParams()const override {
return { UList.data(), UList.data() + UList.size()};
}
void Call(const FieldPtr& field, std::vector<ArgsView>& ArgsList)const override {
if constexpr (std::is_same_v<R, void>) {
MethodType fptr = (MethodType)std::get<Method>(field.data);
auto param = ArgsList.rbegin();
fptr(param++->cast_to<Args>()...);
}
else {
MethodType fptr = (MethodType)std::get<Method>(field.data);
auto param = ArgsList.rbegin();
auto ret = ArgsList.begin();
if (ret->cls == &TypeInfo<R>::StaticClass) {
*(R*)ret->val = fptr(param++->cast_to<Args>()...);
}
else {
fptr(param++->cast_to<Args>()...);
}
}
}
public:
//为了简化判断cls 对象 统统指向 T*
static UMethod_Auto<R,Args...> BuildClass() {
UMethod_Auto<R,Args...> cls(type_name<MethodType>().View(), sizeof(MethodType));
if constexpr(!std::is_same_v<R, void>) {
cls.UList[0] = &TypeInfo<R>::StaticClass;
}
if (sizeof...(Args) > 0) {
auto ptr = &cls.UList[1];
(...,(*ptr = &TypeInfo<typename std::remove_pointer_t<Args>*>::StaticClass, ptr++));
}
return cls;
}
};
// 函数指针类型的偏特化
template<typename R, typename... Args>
struct TypeInfoImpl<R(*)(Args...)> {
using UClass = UMethod_Auto<R,Args...>;
inline static UClass StaticClass = UClass::BuildClass();
};
template<typename T>
inline bool ObjectView::Get(const Name& name, T& t)
{
if (cache && cache->name == name) {
_cache_get: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
t = *(T*)(ptr + std::get<uint32_t>(cache->data));
}
return isChild;
}
auto field = cls->GetField(name, false);
if (field) {
cache = field;
goto _cache_get;
}
if (cls->parent) {
return Parent().Get(name, t);
}
return false;
}
template<typename T>
inline bool ObjectView::Set(const Name& name, const T& t)
{
if (cache && cache->name == name) {
_cache_set: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
*(T*)(ptr + std::get<uint32_t>(cache->data)) = t;
}
return isChild;
}
auto field = cls->GetField(name, false);
if (field) {
cache = field;
goto _cache_set;
}
if (cls->parent) {
return Parent().Set(name, t);
}
return false;
}
template<typename ...Args>
bool ObjectView::Invoke(const Name& name, Args&&... args)
{
auto field = cls->GetField(name, true);
if (!field) {
if (cls->parent) {
return Parent().Invoke(name, args...);
}
return false;
}
constexpr int inputSize = sizeof...(Args);
auto params = field->type->GetParams();
int paramsSize = params.size();
bool member = field->flag & FIELD_MEMBER_FLAG;
if (inputSize + member >= paramsSize) {
return false;
}
std::vector<ArgsView> ArgsList;
ArgsList.reserve(paramsSize);
ArgsList.emplace_back();
if (member) {
ArgsList.emplace_back(ptr, &TypeInfo<const void*>::StaticClass);
}
(..., (ArgsList.emplace_back(args)));
for (int i = member + 1; i < paramsSize; i++) {
if (ArgsList[i].cls != params[i]) {
if (!ArgsList[i].ConvertTo(params[i])) {
return false;
}
}
}
field->type->Call(*field, ArgsList);
return true;
}
template<_ReflCheck_Ctor R, typename ...Args>
R ObjectView::Invoke(const Name& name, Args&&... args)
{
auto field = cls->GetField(name, true);
if (!field) {
if (cls->parent) {
return Parent().Invoke<R>(name, args...);
}
return {};
}
R ret{};
constexpr int inputSize = sizeof...(Args);
auto params = field->type->GetParams();
int paramsSize = params.size();
bool member = field->flag & FIELD_MEMBER_FLAG;
if (inputSize + member >= paramsSize) {
return false;
}
std::vector<ArgsView> ArgsList;
ArgsList.reserve(paramsSize);
ArgsList.emplace_back(ret);
if (member) {
ArgsList.emplace_back(ptr, &TypeInfo<const void*>::StaticClass);
}
(..., (ArgsList.emplace_back(args)));
for (int i = 0; i < paramsSize; i++) {
if (ArgsList[i].cls != params[i]) {
if (!ArgsList[i].ConvertTo(params[i])) {
return ret;
}
}
}
field->type->Call(*field, ArgsList);
return ret;
}
ObjectView ObjectView::Parent() {
return { ptr, cls ? cls->parent : nullptr };
}
/*平凡对象的转化要支持吗
*
*
*
*/
bool ArgsView::ConvertTo(const UClass* toClass) {
//子类转父类
if (cls->IsChildOf(toClass)) {
cls = toClass;
return true;
}
return false;
}
}

View File

@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include <array> #include <array>
#include "refl/uclass.h" #include "refl/refl.h"
#include "vertex.h" #include "vertex.h"
#include "tstring.h" #include "tstring.h"
#include <functional> #include <functional>
@ -13,11 +13,14 @@ struct vec3_parent {
} }
}; };
struct vec3 : public vec3_parent { struct vec3 : public vec3_parent {
using UClass = class vec3_UClass;
float x = 1; float x = 1;
float y = 2; float y = 2;
float z = 3; float z = 3;
string name{ "hellohellohellohellohellohello" }; string name{ "hellohellohellohellohellohello" };
void norm(int x1, int& x2)override {
x2 = x1 * x2;
cout << x2 << "vec3::norm" << endl;
}
virtual float norm1(int x1 = 10) { virtual float norm1(int x1 = 10) {
cout << x1 << "::norm1" << endl; cout << x1 << "::norm1" << endl;
return x * y *z * x1; return x * y *z * x1;
@ -25,93 +28,32 @@ struct vec3 : public vec3_parent {
static void norm2(int x1 = 10) { static void norm2(int x1 = 10) {
cout << x1 << "::norm2" << endl; cout << x1 << "::norm2" << endl;
} }
void norm3(int x1 = 10) { static void norm3(int x1 = 10) {
x1 = x * y * z; x1 = x1 * 10;
cout << x1 << "::norm3" << endl; cout << x1 << "::norm3" << endl;
} }
}; using MyUClass = UClass_Custom;
struct vec3_UClass : public UClass { static MyUClass BuildClass() {
public: MyUClass cls(type_name<vec3>().View(), sizeof(vec3), &TypeInfo<vec3_parent>::StaticClass);
array<FieldPtr,6> FList; auto& FList = cls.FList;
int32_t AttrNum; FList.reserve(6);
using UClass::UClass; FList.push_back(cls.MakeMemberField<offsetof(vec3, x)>("x", &TypeInfo<float>::StaticClass));
const FieldPtr* GetField(const Name& name, bool isMethod)const override { FList.push_back(cls.MakeMemberField<offsetof(vec3, y)>("y", &TypeInfo<float>::StaticClass));
// 指定开始位置的迭代器 FList.push_back(cls.MakeMemberField<offsetof(vec3, z)>("z", &TypeInfo<float>::StaticClass));
auto start = FList.begin(); cls.AttrNum = FList.size();
if (isMethod) { FList.push_back(cls.MakeMethodField(&vec3::norm, "norm"));
start += AttrNum; FList.push_back(cls.MakeMethodField(&vec3::norm, "norm1"));
} cls.vtable.InitObject = &UClass::InitObject<vec3>;
auto end = FList.end(); cls.BuildClass();
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; 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() { int main() {
auto cls = &TypeInfo<vec3>::StaticClass; auto cls = &TypeInfo<vec3>::StaticClass;
auto cls2 = &TypeInfo<int>::StaticClass; int x = 100, y = 2;
auto cls3 = &TypeInfo<int*>::StaticClass; auto ov = cls->New();
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); ov.Invoke("norm", x, y);
printt<const void*, int , int&>(*(void**)&ptr1, ArgsList); vec3* v = (vec3*)ov.ptr;
//ptr2(x, y); delete v;
cout << "hello world\n"; cout << "hello world\n";
} }

View File

@ -5,7 +5,7 @@ target("zlib")
add_packages("UTemplate", {public = true}) add_packages("UTemplate", {public = true})
add_includedirs("include", {public = true}) add_includedirs("include", {public = true})
add_files("src/**/*.cpp") add_files("src/**/*.cpp")
add_headerfiles("include/**/*.h") add_headerfiles("include/**/*.h", "include/**/*.inl")
target("zlib_test") target("zlib_test")
set_kind("binary") set_kind("binary")
add_deps("zlib") add_deps("zlib")

99
engine/logs/test.log Normal file
View File

@ -0,0 +1,99 @@
#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 {
};
struct vec3 : public vec3_parent {
using UClass = class vec3_UClass;
float x = 1;
float y = 2;
float z = 3;
string name{ "hellohellohellohellohellohello" };
void norm(int x1, int& x2) {
x2 = x1 * x * y * z * x2;
cout << x2 << "::norm" << endl;
}
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;
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<float, offsetof(vec3, x)>("x");
cls.FList[1] = cls.MakeMemberField<float, offsetof(vec3, y)>("y");
cls.FList[2] = cls.MakeMemberField<float, offsetof(vec3, z)>("z");
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((Args)param++->val...);
//fptr((Args)param++->val...);
}
{
auto param = ArgsList.begin();
auto val1 = (++param)->val;
auto val2 = (++param)->val;
auto val3 = (++param)->val;
fptr((void *)val1, (int)val2, (int*)val3);
}
//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;
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;
//ptr2(&v, x , y);
printt<const void*, int , int*>(*(void**)&ptr1, ArgsList);
//ptr2(x, y);
//cout << "hello world\n";
}

30
engine/logs/test2.log Normal file
View File

@ -0,0 +1,30 @@
00007FF76185D2E9 mov rax,qword ptr [fptr]
00007FF76185D2F1 mov qword ptr [rsp+30h],rax
00007FF76185D2F6 lea rcx,[rsp+78h]
00007FF76185D2FB lea rdx,[rsp+70h]
00007FF76185D300 xor r8d,r8d
00007FF76185D303 call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator++ (07FF761855E89h)
00007FF76185D308 lea rcx,[rsp+70h]
00007FF76185D30D call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator-> (07FF761856668h)
00007FF76185D312 mov rax,qword ptr [rax]
00007FF76185D315 mov qword ptr [rsp+28h],rax
00007FF76185D31A lea rcx,[rsp+78h]
00007FF76185D31F lea rdx,[rsp+68h]
00007FF76185D324 xor r8d,r8d
00007FF76185D327 call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator++ (07FF761855E89h)
00007FF76185D32C lea rcx,[rsp+68h]
00007FF76185D331 call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator-> (07FF761856668h)
00007FF76185D336 mov rax,qword ptr [rax]
00007FF76185D339 mov dword ptr [rsp+24h],eax
00007FF76185D33D lea rcx,[rsp+78h]
00007FF76185D342 lea rdx,[rsp+60h]
00007FF76185D347 xor r8d,r8d
00007FF76185D34A call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator++ (07FF761855E89h)
00007FF76185D34F lea rcx,[rsp+60h]
00007FF76185D354 call std::reverse_iterator<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<refl::ArgsView> > > >::operator-> (07FF761856668h)
00007FF76185D359 mov edx,dword ptr [rsp+24h]
00007FF76185D35D mov r8,qword ptr [rsp+28h]
00007FF76185D362 mov rcx,rax
00007FF76185D365 mov rax,qword ptr [rsp+30h]
00007FF76185D36A mov rcx,qword ptr [rcx]
00007FF76185D36D call rax