From deba7160c2fa562489a9207172914d93a00073f9 Mon Sep 17 00:00:00 2001 From: ouczbs Date: Mon, 8 Apr 2024 21:01:50 +0800 Subject: [PATCH] update refl --- .../zlib/include/refl/{ => detail}/field.h | 0 .../zlib/include/refl/{ => detail}/type.h | 8 +- .../zlib/include/refl/detail/uclass.h | 124 ++++++++ .../zlib/include/refl/detail/uclass.inl | 113 +++++++ .../zlib/include/refl/{ => detail}/view.h | 2 +- .../zlib/include/refl/detail/view.inl | 126 ++++++++ engine/3rdparty/zlib/include/refl/refl.h | 2 + engine/3rdparty/zlib/include/refl/uclass.h | 297 ------------------ engine/3rdparty/zlib/test/refl_01.cpp | 108 ++----- engine/3rdparty/zlib/xmake.lua | 2 +- engine/logs/test.log | 99 ++++++ engine/logs/test2.log | 30 ++ 12 files changed, 525 insertions(+), 386 deletions(-) rename engine/3rdparty/zlib/include/refl/{ => detail}/field.h (100%) rename engine/3rdparty/zlib/include/refl/{ => detail}/type.h (76%) create mode 100644 engine/3rdparty/zlib/include/refl/detail/uclass.h create mode 100644 engine/3rdparty/zlib/include/refl/detail/uclass.inl rename engine/3rdparty/zlib/include/refl/{ => detail}/view.h (97%) create mode 100644 engine/3rdparty/zlib/include/refl/detail/view.inl create mode 100644 engine/3rdparty/zlib/include/refl/refl.h delete mode 100644 engine/3rdparty/zlib/include/refl/uclass.h create mode 100644 engine/logs/test.log create mode 100644 engine/logs/test2.log diff --git a/engine/3rdparty/zlib/include/refl/field.h b/engine/3rdparty/zlib/include/refl/detail/field.h similarity index 100% rename from engine/3rdparty/zlib/include/refl/field.h rename to engine/3rdparty/zlib/include/refl/detail/field.h diff --git a/engine/3rdparty/zlib/include/refl/type.h b/engine/3rdparty/zlib/include/refl/detail/type.h similarity index 76% rename from engine/3rdparty/zlib/include/refl/type.h rename to engine/3rdparty/zlib/include/refl/detail/type.h index bdfa4ef..95fed11 100644 --- a/engine/3rdparty/zlib/include/refl/type.h +++ b/engine/3rdparty/zlib/include/refl/detail/type.h @@ -4,18 +4,18 @@ namespace refl { template concept _ReflCheck_Ctor = requires { T(); }; template - concept _ReflCheck_UClass = requires{{T::UClass::BuildClass()} -> std::same_as; }; + concept _ReflCheck_UClass = requires{{T::BuildClass()} -> std::same_as; }; template concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor && !_ReflCheck_UClass; - + //类型接口 template struct TypeInfoImpl; template<_ReflCheck_UClass T> struct TypeInfoImpl { - using UClass = typename T::UClass; - inline static UClass StaticClass = UClass::BuildClass(); + using MyUClass = typename T::MyUClass; + inline static MyUClass StaticClass = T::BuildClass(); }; template diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.h b/engine/3rdparty/zlib/include/refl/detail/uclass.h new file mode 100644 index 0000000..d8d37a3 --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.h @@ -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(*GetParams)(const UClass*); + //function + void (*Call)(const FieldPtr&, std::vector&); + + }; + 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 + T* New(T* ptr = nullptr) const{ + if (!IsChildOf()) { + 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 + bool IsChildOf(bool bthis = false) const { + constexpr UClass* cls = &TypeInfo::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 GetParams() const { + if (vtable.GetParams) { + return vtable.GetParams(this); + } + return{}; + } + void Call(const FieldPtr& field, std::vector& ArgsList)const{ + if (vtable.Call) { + return vtable.Call(field, ArgsList); + } + } + public: + template + 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 + static FieldPtr MakeMethodField(R (* ptr)(Args...), Name name) { + FieldPtr::Data member = { *(Method*)&ptr}; + constexpr uint32_t flag = FIELD_METHOD_FLAG; + return { name, &TypeInfo::type...)>::StaticClass, {member},flag }; + } + template + 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::type...)>::StaticClass, {member},flag }; + } + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl new file mode 100644 index 0000000..90df5da --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -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; + public: + constexpr static UClassType BuildClass() { + UClassType cls(type_name().View(), sizeof(T)); + if constexpr (!std::is_trivially_copyable_v) { + cls.vtable.InitObject = &UClass::InitObject; + } + return cls; + } + }; + + /* + * 模板优化 + * 成员参数转化为const void* + * 引用参数转化为指针 + */ + template + class UMethod_Auto : public UClass { + using UClass::UClass; + using MethodType = R(*)(Args...); + using UClassType = UMethod_Auto; + public: + std::array UList{}; + + static std::vector GetParams(const UClass* cls) { + auto& UList = static_cast(cls)->UList; + return { UList.data(), UList.data() + UList.size() }; + } + static void Call(const FieldPtr& field, std::vector& ArgsList) { + if constexpr (std::is_same_v) { + MethodType fptr = (MethodType)std::get(field.data); + auto param = ArgsList.rbegin(); + fptr(param++->cast_to()...); + } + else { + MethodType fptr = (MethodType)std::get(field.data); + auto param = ArgsList.rbegin(); + auto ret = ArgsList.begin(); + if (ret->cls == &TypeInfo::StaticClass) { + *(R*)ret->val = fptr(param++->cast_to()...); + } + else { + fptr(param++->cast_to()...); + } + } + } + protected: + constexpr void BuildUList() { + if constexpr (!std::is_same_v) { + UList[0] = &TypeInfo::StaticClass; + } + if constexpr (sizeof...(Args) > 0) { + auto ptr = &UList[1]; + (..., (*ptr = &TypeInfo*>::StaticClass, ptr++)); + } + } + public: + //为了简化判断,cls 对象 统统指向 T* + constexpr static UClassType BuildClass() { + UClassType cls(type_name().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 FList{}; + int32_t AttrNum{ 0 }; + + static const FieldPtr* GetField(const UClass* _cls,const Name& name, bool isMethod){ + auto cls = static_cast(_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 { + using UClass = UClass_Auto; + inline constexpr static UClass StaticClass = UClass::BuildClass(); + }; + + // 函数指针类型的偏特化 + template + struct TypeInfoImpl { + using UClass = UMethod_Auto; + inline constexpr static UClass StaticClass = UClass::BuildClass(); + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/view.h b/engine/3rdparty/zlib/include/refl/detail/view.h similarity index 97% rename from engine/3rdparty/zlib/include/refl/view.h rename to engine/3rdparty/zlib/include/refl/detail/view.h index bc0b88e..2bd19fd 100644 --- a/engine/3rdparty/zlib/include/refl/view.h +++ b/engine/3rdparty/zlib/include/refl/detail/view.h @@ -35,7 +35,7 @@ namespace refl { ArgsView(T&& v): val(&v), cls(&TypeInfo*>::StaticClass){} template - inline T cast_to() { + constexpr inline T cast_to() { if constexpr (std::is_pointer_v) { return (T)val; } diff --git a/engine/3rdparty/zlib/include/refl/detail/view.inl b/engine/3rdparty/zlib/include/refl/detail/view.inl new file mode 100644 index 0000000..6197250 --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/view.inl @@ -0,0 +1,126 @@ +#pragma once +#include "uclass.h" +namespace refl { + template + inline bool ObjectView::Get(const Name& name, T& t) + { + if (cache && cache->name == name) { + _cache_get: bool isChild = cache->type->IsChildOf(true); + if (isChild) { + t = *(T*)(ptr + std::get(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 + inline bool ObjectView::Set(const Name& name, const T& t) + { + if (cache && cache->name == name) { + + _cache_set: bool isChild = cache->type->IsChildOf(true); + if (isChild) { + *(T*)(ptr + std::get(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 + 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 ArgsList; + ArgsList.reserve(paramsSize); + ArgsList.emplace_back(); + if (member) { + ArgsList.emplace_back(ptr, &TypeInfo::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(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 ArgsList; + ArgsList.reserve(paramsSize); + ArgsList.emplace_back(ret); + if (member) { + ArgsList.emplace_back(ptr, &TypeInfo::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; + } +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/refl.h b/engine/3rdparty/zlib/include/refl/refl.h new file mode 100644 index 0000000..57a0983 --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -0,0 +1,2 @@ +#include "detail/uclass.inl" +#include "detail/view.inl" diff --git a/engine/3rdparty/zlib/include/refl/uclass.h b/engine/3rdparty/zlib/include/refl/uclass.h deleted file mode 100644 index 4a9a738..0000000 --- a/engine/3rdparty/zlib/include/refl/uclass.h +++ /dev/null @@ -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 - T* New(T* ptr = nullptr) const{ - if (!IsChildOf()) { - 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 - bool IsChildOf(bool bthis = false) const { - constexpr UClass* cls = &TypeInfo::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 GetParams() const { - return{}; - } - virtual void Call(const FieldPtr& field, std::vector& ArgsList)const{} - protected: - template - 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 - FieldPtr MakeMethodField(R (* ptr)(Args...), Name name) { - FieldPtr::Data member = { *(Method*)&ptr}; - constexpr uint32_t flag = FIELD_METHOD_FLAG; - return { name, &TypeInfo::type...)>::StaticClass, {member},flag }; - } - template - 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::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 obj{}; - std::construct_at((T*)ptr, obj); - } - else { - memset(ptr, 0, size); - } - } - public: - constexpr static UClass_Auto BuildClass() { - auto cls = UClass_Auto(type_name().View(), sizeof(T)); - return cls; - } - }; - - template<_ReflCheck_Ctor_NoUClass T> - struct TypeInfoImpl{ - using UClass = UClass_Auto; - inline constexpr static UClass StaticClass = UClass::BuildClass(); - }; - - /* - * 模板优化 - * 成员参数转化为const void* - * 引用参数转化为指针 - */ - template - class UMethod_Auto : public UClass { - using UClass::UClass; - using MethodType = R(*)(Args...); - public: - std::array UList{}; - - std::vector GetParams()const override { - return { UList.data(), UList.data() + UList.size()}; - } - void Call(const FieldPtr& field, std::vector& ArgsList)const override { - if constexpr (std::is_same_v) { - MethodType fptr = (MethodType)std::get(field.data); - auto param = ArgsList.rbegin(); - fptr(param++->cast_to()...); - } - else { - MethodType fptr = (MethodType)std::get(field.data); - auto param = ArgsList.rbegin(); - auto ret = ArgsList.begin(); - if (ret->cls == &TypeInfo::StaticClass) { - *(R*)ret->val = fptr(param++->cast_to()...); - } - else { - fptr(param++->cast_to()...); - } - } - } - public: - //为了简化判断,cls 对象 统统指向 T* - static UMethod_Auto BuildClass() { - UMethod_Auto cls(type_name().View(), sizeof(MethodType)); - if constexpr(!std::is_same_v) { - cls.UList[0] = &TypeInfo::StaticClass; - } - if (sizeof...(Args) > 0) { - auto ptr = &cls.UList[1]; - (...,(*ptr = &TypeInfo*>::StaticClass, ptr++)); - } - return cls; - } - }; - // 函数指针类型的偏特化 - template - struct TypeInfoImpl { - using UClass = UMethod_Auto; - inline static UClass StaticClass = UClass::BuildClass(); - }; - template - inline bool ObjectView::Get(const Name& name, T& t) - { - if (cache && cache->name == name) { -_cache_get: bool isChild = cache->type->IsChildOf(true); - if (isChild) { - t = *(T*)(ptr + std::get(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 - inline bool ObjectView::Set(const Name& name, const T& t) - { - if (cache && cache->name == name) { - -_cache_set: bool isChild = cache->type->IsChildOf(true); - if (isChild) { - *(T*)(ptr + std::get(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 - 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 ArgsList; - ArgsList.reserve(paramsSize); - ArgsList.emplace_back(); - if (member) { - ArgsList.emplace_back(ptr, &TypeInfo::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(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 ArgsList; - ArgsList.reserve(paramsSize); - ArgsList.emplace_back(ret); - if (member) { - ArgsList.emplace_back(ptr, &TypeInfo::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; - } -} \ No newline at end of file diff --git a/engine/3rdparty/zlib/test/refl_01.cpp b/engine/3rdparty/zlib/test/refl_01.cpp index 0c8fdc7..0a3f721 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -1,6 +1,6 @@ #include #include -#include "refl/uclass.h" +#include "refl/refl.h" #include "vertex.h" #include "tstring.h" #include @@ -13,11 +13,14 @@ 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)override { + x2 = x1 * x2; + cout << x2 << "vec3::norm" << endl; + } virtual float norm1(int x1 = 10) { cout << x1 << "::norm1" << endl; return x * y *z * x1; @@ -25,93 +28,32 @@ struct vec3 : public vec3_parent { static void norm2(int x1 = 10) { cout << x1 << "::norm2" << endl; } - void norm3(int x1 = 10) { - x1 = x * y * z; + static void norm3(int x1 = 10) { + x1 = x1 * 10; cout << x1 << "::norm3" << endl; } -}; -struct vec3_UClass : public UClass { -public: - array 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().View(), sizeof(vec3), &TypeInfo::StaticClass); - //offsetof(vec3, x) 很坑,相当坑 - cls.AttrNum = 3; - cls.FList[0] = cls.MakeMemberField("x", &TypeInfo::StaticClass); - cls.FList[1] = cls.MakeMemberField("y", &TypeInfo::StaticClass); - cls.FList[2] = cls.MakeMemberField("z", &TypeInfo::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"); + using MyUClass = UClass_Custom; + static MyUClass BuildClass() { + MyUClass cls(type_name().View(), sizeof(vec3), &TypeInfo::StaticClass); + auto& FList = cls.FList; + FList.reserve(6); + FList.push_back(cls.MakeMemberField("x", &TypeInfo::StaticClass)); + FList.push_back(cls.MakeMemberField("y", &TypeInfo::StaticClass)); + FList.push_back(cls.MakeMemberField("z", &TypeInfo::StaticClass)); + cls.AttrNum = FList.size(); + FList.push_back(cls.MakeMethodField(&vec3::norm, "norm")); + FList.push_back(cls.MakeMethodField(&vec3::norm, "norm1")); + cls.vtable.InitObject = &UClass::InitObject; + cls.BuildClass(); return cls; } }; -template -void printt(void* ptr, std::vector& ArgsList) { - using MethodType = void (*)(Args...); - MethodType fptr = (MethodType)ptr; - { - auto param = ArgsList.rbegin(); - fptr(param++->cast_to()...); - //fptr((Args)param++->val...); - } - /* { - auto param = ArgsList.begin(); - auto val1 = (++param); - auto val2 = (++param); - auto val3 = (++param); - fptr(val1->cast_to(), val1->cast_to(), val1->cast_to(); - } - */ - //fptr((++param)->val, (int)(++param)->val, (int&)(++param)->val); - //fptr(std::forward((Args)(++param)->val)...); - int x = 1; - x = x + 1; -} int main() { - auto cls = &TypeInfo::StaticClass; - auto cls2 = &TypeInfo::StaticClass; - auto cls3 = &TypeInfo::StaticClass; - auto cls4 = &TypeInfo::StaticClass; - auto cls5 = &TypeInfo::StaticClass; - vec3 v; - int x = 1, y = 2; - std::vector 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); + auto cls = &TypeInfo::StaticClass; + int x = 100, y = 2; + auto ov = cls->New(); ov.Invoke("norm", x, y); - printt(*(void**)&ptr1, ArgsList); - //ptr2(x, y); + vec3* v = (vec3*)ov.ptr; + delete v; cout << "hello world\n"; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/xmake.lua b/engine/3rdparty/zlib/xmake.lua index d512e3b..cf90af2 100644 --- a/engine/3rdparty/zlib/xmake.lua +++ b/engine/3rdparty/zlib/xmake.lua @@ -5,7 +5,7 @@ target("zlib") add_packages("UTemplate", {public = true}) add_includedirs("include", {public = true}) add_files("src/**/*.cpp") - add_headerfiles("include/**/*.h") + add_headerfiles("include/**/*.h", "include/**/*.inl") target("zlib_test") set_kind("binary") add_deps("zlib") diff --git a/engine/logs/test.log b/engine/logs/test.log new file mode 100644 index 0000000..d579c15 --- /dev/null +++ b/engine/logs/test.log @@ -0,0 +1,99 @@ +#include +#include +#include "refl/uclass.h" +#include "vertex.h" +#include "tstring.h" +#include +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 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().View(), sizeof(vec3), &TypeInfo::StaticClass); + //offsetof(vec3, x) 很坑,相当坑 + cls.FList[0] = cls.MakeMemberField("x"); + cls.FList[1] = cls.MakeMemberField("y"); + cls.FList[2] = cls.MakeMemberField("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 +void printt(void* ptr, std::vector& 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)(++param)->val)...); + int x = 1; + x = x + 1; +} +int main() { + auto cls = &TypeInfo::StaticClass; + vec3 v; + int x = 1, y = 2; + std::vector 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(*(void**)&ptr1, ArgsList); + //ptr2(x, y); + //cout << "hello world\n"; +} \ No newline at end of file diff --git a/engine/logs/test2.log b/engine/logs/test2.log new file mode 100644 index 0000000..2bb76bb --- /dev/null +++ b/engine/logs/test2.log @@ -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 > > >::operator++ (07FF761855E89h) +00007FF76185D308 lea rcx,[rsp+70h] +00007FF76185D30D call std::reverse_iterator > > >::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 > > >::operator++ (07FF761855E89h) +00007FF76185D32C lea rcx,[rsp+68h] +00007FF76185D331 call std::reverse_iterator > > >::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 > > >::operator++ (07FF761855E89h) +00007FF76185D34F lea rcx,[rsp+60h] +00007FF76185D354 call std::reverse_iterator > > >::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 \ No newline at end of file