diff --git a/engine/3rdparty/zlib/include/refl/detail/field.h b/engine/3rdparty/zlib/include/refl/detail/field.h index d505fe4..8790795 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.h +++ b/engine/3rdparty/zlib/include/refl/detail/field.h @@ -9,7 +9,7 @@ namespace refl { class ArgsView; using Offset = uint32_t; - using Method = void(*)(...); + using Method = void*; using DefaultMember = const ArgsView (*)(); using DefaultMethod = std::pair(*)(); @@ -26,22 +26,23 @@ namespace refl { { Offset offset; Method method; - Data() : method(nullptr) {} - Data(Offset& offset) : offset(offset) {} - Data(Method& method) : 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; - Default(): method(nullptr) {} - Default(DefaultMember& member) : member(member) {} - Default(DefaultMethod& method) : method(method) {} + constexpr Default(): method(nullptr) {} + constexpr Default(DefaultMember& member) : member(member) {} + constexpr Default(DefaultMethod& method) : method(method) {} }; Name name; - const UClass* type; - Data data; - Default value; - uint32_t flag; + const UClass* type{}; + Data data{}; + Default value{}; + uint32_t flag{}; //safe bool Invoke(std::vector& ArgsList)const; @@ -50,5 +51,48 @@ namespace refl { std::vector GetParams() const; }; + /* + 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/type.h b/engine/3rdparty/zlib/include/refl/detail/type.h index bd25c70..311e63c 100644 --- a/engine/3rdparty/zlib/include/refl/detail/type.h +++ b/engine/3rdparty/zlib/include/refl/detail/type.h @@ -1,10 +1,17 @@ #pragma once #include +#define REGISTER_CLASS_META(T, P) using MyUClass = UClass_Meta; +#define REGISTER_MEMBER(Ptr, Name) MakeStaticField(Ptr, Name), +#define BUILD_FELDS() constexpr static auto __BuildFields() {\ + return std::array{\ + REGISTER_FIELDS(REGISTER_MEMBER, REGISTER_MEMBER)\ + };\ +}; namespace refl { template concept _ReflCheck_Ctor = requires { T(); }; template - concept _ReflCheck_UClass = requires(typename T::MyUClass& cls) { T::BuildClass(cls); } ; + concept _ReflCheck_UClass = requires { !std::is_void_v; }; template concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor && !_ReflCheck_UClass; //类型接口 diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl index 87af7df..0b57ce8 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.inl +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -8,7 +8,7 @@ namespace refl { using UClass::UClass; using MyUClass = UClass_Auto; public: - constexpr static MyUClass BuildClass() { + consteval static MyUClass BuildClass() { MyUClass cls(type_name().View(), sizeof(T)); if constexpr (!std::is_trivially_copyable_v) { cls.vtable.InitObject = &MyUClass::InitObject; @@ -16,6 +16,12 @@ namespace refl { } return cls; } + void Set(void* ptr,const T& t) { + *(T*)ptr = t; + } + T& Get(void* ptr) { + return *(T*)ptr; + } }; /* @@ -36,6 +42,15 @@ namespace refl { auto& UList = static_cast(cls)->UList; return { UList.data(), UList.data() + UList.size() }; } + static R Call(const FieldPtr* field, Args... args) { + MethodType fptr = (MethodType)field->data.method; + if constexpr (std::is_same_v) { + fptr(args...); + } + else { + return fptr(args...); + } + } static void Call(const FieldPtr* field, const std::vector& ArgsList) { if constexpr (std::is_same_v) { MethodType fptr = (MethodType)field->data.method; @@ -55,7 +70,7 @@ namespace refl { } } protected: - constexpr void BuildUList() { + consteval void BuildUList() { if constexpr (!std::is_same_v) { UList[0] = &TypeInfo::StaticClass; } @@ -66,7 +81,7 @@ namespace refl { } public: //为了简化判断,cls 对象 统统指向 T* - constexpr static MyUClass BuildClass() { + consteval static MyUClass BuildClass() { MyUClass cls(type_name().View(), sizeof(MethodType)); cls.vtable.GetParams = &MyUClass::GetParams; cls.vtable.Call = &MyUClass::Call; @@ -74,6 +89,7 @@ namespace refl { return cls; } }; + /* class UClass_Custom : public UClass { public: using UClass::UClass; @@ -96,13 +112,27 @@ namespace refl { } return nullptr; } - /* + void BuildClass() { FList.shrink_to_fit();//缩减容量,FList大小不再改变了 vtable.GetField = &UClass_Custom::GetField; } - */ + };*/ + + + + template + class UClass_Meta : public UClass { + public: + using FieldsType = decltype(T::__BuildFields()); + FieldsType Fields{ T::__BuildFields() }; + consteval UClass_Meta() : UClass(type_name().View(), sizeof(T)){ + if constexpr (!std::is_same_v) { + parent = &TypeInfo

::StaticClass; + } + } }; + //基础类型的偏特化 template<_ReflCheck_Ctor_NoUClass T> struct TypeInfoImpl { @@ -116,28 +146,10 @@ namespace refl { using UClass = UMethod_Auto; inline constexpr static UClass StaticClass = UClass::BuildClass(); }; - template<_ReflCheck_UClass T> struct TypeInfoImpl { using MyUClass = typename T::MyUClass; - inline static MyUClass StaticClass = []()->MyUClass { - MyUClass cls(type_name().View(), sizeof(T)); - T::BuildClass(cls); - auto& vtable = cls.vtable; - if (!vtable.GetField) { - vtable.GetField = &MyUClass::GetField; - } - if constexpr (!std::is_trivially_copyable_v) { - if (!vtable.InitObject) { - //这里一定会创建一个InitObject 模板函数,如何优化 - vtable.InitObject = &UClass::InitObject; - } - if (!vtable.CopyObject) { - //这里一定会创建一个CopyObject 模板函数,如何优化 - vtable.CopyObject = &UClass::CopyObject; - } - } - return cls; - }(); + //这里如何改成 constexpr + inline constexpr static MyUClass StaticClass = MyUClass(); }; } \ 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 231eb71..79f0317 100644 --- a/engine/3rdparty/zlib/include/refl/refl.h +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -4,39 +4,55 @@ namespace refl { /* 成员变量需要定义默认值吗?那为什么不在initObjec里初始化?*/ template - static FieldPtr MakeMemberField(T Obj::* ptr, Name name) { + consteval FieldPtr MakeStaticField(T Obj::* ptr, Name name) { FieldPtr::Default value; - Offset offset = reinterpret_cast(&(reinterpret_cast(0)->*ptr)); - FieldPtr::Data member = { offset }; + FieldPtr::Data member = { nullptr }; constexpr uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG; return { name, &TypeInfo::StaticClass, member, value, flag }; } template - static FieldPtr MakeMethodField(R(*ptr)(Args...), Name name, const std::vector& args = {}) { + consteval FieldPtr MakeStaticField(R(*ptr)(Args...), Name name/*, const std::vector& args = {}*/) { uint32_t flag = FIELD_METHOD_FLAG; FieldPtr::Default value; - if (args.size() > 0) { + /*if (args.size() > 0) { flag |= FIELD_METHOD_VALUE_FLAG; static const ArgsValueList argsValue(args); value.method = []() ->std::pair { return { argsValue.ptr , argsValue.num }; }; - } - FieldPtr::Data method = { *(Method*)&ptr }; + }*/ + FieldPtr::Data method = { nullptr }; return { name, &TypeInfo(*)(real_type_t...)>::StaticClass, method, value, flag }; } template - static FieldPtr MakeMethodField(R(T::* ptr)(Args...), Name name, const std::vector& args = {}) { + consteval FieldPtr MakeStaticField(R(T::* ptr)(Args...), Name name/*, const std::vector& args = {}*/) { uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG; FieldPtr::Default value; - if (args.size() > 0) { + /*if (args.size() > 0) { flag |= FIELD_METHOD_VALUE_FLAG; static const ArgsValueList argsValue(args); value.method = []() ->std::pair { return { argsValue.ptr , argsValue.num }; }; - } - FieldPtr::Data method = { *(Method*)&ptr }; + }*/ + //Method fptr = nullptr; + FieldPtr::Data method = { nullptr }; return { name, &TypeInfo(*)(const void*,real_type_t...)>::StaticClass, method, value,flag }; } + template + consteval auto MakeTuple(Args&&... args) { + return std::make_tuple(args...); + } + template + auto MakeStaticFields(const std::tuple& args) { + constexpr std::size_t N = sizeof...(Args); + std::array fields{}; + for (std::size_t i = 0; i < N; ++i) { + auto& arg = std::get<0>(args); + auto a1 = std::get<0>(arg); + auto a2 = std::get<1>(arg); + //fields[i] = MakeStaticField(std::get<0>(arg), std::get<1>(arg)); + } + return fields; + } } \ No newline at end of file diff --git a/engine/3rdparty/zlib/test/refl/vertex.h b/engine/3rdparty/zlib/test/refl/vertex.h index a7ec335..df2b8a4 100644 --- a/engine/3rdparty/zlib/test/refl/vertex.h +++ b/engine/3rdparty/zlib/test/refl/vertex.h @@ -32,16 +32,27 @@ struct vec3 : public vec3_parent { x1 = x1 * 10; //cout << x1 << "::norm3" << endl; } - using MyUClass = UClass_Custom; - static void BuildClass(MyUClass& cls) { - cls.parent = &TypeInfo::StaticClass; - cls.AttrNum = 3; - cls.FList = { - MakeMemberField(&vec3::x, "x"), - MakeMemberField(&vec3::y, "y"), - MakeMemberField(&vec3::z, "z"), - MakeMethodField(&vec3::norm, "norm", {8, 9}), - MakeMethodField(&vec3::norm1, "norm1",{(float)2.0}) + constexpr static auto __GetFields() { + return std::tuple{ + MakeTuple(&vec3::x, "x"), + MakeTuple(&vec3::y, "y"), + MakeTuple(&vec3::z, "z"), + MakeTuple(&vec3::norm, "norm"), + MakeTuple(&vec3::norm1, "norm1"), + MakeTuple(&vec3::norm2, "norm2") }; - } + }; + +#define REGISTER_FIELDS(MemberFunc, MethodFunc)\ + MemberFunc(&vec3::x, "x")\ + MemberFunc(&vec3::y, "y")\ + MemberFunc(&vec3::z, "z")\ + MethodFunc(&vec3::norm,"norm",{})\ + MethodFunc(&vec3::norm1,"norm1",{})\ + MethodFunc(&vec3::norm2,"norm2",{}) + REGISTER_CLASS_META(vec3, vec3_parent) + BUILD_FELDS() + + +#undef REGISTER_FIELDS }; \ 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 5c3d28a..5132f62 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -1,40 +1,28 @@ #include "refl/vertex.h" -#include -void TestRefl1(benchmark::State& state) { - auto cls = &TypeInfo::StaticClass; - vec3 v; - auto ptr = (void*)&v; - auto ov = cls->New(ptr); - int x = 1, y = 2; - for (auto _ : state) { - ov.Invoke("norm", { {},ptr, x, y}); - } -} -BENCHMARK(TestRefl1); -void TestRefl2(benchmark::State& state) { - auto cls = &TypeInfo::StaticClass; - vec3 v; - auto ptr = (void*)&v; - auto ov = cls->New(ptr); - int x = 1, y = 2; - std::vector ArgsList; - ArgsList.emplace_back(); - ArgsList.emplace_back(ptr); - ArgsList.emplace_back(x); - ArgsList.emplace_back(y); - auto field = cls->GetField(cls, "norm", true); - for (auto _ : state) { - field->Invoke(ArgsList); - } -} -BENCHMARK(TestRefl2); -void TestCPlusPlus(benchmark::State& state) { - vec3 v; - int x = 1, y = 2; - for (auto _ : state) { - v.norm(x, y); - } -} -BENCHMARK(TestCPlusPlus); +union MemberMethod { + using F1 = int(*)(int); + using F2 = void(*)(); + F1 f1; + F2 f2; + constexpr MemberMethod(F1 f):f1(f) { -BENCHMARK_MAIN(); + } + constexpr MemberMethod(F2 f) :f2(f) { + + } +}; +void test() { + +} +template +void print(T* t) { + +} +int main() { + //auto& cls = TypeInfo::StaticClass; + //int FList[3]{}; + //auto norm = MakeMethodField(&vec3::norm,"norm"); + MakeStaticFields(vec3::__GetFields()); + std::cout << "hello world\n"; + return 0; +} \ No newline at end of file