From 545a72a5de1a4671edd90a1c74011d6ad1de4492 Mon Sep 17 00:00:00 2001 From: ouczbs Date: Fri, 19 Apr 2024 22:02:27 +0800 Subject: [PATCH] rebuild refl --- .../3rdparty/zlib/include/refl/detail/any.h | 60 ++++++++++ .../3rdparty/zlib/include/refl/detail/field.h | 113 ++++-------------- .../zlib/include/refl/detail/field.inl | 59 ++------- .../3rdparty/zlib/include/refl/detail/meta.h | 45 +++++++ .../zlib/include/refl/detail/meta.inl | 70 +++++++++++ .../3rdparty/zlib/include/refl/detail/type.h | 6 +- .../zlib/include/refl/detail/uclass.h | 7 +- .../zlib/include/refl/detail/uclass.inl | 14 +-- .../3rdparty/zlib/include/refl/detail/view.h | 91 +++++--------- .../zlib/include/refl/detail/view.inl | 62 ++++++---- engine/3rdparty/zlib/include/refl/macro.h | 38 +----- engine/3rdparty/zlib/include/refl/refl.h | 53 ++++---- .../3rdparty/zlib/include/refl/std/sarray.h | 3 + .../3rdparty/zlib/include/refl/std/svector.h | 3 + engine/3rdparty/zlib/test/refl/vertex.h | 48 +++++--- engine/3rdparty/zlib/test/refl_01.cpp | 14 ++- 16 files changed, 371 insertions(+), 315 deletions(-) create mode 100644 engine/3rdparty/zlib/include/refl/detail/any.h create mode 100644 engine/3rdparty/zlib/include/refl/detail/meta.h create mode 100644 engine/3rdparty/zlib/include/refl/detail/meta.inl diff --git a/engine/3rdparty/zlib/include/refl/detail/any.h b/engine/3rdparty/zlib/include/refl/detail/any.h new file mode 100644 index 0000000..71e4e4c --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/any.h @@ -0,0 +1,60 @@ +#pragma once +#include "type.h" +namespace refl { + class UClass; + //生命周期短,适用于传参,不建议保存数据 + //只能指向指针,引用=>指针,指针=>指针,T => T*,T的类型丢失 + struct Any { + public: + const void* ptr; + const UClass* cls; + constexpr Any(const void* ptr,const UClass* cls) : ptr(ptr), cls(cls) {} + constexpr Any() : ptr(nullptr), cls(nullptr) {} + //右值=>右值压入栈,caller入栈地址 + //左值=>caller变量地址 + template + constexpr Any(T&& v) : ptr(&v), cls(&TypeInfo*>::StaticClass) { + if constexpr (std::is_same_v, Any>) { + ptr = v.ptr; + cls = v.cls; + } + } + template + constexpr Any(T* v) : ptr(v), cls(&TypeInfo*>::StaticClass) { + if constexpr (std::is_same_v, Any>) { + ptr = v->ptr; + cls = v->cls; + } + } + template//参数 T* => T* + constexpr inline T cast_to() const { + if constexpr (std::is_pointer_v) { + return (T)ptr; + } + else if constexpr (std::is_reference_v) { + using RT = std::remove_reference_t; + return *(RT*)ptr; + } + else { + return *(T*)ptr; + } + } + template + T cast_ret() const { + if constexpr (std::is_pointer_v) { + return *(T*)ptr; + } + else if constexpr (std::is_reference_v) { + using RT = std::remove_reference_t; + return **(RT**)ptr; + } + else { + return *(T*)ptr; + } + } + Any* ConvertTo(const UClass* toClass); + constexpr bool IsValid() const{ + return cls != nullptr && ptr != nullptr; + } + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/field.h b/engine/3rdparty/zlib/include/refl/detail/field.h index 943996f..f98ff6e 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.h +++ b/engine/3rdparty/zlib/include/refl/detail/field.h @@ -1,21 +1,29 @@ #pragma once #include "UTemplate/Type.hpp" #include "../std/sarray.h" +#include "any.h" #include #include using Ubpa::Name; using Ubpa::type_name; namespace refl { - class UClass; - class ArgsView; - class ArgsValue; - using Offset = uint32_t; using Method = void*; - using DefaultMember = const ArgsView (*)(); - using DefaultMethod = std::pair(*)(); - + struct MemberData { + Offset offset; + Any value; + Any meta; + constexpr MemberData() :offset(0), value(), meta() {} + constexpr MemberData(Offset offset,const Any& value,const Any& meta) : offset(offset), value(value), meta(meta) {} + }; + struct MethodData { + Method fptr; + sarray value; + Any meta; + constexpr MethodData() :fptr(nullptr), value(), meta() {} + constexpr MethodData(Method fptr,const sarray& value, const Any& meta) : fptr(fptr), value(value), meta(meta) {} + }; enum FieldFlag:uint32_t { FIELD_NONE_FLAG = 0, FIELD_MEMBER_FLAG = 1 << 0, @@ -27,99 +35,28 @@ namespace refl { struct FieldPtr { union Data { - Offset offset; - Method method; - constexpr Data() : method(nullptr) {} - constexpr Data(Offset& offset) : offset(offset) {} - constexpr Data(Method& method) : method(method) {} - constexpr Data(const Method& method) : method(method) {} - }; - union Default { - DefaultMember member; - DefaultMethod method; - constexpr Default(): method(nullptr) {} - constexpr Default(DefaultMember& member) : member(member) {} - constexpr Default(DefaultMethod& method) : method(method) {} + MemberData member; + MethodData method; + constexpr Data() : member() {}; + constexpr Data(const MemberData& member) :member(member) {} + constexpr Data(const MethodData& method) : method(method) {} + constexpr Data(Offset offset) :member(offset, {}, {}) {} + constexpr Data(Offset offset, const Any& value, const Any& meta):member(offset, value, meta){} + constexpr Data(Method fptr, const sarray& value, const Any& meta) : method(fptr, value, meta) {} }; Name name; const UClass* type{}; Data data{}; - Default value{}; uint32_t flag{}; //unsafe - bool Invoke(const sarray& ArgsList)const; + bool Invoke(const sarray& ArgsList)const; //safe - bool Invoke(svector& ArgsList)const; + bool Invoke(svector& ArgsList)const; template auto Call(Func func, Args&&... args)const; sarray GetParams() const; }; - template - consteval auto fetch_method_t(R(T::*)(Args...)) { - using MethodType = R(*)(const void*, Args...); - return MethodType{ nullptr }; - } - template - consteval auto fetch_method_t(R(*)(Args...)) { - using MethodType = R(*)(Args...); - return MethodType{ nullptr }; - } - template - consteval int fetch_args_size() { - auto fields = T::__MakeStaticFields(); - int size = 0; - for (auto& field : fields) { - size += field.data.offset; - } - return size; - } - template - class Field; - /* - template - consteval V* fetch_member_v(V T::*) { - return nullptr; - } - template - consteval auto fetch_method_v(R (T::* ptr)(Args...)) { - using MethodType = R(*)(T*, Args...); - return MethodType{nullptr}; - } - template - consteval auto fetch_method_v(R(*ptr)(Args...)) { - return ptr; - } - template - consteval auto FetchMembers(const std::tuple&) { - return std::make_tuple(fetch_member_v(Args{nullptr})...); - } - template - consteval auto FetchMethods(const std::tuple&) { - return std::make_tuple(fetch_method_v(Args{ nullptr })...); - } - template - class FieldMembers{ - public: - constexpr FieldMembers() {} - FieldMembers(const std::tuple&) { - - } - constexpr static int GetSize() { - return sizeof...(Args); - } - }; - template - class FieldMethods{ - public: - constexpr FieldMethods() {} - FieldMethods(const std::tuple&) { - - } - constexpr static int GetSize() { - return sizeof...(Args); - } - };*/ } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/field.inl b/engine/3rdparty/zlib/include/refl/detail/field.inl index 90d3f2a..d634bf1 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.inl +++ b/engine/3rdparty/zlib/include/refl/detail/field.inl @@ -4,24 +4,25 @@ namespace refl { inline auto FieldPtr::Call(Func func, Args&& ...args)const { using MemberFunc = decltype(fetch_method_t(func)); - MemberFunc fptr = (MemberFunc)data.method; + MemberFunc fptr = (MemberFunc)data.method.fptr; return fptr(std::forward(args)...); } - bool FieldPtr::Invoke(const sarray& ArgsList)const{ + bool FieldPtr::Invoke(const sarray& ArgsList)const{ auto Call = type->vtable.Call; if (Call) { Call(this, ArgsList); } return Call; } - bool FieldPtr::Invoke(svector& ArgsList)const { + bool FieldPtr::Invoke(svector& ArgsList)const { auto Call = type->vtable.Call; if (Call) { - auto params = GetParams(); + sarray params = GetParams(); int paramsSize = params.size(); int argsSize = ArgsList.size(); if (argsSize < paramsSize && flag & FIELD_METHOD_VALUE_FLAG) { - auto [argsPtr, valueSize] = value.method(); + const Any* argsPtr = data.method.value.front(); + int valueSize = data.method.value.size(); if (argsSize + valueSize >= paramsSize) { for (int i = valueSize + argsSize - paramsSize; i < valueSize; i++) { ArgsList.push_back(*(argsPtr + i)); @@ -32,8 +33,7 @@ namespace refl { if (argsSize < paramsSize) { return false; } - - auto a = ArgsList.front(); + Any* a = ArgsList.front(); auto p = params.front(); for (auto e = params.back(); p < e; ++p, ++a) { if (a->cls != *p && !a->ConvertTo(*p)) { @@ -51,49 +51,4 @@ namespace refl { } return{}; } - template - class Field { - public: - inline static char s_data[fetch_args_size()]{}; - inline static int s_index{0}; - template - static FieldPtr MakeField(V T::* ptr, Name name) - { - FieldPtr::Default value; - Offset offset = reinterpret_cast(&(reinterpret_cast(0)->*ptr)); - FieldPtr::Data member = { offset }; - constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG; - return { name, &TypeInfo::StaticClass, member, value, flag }; - } - template - static FieldPtr MakeField(R(*ptr)(Args...), Name name, const sarray& args = {}) { - uint32_t flag = FIELD_METHOD_FLAG; - FieldPtr::Default value; - if (args.size() > 0) { - flag |= FIELD_METHOD_VALUE_FLAG; - static const ArgsValueList argsValue(args, &Field::s_data[Field::s_index]); - Field::s_index += ArgsValueList::GetArgsSize(args); - value.method = []() ->std::pair { - return { argsValue.ptr , argsValue.num }; - }; - } - FieldPtr::Data method = { *(Method*)&ptr }; - return { name, &TypeInfo(*)(real_type_t...)>::StaticClass, method, value, flag }; - } - template - static FieldPtr MakeField(R(T::* ptr)(Args...), Name name, const sarray& args = {}) { - uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG; - FieldPtr::Default value; - if (args.size() > 0) { - flag |= FIELD_METHOD_VALUE_FLAG; - static const ArgsValueList argsValue(args, &s_data[s_index]); - s_index += ArgsValueList::GetArgsSize(args); - value.method = []() ->std::pair { - return { argsValue.ptr , argsValue.num }; - }; - } - FieldPtr::Data method = { *(Method*)&ptr }; - return { name, &TypeInfo(*)(const void*,real_type_t...)>::StaticClass, method, value,flag }; - } - }; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/meta.h b/engine/3rdparty/zlib/include/refl/detail/meta.h new file mode 100644 index 0000000..234407c --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/meta.h @@ -0,0 +1,45 @@ +#pragma once +#include "field.h" +namespace refl { + template + consteval auto fetch_method_t(R(T::*)(Args...)) { + using MethodType = R(*)(const void*, Args...); + return MethodType{ nullptr }; + } + template + consteval auto fetch_method_t(R(*)(Args...)) { + using MethodType = R(*)(Args...); + return MethodType{ nullptr }; + } + template + consteval int fetch_meta_size() { + auto fields = T::__MakeStaticFields(); + int size = 0; + for (auto& field : fields) { + size += field.data.member.offset; + } + return size; + } + enum MetaFlag :uint32_t { + META_NONE_FLAG = 0, + META_SERIZLE_FLAG = 1 << 0, + META_UI_FLAG = 1 << 0, + }; + class UClass; + class MemberDataValue; + class MethodDataValue; + class Meta { + public: + UClass* cls; + uint32_t flag{ 0 }; + + template + static FieldPtr MemberField(T Obj::* ptr, const Name& name, char*& memory, const MemberDataValue& data = {}); + + template + static FieldPtr MethodField(R(*ptr)(Args...), const Name& name,char*& memory, const MethodDataValue& data = {}); + + template + static FieldPtr MethodField(R(T::*ptr)(Args...), const Name& name,char*& memory, const MethodDataValue& data = {}); + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/meta.inl b/engine/3rdparty/zlib/include/refl/detail/meta.inl new file mode 100644 index 0000000..d951d3c --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/detail/meta.inl @@ -0,0 +1,70 @@ +#pragma once +#include "meta.h" +#include "uclass.h" +namespace refl { + + template + inline FieldPtr Meta::MemberField(T Obj::* ptr, const Name& name, char*& memory, const MemberDataValue& data) + { + MemberData member; + member.offset = reinterpret_cast(&(reinterpret_cast(0)->*ptr)); + if (AnyValue arg = data.value; arg.IsValid()) { + arg.type->InitObject(memory); + arg.type->CopyObject(memory, arg.ptr); + member.value = Any(memory, arg.cls).ConvertTo(&TypeInfo*>::StaticClass); + memory += arg.type->size; + } + if (AnyValue arg = data.meta; arg.IsValid()) { + arg.type->InitObject(memory); + arg.type->CopyObject(memory, arg.ptr); + member.meta = Any(memory, arg.cls); + memory += arg.type->size; + } + constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG; + return { name, &TypeInfo::StaticClass, member,flag}; + } + template + inline FieldPtr Meta::MethodField(R(*ptr)(Args...), const Name& name, char*& memory, const MethodDataValue& data) + { + MethodData method; + uint32_t flag = FIELD_METHOD_FLAG; + auto cls = &TypeInfo(*)(real_type_t...)>::StaticClass; + if (auto& args = data.value; !args.empty()) { + flag |= FIELD_METHOD_VALUE_FLAG; + AnyValueList argsValue(args, memory); + argsValue.ConvertArgs(cls->vtable.GetParams(cls)); + method.value = argsValue.ToSArray(); + memory += AnyValueList::GetArgsSize(args); + } + if (AnyValue arg = data.meta; arg.IsValid()) { + arg.type->InitObject(memory); + arg.type->CopyObject(memory, arg.ptr); + method.meta = Any(memory, arg.cls); + memory += arg.type->size; + } + method.fptr = { *(Method*)&ptr }; + return { name, cls, method,flag }; + } + template + inline FieldPtr Meta::MethodField(R(T::*ptr)(Args...),const Name& name, char*& memory, const MethodDataValue& data) + { + MethodData method; + uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG; + auto cls = &TypeInfo(*)(const void*, real_type_t...)>::StaticClass; + if (auto& args = data.value; !args.empty()) { + flag |= FIELD_METHOD_VALUE_FLAG; + AnyValueList argsValue(args, memory); + argsValue.ConvertArgs(cls->vtable.GetParams(cls)); + method.value = argsValue.ToSArray(); + memory += AnyValueList::GetArgsSize(args); + } + if (AnyValue arg = data.meta; arg.IsValid()) { + arg.type->InitObject(memory); + arg.type->CopyObject(memory, arg.ptr); + method.meta = Any(memory, arg.cls); + memory += arg.type->size; + } + method.fptr = { *(Method*)&ptr }; + return { name, cls, method,flag }; + } +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/type.h b/engine/3rdparty/zlib/include/refl/detail/type.h index 2735e55..1f0826b 100644 --- a/engine/3rdparty/zlib/include/refl/detail/type.h +++ b/engine/3rdparty/zlib/include/refl/detail/type.h @@ -1,10 +1,13 @@ #pragma once #include namespace refl { + template + concept _ReflCheck_Meta = requires { typename T::MyStatic; typename T::MyUClass;}; + template concept _ReflCheck_Ctor = requires { T(); }; template - concept _ReflCheck_UClass = requires { !std::is_void_v; }; + concept _ReflCheck_UClass = _ReflCheck_Meta; template concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor && !_ReflCheck_UClass; @@ -34,6 +37,7 @@ namespace refl { struct real_type { using type = std::remove_cv_t*; }; + template using real_type_t = real_type::type; diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.h b/engine/3rdparty/zlib/include/refl/detail/uclass.h index 73653fd..9200b16 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.h +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.h @@ -1,7 +1,7 @@ #pragma once -#include "type.h" #include "field.h" #include "view.h" +#include "meta.h" namespace refl { enum ClassFlag :uint32_t { CLASS_NONE_FLAG = 0, @@ -15,7 +15,7 @@ namespace refl { //function sarray(*GetParams)(const UClass*); //function - void (*Call)(const FieldPtr*, const sarray& ArgsList); + void (*Call)(const FieldPtr*, const sarray& ArgsList); //object void (*InitObject)(void*); @@ -28,11 +28,12 @@ namespace refl { uint32_t size; uint32_t flag{0}; const UClass* parent; + Meta* meta{nullptr}; 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{ + AnyView New(void* ptr = nullptr) const{ if (ptr == nullptr) { ptr = malloc(size); InitObject(ptr); diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl index 200d3b3..52c0c83 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.inl +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -48,19 +48,19 @@ namespace refl { //这里顺序似乎是不确定的,但是我实际运用是对的 //如果使用make_index_sequence,会多一次函数调用 //为什么包裹一层迭代器,就不会出现警告了 - static void Call(const FieldPtr* field, const sarray& ArgsList) { + static void Call(const FieldPtr* field, const sarray& ArgsList) { assert(sizeof...(Args) <= ArgsList.size()); if constexpr (std::is_same_v) { - MethodType fptr = (MethodType)field->data.method; + MethodType fptr = (MethodType)field->data.method.fptr; auto param = ArgsList.end(); fptr((param--->cast_to())...); } else { - MethodType fptr = (MethodType)field->data.method; + MethodType fptr = (MethodType)field->data.method.fptr; auto param = ArgsList.end(); auto ret = ArgsList.front(); if (ret->cls == &TypeInfo::StaticClass) { - *(R*)ret->val = fptr((param--->cast_to())...); + *(R*)ret->ptr = fptr((param--->cast_to())...); } else { fptr((param--->cast_to())...); @@ -91,8 +91,8 @@ namespace refl { template class UClass_Meta : public UClass { public: - using FieldsType = decltype(T::__MakeFields()); - FieldsType Fields{ T::__MakeFields() }; + using FieldsType = decltype(T::MyMeta::__MakeFields()); + FieldsType Fields{ T::MyMeta::__MakeFields() }; UClass_Meta() : UClass(type_name().View(), sizeof(T)){ if constexpr (std::is_trivially_copyable_v) { flag = CLASS_TRIVIAL_FLAG; @@ -140,7 +140,7 @@ namespace refl { }; template<_ReflCheck_UClass T> struct TypeInfoImpl { - using MyUClass = typename T::MyUClass; + using MyUClass = typename T::MyMeta::MyUClass; inline static MyUClass StaticClass = MyUClass(); }; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/view.h b/engine/3rdparty/zlib/include/refl/detail/view.h index dcfe24b..61f4c75 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.h +++ b/engine/3rdparty/zlib/include/refl/detail/view.h @@ -1,77 +1,42 @@ #pragma once #include -#include "type.h" #include "field.h" namespace refl { - class UClass; - class FieldPtr; - //生命周期短,适用于传参,不建议保存数据 - //只能指向指针,引用=>指针,指针=>指针,T => T*,T的类型丢失 - struct ArgsView { - public: - const void* val; - const UClass* cls; - constexpr ArgsView() : val(nullptr), cls(nullptr) {} - //右值=>右值压入栈,caller入栈地址 - //左值=>caller变量地址 - template - constexpr ArgsView(T&& v): val(&v), cls(&TypeInfo*>::StaticClass){ - if constexpr (std::is_same_v, ArgsView>) { - val = v.val; - cls = v.cls; - } - } - template - constexpr ArgsView(T* v) : val(v), cls(&TypeInfo*>::StaticClass) { - } - template//参数 T* => T* - constexpr inline T cast_to() const { - if constexpr (std::is_pointer_v) { - return (T)val; - } - else if constexpr (std::is_reference_v){ - using RT = std::remove_reference_t; - return *(RT*)val; - }else{ - return *(T*)val; - } - } - template - T cast_ret() const { - if constexpr (std::is_pointer_v) { - return *(T*)val; - } - else if constexpr (std::is_reference_v) { - using RT = std::remove_reference_t; - return **(RT**)val; - } - else { - return *(T*)val; - } - } - bool ConvertTo(const UClass* toClass); - }; //存入的是真实的数据类型,type用于拷贝数据 - struct ArgsValue : public ArgsView { + struct AnyValue : public Any { const UClass* type; + constexpr AnyValue() :Any(), type(nullptr) {} template - constexpr ArgsValue(T&& t) : ArgsView(t), type(&TypeInfo>::StaticClass){} + constexpr AnyValue(T&& t) : Any(t), type(&TypeInfo>::StaticClass){} }; - struct ArgsValueList{ + struct MemberDataValue { + Offset offset{0}; + AnyValue value; + AnyValue meta; + constexpr MemberDataValue() :value(), meta() {} + constexpr MemberDataValue(const AnyValue& value, const AnyValue& meta = {}) : value(value), meta(meta) {} + }; + struct MethodDataValue { + Method fptr{nullptr}; + sarray value; + AnyValue meta; + constexpr MethodDataValue() :value(), meta() {} + constexpr MethodDataValue(const sarray& value, const AnyValue& meta = {}) : value(value), meta(meta) {} + }; + struct AnyValueList{ void* data; - ArgsView* ptr; int num; bool isMemoryOwner{false}; - ArgsValueList(const sarray& args,void* memory = nullptr); - ~ArgsValueList(); - constexpr static int GetArgsSize(const sarray& args); + AnyValueList(const sarray& args,void* memory = nullptr); + ~AnyValueList(); + void ConvertArgs(sarray params); + const sarray ToSArray(); + constexpr static int GetArgsSize(const sarray& args); }; - class ObjectView { + class AnyView : public Any{ public: - const void* ptr; - const UClass* cls; const FieldPtr* cache{ nullptr }; - ObjectView(const void* ptr, const UClass* cls) : ptr(ptr), cls(cls) {} + AnyView(const void* ptr, const UClass* cls) : Any(ptr,cls) {} public: template @@ -80,10 +45,10 @@ namespace refl { template bool Set(const Name& name, const T& t); - bool Invoke(const Name& name,const sarray& ArgsList);//这里是内存分配慢呀,这里是可以改成栈容器的 + bool Invoke(const Name& name,const sarray& ArgsList); - bool Invoke(const Name& name,svector& ArgsList); + bool Invoke(const Name& name,svector& ArgsList); - ObjectView Parent(); + AnyView Parent(); }; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/view.inl b/engine/3rdparty/zlib/include/refl/detail/view.inl index ee75513..0912b16 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.inl +++ b/engine/3rdparty/zlib/include/refl/detail/view.inl @@ -8,45 +8,49 @@ namespace refl { * 对象从大到小 */ using enum ClassFlag; - bool ArgsView::ConvertTo(const UClass* toClass) { + Any* Any::ConvertTo(const UClass* toClass) { + if (cls == toClass) { + return this; + } //子类转父类 if (cls->IsChildOf(toClass)) { cls = toClass; - return true; + return this; } if (cls->flag & CLASS_TRIVIAL_FLAG && toClass->flag & CLASS_TRIVIAL_FLAG) { cls = toClass; - return true; + return this; } - return false; + return nullptr; } - ArgsValueList::ArgsValueList(const sarray& args, void* memory) - : ptr((ArgsView*)memory),data(memory), num(args.size()) + AnyValueList::AnyValueList(const sarray& args, void* memory) + : data(memory), num(args.size()) { if (!memory) { isMemoryOwner = true; data = malloc(GetArgsSize(args)); - ptr = (ArgsView*)data; - assert(data != nullptr); } - char* pData = ((char*)data) + num * sizeof(ArgsView); + Any* any = (Any*)data; + assert(any != nullptr); + char* pData = ((char*)data) + num * sizeof(Any); for (auto& arg : args) { arg.type->InitObject(pData); - arg.type->CopyObject(pData, arg.val); - ptr->val = pData; - ptr->cls = arg.cls; - ptr++; + arg.type->CopyObject(pData, arg.ptr); + any->ptr = pData; + any->cls = arg.cls; + any++; pData += arg.type->size; } - ptr = (ArgsView*)data; } - inline ArgsValueList::~ArgsValueList() + inline AnyValueList::~AnyValueList() { if (num == 0 || !data) { return; } + Any* ptr = (Any*)data; for (int i = 0; i < num; i++) { - ptr->cls->DestObject((void*)ptr->val); + ptr->cls->DestObject((void*)ptr->ptr); + ptr++; } if (isMemoryOwner) { free(data); @@ -54,16 +58,28 @@ namespace refl { num = 0; data = nullptr; } - inline constexpr int ArgsValueList::GetArgsSize(const sarray& args) + inline void AnyValueList::ConvertArgs(sarray params) { - int size = args.size() * sizeof(ArgsView); + Any* ptr = (Any*)data; + int first = params.size() - num; + for (int i = 0; i < num; i++, ptr++) { + ptr->ConvertTo(*params.at(first + i)); + } + } + inline const sarray AnyValueList::ToSArray() + { + return sarray((Any*)data, num); + } + inline constexpr int AnyValueList::GetArgsSize(const sarray& args) + { + int size = args.size() * sizeof(Any); for (auto& arg : args) { size += arg.type->size; } return size; } template - inline bool ObjectView::Get(const Name& name, T& t) + inline bool AnyView::Get(const Name& name, T& t) { if (cache && cache->name == name) { _cache_get: bool isChild = cache->type->IsChildOf(true); @@ -83,7 +99,7 @@ namespace refl { return false; } template - inline bool ObjectView::Set(const Name& name, const T& t) + inline bool AnyView::Set(const Name& name, const T& t) { if (cache && cache->name == name) { _cache_set: bool isChild = cache->type->IsChildOf(true); @@ -102,7 +118,7 @@ namespace refl { } return false; } - bool ObjectView::Invoke(const Name& name,const sarray& ArgsList) + bool AnyView::Invoke(const Name& name,const sarray& ArgsList) { auto field = cls->GetField(name, 0); if (!field) { @@ -113,7 +129,7 @@ namespace refl { } return field->Invoke(ArgsList); } - bool ObjectView::Invoke(const Name& name, svector& ArgsList) + bool AnyView::Invoke(const Name& name, svector& ArgsList) { auto field = cls->GetField(name, 0); if (!field) { @@ -124,7 +140,7 @@ namespace refl { } return field->Invoke(ArgsList); } - ObjectView ObjectView::Parent() { + AnyView AnyView::Parent() { return { ptr, cls ? cls->parent : nullptr }; } } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/macro.h b/engine/3rdparty/zlib/include/refl/macro.h index 9c0be35..3bfa402 100644 --- a/engine/3rdparty/zlib/include/refl/macro.h +++ b/engine/3rdparty/zlib/include/refl/macro.h @@ -1,35 +1,3 @@ -#pragma once -/* -#define REGISTER_CLASS(cls) using _T = cls; -#define MAKE_STATIC_FIELD(member, ...) \ - MakeStaticField(&_T::member, #member, __VA_ARGS__) - -#define MAKE_FIELD(member, ...) \ - Field<_T>::MakeField(&_T::member, #member, __VA_ARGS__) - -#define REGISTER_FIELDS()\ - consteval static auto __MakeStaticFields() {\ - return std::array{\ - MAKE_FIELD_LIST(MAKE_STATIC_FIELD)\ - };\ - }\ - static auto __MakeFields() {\ - return std::array{\ - MAKE_FIELD_LIST(MAKE_FIELD)\ - };\ - } - - -#define MAKE_FIELD_LIST(MACRO) \ - MACRO(x), \ - MACRO(y), \ - MACRO(z), \ - MACRO(norm, {10,9}), \ - MACRO(norm1, {1}), \ - MACRO(norm2, {2}), - -REGISTER_CLASS(vec3) -REGISTER_FIELDS() - -#undef MAKE_FIELD_LIST -*/ \ No newline at end of file +#if !defined(__cppast) +#define __cppast(...) +#endif \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/refl.h b/engine/3rdparty/zlib/include/refl/refl.h index 2c1ef46..78518f1 100644 --- a/engine/3rdparty/zlib/include/refl/refl.h +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -2,37 +2,44 @@ #include "detail/uclass.inl" #include "detail/view.inl" #include "detail/field.inl" +#include "detail/meta.inl" #include "std/sarray.h" #include "macro.h" namespace refl { template - consteval FieldPtr MakeStaticField(T Obj::* ptr, Name name) { - FieldPtr::Default value; - Offset offset = 0; - FieldPtr::Data member = { offset }; + consteval FieldPtr StaticMemberField(T Obj::* ptr, const Name& name, const MemberDataValue& data = {}) { + Offset offset = data.value.IsValid() ? sizeof(T) : 0; + if (data.meta.IsValid()) { + offset += data.meta.type->size; + } + FieldPtr::Data member(offset); constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG; - return { name, &TypeInfo::StaticClass, member, value, flag }; + return { name,&TypeInfo::StaticClass, member,flag}; } template - consteval FieldPtr MakeStaticField(R(*ptr)(Args...), Name name, const sarray& args = {}) { + consteval FieldPtr StaticMethodField(R(*ptr)(Args...), const Name& name, const MethodDataValue& data = {}) { uint32_t flag = FIELD_METHOD_FLAG; - FieldPtr::Default value; - Offset offset = ArgsValueList::GetArgsSize(args); - FieldPtr::Data method = { offset }; - return { name, &TypeInfo(*)(real_type_t...)>::StaticClass, method, value, flag }; + Offset offset = AnyValueList::GetArgsSize(data.value); + if (data.meta.IsValid()) { + offset += data.meta.type->size; + } + FieldPtr::Data method(offset); + return { name,&TypeInfo(*)(real_type_t...)>::StaticClass,method, flag }; } template - consteval FieldPtr MakeStaticField(R(T::* ptr)(Args...), Name name, const sarray& args = {}) { + consteval FieldPtr StaticMethodField(R(T::* ptr)(Args...), const Name& name, const MethodDataValue& data = {}) { uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG; - FieldPtr::Default value; - Offset offset = ArgsValueList::GetArgsSize(args); - FieldPtr::Data method = { offset }; - return { name, &TypeInfo(*)(const void*,real_type_t...)>::StaticClass, method, value,flag }; + Offset offset = AnyValueList::GetArgsSize(data.value); + if (data.meta.IsValid()) { + offset += data.meta.type->size; + } + FieldPtr::Data method(offset); + return { name, &TypeInfo(*)(const void*,real_type_t...)>::StaticClass,method,flag }; } //用处不大---------------begin - template - consteval int GetStaticFieldID(Name name) { - auto fields = T::__MakeStaticFields(); + template<_ReflCheck_Meta T> + consteval int GetStaticFieldID(const Name& name) { + auto fields = T::MyStatic::__MakeStaticFields(); auto first = fields.begin(); for (auto& field : fields) { if (field.name == name) { @@ -41,9 +48,9 @@ namespace refl { } return 0; } - template + template<_ReflCheck_Meta T> consteval int GetStaticFieldID(bool(* fptr)(FieldPtr)) { - auto fields = T::__MakeStaticFields(); + auto fields = T::MyStatic::__MakeStaticFields(); auto first = fields.begin(); for (auto& field : fields) { if (fptr(field)) { @@ -52,15 +59,15 @@ namespace refl { } return 0; } - template + template<_ReflCheck_Meta T, auto name> consteval auto GetStaticField() { - constexpr auto fields = T::__StaticFields(); + constexpr auto fields = T::MyMeta::__StaticFields(); using AutoType = decltype(name); if constexpr (std::is_same_v) { return std::get(fields); } else { - constexpr auto id = GetStaticFieldID(Name(name.Data())); + constexpr auto id = GetStaticFieldID(name.Data()); return std::get(fields); } } diff --git a/engine/3rdparty/zlib/include/refl/std/sarray.h b/engine/3rdparty/zlib/include/refl/std/sarray.h index 61fb710..1932eac 100644 --- a/engine/3rdparty/zlib/include/refl/std/sarray.h +++ b/engine/3rdparty/zlib/include/refl/std/sarray.h @@ -24,6 +24,9 @@ namespace refl { } return nullptr; } + constexpr bool empty() const noexcept { + return m_count == 0; + } constexpr int size() const noexcept { return m_count; } diff --git a/engine/3rdparty/zlib/include/refl/std/svector.h b/engine/3rdparty/zlib/include/refl/std/svector.h index 0c0a5dc..ae262dc 100644 --- a/engine/3rdparty/zlib/include/refl/std/svector.h +++ b/engine/3rdparty/zlib/include/refl/std/svector.h @@ -23,6 +23,9 @@ namespace refl { m_count++; } } + constexpr bool empty() const noexcept { + return m_count == 0; + } constexpr int size() const noexcept { return m_count; } diff --git a/engine/3rdparty/zlib/test/refl/vertex.h b/engine/3rdparty/zlib/test/refl/vertex.h index d2ab143..96f2444 100644 --- a/engine/3rdparty/zlib/test/refl/vertex.h +++ b/engine/3rdparty/zlib/test/refl/vertex.h @@ -9,11 +9,20 @@ struct vec3_parent { //cout << x2 << "vec3_parent::norm" << endl; } }; +struct vec4 { + string name{ "hello" }; +}; +struct vec3_Meta; struct vec3 : public vec3_parent { + using MyMeta = vec3_Meta; + __cppast(Meta = { 0.f}) float x = 1; + __cppast(Meta = { 0.f}) float y = 2; + __cppast(Meta = { 0.f}) float z = 3; string name{ "hellohellohellohellohellohello" }; + __cppast(Meta = { {3,4} }) int norm(int x1, int& x2)override { int tmp = x1 * 2 + 1; x1 = x2; @@ -36,29 +45,40 @@ struct vec3 : public vec3_parent { x1 = x1 * 10; cout << x1 << "::norm3" << endl; } - using MyUClass = UClass_Meta; +}; +struct vec3_Static_Meta { //这里好像不需要 consteval static auto __StaticFields() { return std::make_tuple(&vec3::x, &vec3::y, &vec3::z, &vec3::norm, &vec3::norm1, &vec3::norm2); }; consteval static auto __MakeStaticFields() { return std::array{ - MakeStaticField(&vec3::x, "x"), - MakeStaticField(&vec3::y, "y"), - MakeStaticField(&vec3::z, "z"), - MakeStaticField(&vec3::norm, "norm", { 10,9 }), - MakeStaticField(&vec3::norm1, "norm1", { 10 }), - MakeStaticField(&vec3::norm2, "norm2", { 10 }), + StaticMemberField(&vec3::x, "x", {0.f,vec4{}}), + StaticMemberField(&vec3::y, "y", {0.f,{}}), + StaticMemberField(&vec3::z, "z", {0.f,{}}), + StaticMethodField(&vec3::norm, "norm", {{10,9},{}}), + StaticMethodField(&vec3::norm1, "norm1", {{10},{}}), + StaticMethodField(&vec3::norm2, "norm2", {{10},{}}), }; } + consteval static int Size() { + return fetch_meta_size(); + } +}; +constexpr int size = vec3_Static_Meta::Size(); +struct vec3_Meta : public Meta{ + using MyStatic = vec3_Static_Meta; + using MyUClass = UClass_Meta; + inline static char s_data[MyStatic::Size()]{}; static auto __MakeFields() { - return std::array{ - Field::MakeField(&vec3::x, "x"), - Field::MakeField(&vec3::y, "y"), - Field::MakeField(&vec3::z, "z"), - Field::MakeField(&vec3::norm, "norm", {10,9}), - Field::MakeField(&vec3::norm1, "norm1", {10}), - Field::MakeField(&vec3::norm2, "norm2", {10}), + char* memory = &s_data[0]; + return std::array{ + MemberField(&vec3::x, "x",memory, {0.f,vec4{}}), + MemberField(&vec3::y, "y",memory, {0.f,{}}), + MemberField(&vec3::z, "z",memory, {0.f,{}}), + MethodField(&vec3::norm, "norm", memory, {{10,9},{}}), + MethodField(&vec3::norm1, "norm1",memory, {{10},{}}), + MethodField(&vec3::norm2, "norm2",memory, {{10},{}}), }; }; }; \ 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 3f18d75..bcd8178 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -2,31 +2,33 @@ #include auto cls = &TypeInfo::StaticClass; vec3 v; +constexpr int smeta = sizeof(vec3_Meta); +constexpr int scls = sizeof(decltype(*cls)); auto ov = cls->New((void*)&v); constexpr Name func("norm"); void TestRefl1(benchmark::State& state) { int x = 1, y = 2; for (auto _ : state) { - std::array arr{&x, ov.ptr}; - svector svec(&arr.front(), arr.size(), 2); + std::array arr{&x, ov.ptr}; + svector svec(&arr.front(), arr.size(), 2); ov.Invoke("norm", svec); } } BENCHMARK(TestRefl1); void TestRefl2(benchmark::State& state) { int x = 1, y = 2; - constexpr auto id = GetStaticFieldID(func); + constexpr auto id = GetStaticFieldID(func); auto field = cls->GetField(id); for (auto _ : state) { - std::array arr{&x, ov.ptr}; - svector svec(&arr.front(), arr.size(), 2); + std::array arr{&x, ov.ptr}; + svector svec(&arr.front(), arr.size(), 2); field->Invoke(svec); } } BENCHMARK(TestRefl2); void TestRefl3(benchmark::State& state) { int x = 1, y = 2; - constexpr auto id = GetStaticFieldID(func); + constexpr auto id = GetStaticFieldID(func); auto field = cls->GetField(id); for (auto _ : state) { field->Invoke({ &x,ov.ptr, x, y });