diff --git a/engine/3rdparty/zlib/include/refl/detail/field.h b/engine/3rdparty/zlib/include/refl/detail/field.h index fe61e7f..943996f 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.h +++ b/engine/3rdparty/zlib/include/refl/detail/field.h @@ -23,6 +23,7 @@ namespace refl { FIELD_METHOD_FLAG = 1 << 2, FIELD_METHOD_VALUE_FLAG = 1 << 3 }; + using enum FieldFlag; struct FieldPtr { union Data { @@ -49,12 +50,12 @@ namespace refl { //unsafe bool Invoke(const sarray& ArgsList)const; //safe - bool Invoke(std::vector& ArgsList)const; + bool Invoke(svector& ArgsList)const; template auto Call(Func func, Args&&... args)const; - std::vector GetParams() const; + sarray GetParams() const; }; template consteval auto fetch_method_t(R(T::*)(Args...)) { diff --git a/engine/3rdparty/zlib/include/refl/detail/field.inl b/engine/3rdparty/zlib/include/refl/detail/field.inl index f62a7f1..90d3f2a 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.inl +++ b/engine/3rdparty/zlib/include/refl/detail/field.inl @@ -14,7 +14,7 @@ namespace refl { } return Call; } - bool FieldPtr::Invoke(std::vector& ArgsList)const { + bool FieldPtr::Invoke(svector& ArgsList)const { auto Call = type->vtable.Call; if (Call) { auto params = GetParams(); @@ -33,8 +33,10 @@ namespace refl { return false; } - for (int i = 0; i < paramsSize; i++) { - if (ArgsList[i].cls != params[i] && !ArgsList[i].ConvertTo(params[i])) { + auto a = ArgsList.front(); + auto p = params.front(); + for (auto e = params.back(); p < e; ++p, ++a) { + if (a->cls != *p && !a->ConvertTo(*p)) { return false; } } @@ -42,7 +44,7 @@ namespace refl { } return Call; } - std::vector FieldPtr::GetParams() const { + sarray FieldPtr::GetParams() const { auto GetParams = type->vtable.GetParams; if (GetParams) { return GetParams(type); @@ -52,8 +54,8 @@ namespace refl { template class Field { public: - inline static char data[fetch_args_size()]{}; - inline static int index{0}; + inline static char s_data[fetch_args_size()]{}; + inline static int s_index{0}; template static FieldPtr MakeField(V T::* ptr, Name name) { @@ -69,8 +71,8 @@ namespace refl { FieldPtr::Default value; if (args.size() > 0) { flag |= FIELD_METHOD_VALUE_FLAG; - static const ArgsValueList argsValue(args, &data[index]); - index += ArgsValueList::GetArgsSize(args); + 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 }; }; @@ -84,8 +86,8 @@ namespace refl { FieldPtr::Default value; if (args.size() > 0) { flag |= FIELD_METHOD_VALUE_FLAG; - static const ArgsValueList argsValue(args, &data[index]); - index += ArgsValueList::GetArgsSize(args); + static const ArgsValueList argsValue(args, &s_data[s_index]); + s_index += ArgsValueList::GetArgsSize(args); value.method = []() ->std::pair { return { argsValue.ptr , argsValue.num }; }; diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.h b/engine/3rdparty/zlib/include/refl/detail/uclass.h index 0b291f9..73653fd 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.h +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.h @@ -7,12 +7,13 @@ namespace refl { CLASS_NONE_FLAG = 0, CLASS_TRIVIAL_FLAG = 1 << 0, }; + using enum ClassFlag; struct vtable_uclass { //class - const FieldPtr* (*GetField)(const UClass*, const Name&, bool); + const FieldPtr* (*GetField)(const UClass*, const Name&, int); //function - std::vector(*GetParams)(const UClass*); + sarray(*GetParams)(const UClass*); //function void (*Call)(const FieldPtr*, const sarray& ArgsList); @@ -23,8 +24,6 @@ namespace refl { }; class UClass{ public: - using enum FieldFlag; - using enum ClassFlag; Name name; uint32_t size; uint32_t flag{0}; @@ -77,9 +76,9 @@ namespace refl { return false; } public: - const FieldPtr* GetField(const Name& name, bool isMethod)const { + const FieldPtr* GetField(const Name& name, int index)const { if (vtable.GetField) { - return vtable.GetField(this, name, isMethod); + return vtable.GetField(this, name, index); } return nullptr; } diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl index 7558dc1..200d3b3 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.inl +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -41,9 +41,9 @@ namespace refl { public: std::array UList{}; - static std::vector GetParams(const UClass* cls) { + static sarray GetParams(const UClass* cls) { auto& UList = static_cast(cls)->UList; - return { UList.data(), UList.data() + UList.size() }; + return sarray(&UList.front(),UList.size()); } //这里顺序似乎是不确定的,但是我实际运用是对的 //如果使用make_index_sequence,会多一次函数调用 @@ -58,7 +58,7 @@ namespace refl { else { MethodType fptr = (MethodType)field->data.method; auto param = ArgsList.end(); - auto ret = ArgsList.get(); + auto ret = ArgsList.front(); if (ret->cls == &TypeInfo::StaticClass) { *(R*)ret->val = fptr((param--->cast_to())...); } @@ -106,11 +106,14 @@ namespace refl { else { vtable.InitObject = &UClass::InitObject; } + vtable.GetField = &UClass_Meta::GetField; } const FieldPtr* GetField(int index) const { return &Fields[index]; } - const FieldPtr* GetField(const Name& name, int index = 0) const{ + static const FieldPtr* GetField(const UClass* _cls,const Name& name, int index = 0){ + auto cls = static_cast(_cls); + auto& Fields = cls->Fields; constexpr int length = std::tuple_size::value; auto ptr = index < length ? &Fields[index] : nullptr; for (int i = index; i < length; i++, ptr++) { diff --git a/engine/3rdparty/zlib/include/refl/detail/view.h b/engine/3rdparty/zlib/include/refl/detail/view.h index ac28e99..dcfe24b 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.h +++ b/engine/3rdparty/zlib/include/refl/detail/view.h @@ -1,7 +1,7 @@ #pragma once #include #include "type.h" -#include "../std/sarray.h" +#include "field.h" namespace refl { class UClass; class FieldPtr; @@ -82,7 +82,7 @@ namespace refl { bool Invoke(const Name& name,const sarray& ArgsList);//这里是内存分配慢呀,这里是可以改成栈容器的 - bool Invoke(const Name& name,std::vector& ArgsList); + bool Invoke(const Name& name,svector& ArgsList); ObjectView Parent(); }; diff --git a/engine/3rdparty/zlib/include/refl/detail/view.inl b/engine/3rdparty/zlib/include/refl/detail/view.inl index 3b4b981..ee75513 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.inl +++ b/engine/3rdparty/zlib/include/refl/detail/view.inl @@ -27,6 +27,7 @@ namespace refl { isMemoryOwner = true; data = malloc(GetArgsSize(args)); ptr = (ArgsView*)data; + assert(data != nullptr); } char* pData = ((char*)data) + num * sizeof(ArgsView); for (auto& arg : args) { @@ -35,7 +36,7 @@ namespace refl { ptr->val = pData; ptr->cls = arg.cls; ptr++; - pData += arg.cls->size; + pData += arg.type->size; } ptr = (ArgsView*)data; } @@ -71,7 +72,7 @@ namespace refl { } return isChild; } - auto field = cls->GetField(name, false); + auto field = cls->GetField(name, 0); if (field) { cache = field; goto _cache_get; @@ -91,7 +92,7 @@ namespace refl { } return isChild; } - auto field = cls->GetField(name, false); + auto field = cls->GetField(name, 0); if (field) { cache = field; goto _cache_set; @@ -103,7 +104,7 @@ namespace refl { } bool ObjectView::Invoke(const Name& name,const sarray& ArgsList) { - auto field = cls->GetField(name, true); + auto field = cls->GetField(name, 0); if (!field) { if (cls->parent) { return Parent().Invoke(name, ArgsList); @@ -112,9 +113,9 @@ namespace refl { } return field->Invoke(ArgsList); } - bool ObjectView::Invoke(const Name& name, std::vector& ArgsList) + bool ObjectView::Invoke(const Name& name, svector& ArgsList) { - auto field = cls->GetField(name, true); + auto field = cls->GetField(name, 0); if (!field) { if (cls->parent) { return Parent().Invoke(name, ArgsList); diff --git a/engine/3rdparty/zlib/include/refl/macro.h b/engine/3rdparty/zlib/include/refl/macro.h new file mode 100644 index 0000000..9c0be35 --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/macro.h @@ -0,0 +1,35 @@ +#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 diff --git a/engine/3rdparty/zlib/include/refl/refl.h b/engine/3rdparty/zlib/include/refl/refl.h index fb34958..2c1ef46 100644 --- a/engine/3rdparty/zlib/include/refl/refl.h +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -1,7 +1,9 @@ +#pragma once #include "detail/uclass.inl" #include "detail/view.inl" #include "detail/field.inl" #include "std/sarray.h" +#include "macro.h" namespace refl { template consteval FieldPtr MakeStaticField(T Obj::* ptr, Name name) { diff --git a/engine/3rdparty/zlib/include/refl/std/sarray.h b/engine/3rdparty/zlib/include/refl/std/sarray.h index 553aaaf..61fb710 100644 --- a/engine/3rdparty/zlib/include/refl/std/sarray.h +++ b/engine/3rdparty/zlib/include/refl/std/sarray.h @@ -1,40 +1,37 @@ #pragma once -#include +#include "svector.h" #include namespace refl { template class sarray { protected: const T* m_ptr; - int m_size; + int m_count; public: - constexpr sarray(const std::vector& list) : m_ptr(&list.front()), m_size(list.size()) {} - constexpr sarray() :m_ptr(nullptr), m_size(0) {} - constexpr sarray(std::initializer_list list) : m_ptr(list.begin()), m_size(list.size()) {} - template - constexpr sarray(const std::array& arr):m_ptr(&arr[0]), m_size(arr.size()){} - const T* get() const noexcept { + constexpr sarray(const T* ptr, int count) : m_ptr(ptr), m_count(count) {} + constexpr sarray(const svector& vec) : m_ptr(vec.front()), m_count(vec.size()) {} + constexpr sarray() :m_ptr(nullptr), m_count(0) {} + constexpr sarray(std::initializer_list list) : m_ptr(list.begin()), m_count(list.size()) {} + const T* front() const noexcept { return m_ptr; } - const T* last()const noexcept { - if (m_size > 0) - return m_ptr + m_size - 1; - return m_ptr; + const T* back()const noexcept { + return m_ptr + m_count; } const T* at(int i) const noexcept { - if (i < m_size) { + if (i < m_count) { return m_ptr + i; } return nullptr; } constexpr int size() const noexcept { - return m_size; + return m_count; } constexpr auto begin()const noexcept { return iterator{ m_ptr }; } constexpr auto end() const noexcept { - return iterator{ m_ptr + m_size}; + return iterator{ m_ptr + m_count}; } // Iterator class class iterator { diff --git a/engine/3rdparty/zlib/include/refl/std/svector.h b/engine/3rdparty/zlib/include/refl/std/svector.h new file mode 100644 index 0000000..0c0a5dc --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/std/svector.h @@ -0,0 +1,67 @@ +#pragma once +#include +#include +namespace refl { + template + class svector { + protected: + T* m_ptr; + int m_count; + int m_capicty; + public: + constexpr svector() :m_ptr(nullptr), m_count(0), m_capicty(0){} + constexpr svector(T* ptr, int size, int count) : m_ptr(ptr), m_capicty(size), m_count(count){} + T* front()const noexcept { + return m_ptr; + } + T* back()const noexcept { + return m_ptr + m_count; + } + void push_back(const T& t) { + if (m_count < m_capicty) { + *(m_ptr + m_count) = t; + m_count++; + } + } + constexpr int size() const noexcept { + return m_count; + } + constexpr auto begin()const noexcept { + return iterator{ m_ptr }; + } + constexpr auto end() const noexcept { + return iterator{ m_ptr + m_count }; + } + // Iterator class + class iterator { + private: + const T* ptr; + + public: + constexpr iterator(const T* p) : ptr(p) {} + + // Overload ++ to move to next element + constexpr iterator& operator++() noexcept { + ++ptr; + return *this; + } + //这里其实是--it,而不是it-- + constexpr iterator& operator--(int) noexcept { + --ptr; + return *this; + } + constexpr const T* operator->() const noexcept { + return ptr; + } + // Overload * to dereference iterator + constexpr const T& operator*() const noexcept { + return *ptr; + } + + // Overload != to compare iterators + constexpr bool operator!=(const iterator& other) const noexcept { + return ptr != other.ptr; + } + }; + }; +} \ 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 51ef154..d2ab143 100644 --- a/engine/3rdparty/zlib/test/refl/vertex.h +++ b/engine/3rdparty/zlib/test/refl/vertex.h @@ -3,8 +3,9 @@ using namespace std; using namespace refl; struct vec3_parent { - virtual void norm(int x1, int& x2) { + virtual int norm(int x1, int& x2) { x2 = x1 * x2; + return x2; //cout << x2 << "vec3_parent::norm" << endl; } }; @@ -13,9 +14,12 @@ struct vec3 : public vec3_parent { float y = 2; float z = 3; string name{ "hellohellohellohellohellohello" }; - void norm(int x1, int& x2)override { - x2 = x1 * x2; - cout << x2 << "vec3::norm" << endl; + int norm(int x1, int& x2)override { + int tmp = x1 * 2 + 1; + x1 = x2; + x2 = tmp; + return x2; + //cout << x2 << "vec3::norm" << endl; } virtual float norm1(int& x1) { x1 = x1 * x * y * z; diff --git a/engine/3rdparty/zlib/test/refl_01.cpp b/engine/3rdparty/zlib/test/refl_01.cpp index d857e42..3f18d75 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -1,23 +1,44 @@ #include "refl/vertex.h" -#include "refl/std/sarray.h" -#include -int main() { - auto cls1 = &TypeInfo::StaticClass; - auto cls2 = &TypeInfo::StaticClass; - using type1 = real_type_t; - auto& cls = TypeInfo::StaticClass; - auto field = cls.GetField(GetStaticFieldID("norm")); - auto ov = cls.New(); +#include +auto cls = &TypeInfo::StaticClass; +vec3 v; +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); + ov.Invoke("norm", svec); + } +} +BENCHMARK(TestRefl1); +void TestRefl2(benchmark::State& state) { + int x = 1, y = 2; + 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); + field->Invoke(svec); + } +} +BENCHMARK(TestRefl2); +void TestRefl3(benchmark::State& state) { + int x = 1, y = 2; + constexpr auto id = GetStaticFieldID(func); + auto field = cls->GetField(id); + for (auto _ : state) { + field->Invoke({ &x,ov.ptr, x, y }); + } +} +BENCHMARK(TestRefl3); +void TestCPlusPlus(benchmark::State& state) { + int x = 1, y = 2; + for (auto _ : state) { + x = v.norm(x, y); + } +} +BENCHMARK(TestCPlusPlus); - int x = 10; - field->Call(&vec3::norm, ov.ptr, x, x); - field->Invoke({ov.ptr, x, x}); - std::vector ArgsList{{}, ov.ptr, x, x}; - field->Invoke(ArgsList); - sarray sa(ArgsList); - field->Invoke(sa); - UMethod_Auto::Call(field, { ov.ptr, x, x }); - //fetchUMethod(&vec3::norm)->Call(field, ov.ptr, x, &x); - std::cout << "hello world\n"; - return 0; -} \ No newline at end of file +BENCHMARK_MAIN();