From 7ee697a1e28dde6da0c86459c70af74cde25628c Mon Sep 17 00:00:00 2001 From: ouczbs Date: Sun, 28 Apr 2024 00:23:35 +0800 Subject: [PATCH] rebuild zlib {array & vector} --- .../3rdparty/zlib/include/refl/detail/field.h | 5 +- .../3rdparty/zlib/include/refl/detail/type.h | 10 -- .../zlib/include/refl/detail/uclass.h | 3 +- .../zlib/include/refl/detail/uclass.inl | 5 +- engine/3rdparty/zlib/include/refl/refl.h | 1 - .../3rdparty/zlib/include/refl/std/parray.h | 69 ++++++++++++ .../3rdparty/zlib/include/refl/std/sarray.h | 72 ------------- .../3rdparty/zlib/include/refl/std/svector.h | 71 ------------- engine/3rdparty/zlib/include/zstd/harray.h | 100 ++++++++++++++++++ .../co_context.cpp => include/zstd/hvector.h} | 0 engine/3rdparty/zlib/include/zstd/parray.h | 50 --------- engine/3rdparty/zlib/include/zstd/sarray.h | 74 +++++++++++++ engine/3rdparty/zlib/include/zstd/svector.h | 75 +++++++++++++ engine/3rdparty/zlib/test/refl/vertex.cpp | 6 ++ engine/3rdparty/zlib/test/refl/vertex.h | 11 +- engine/3rdparty/zlib/test/refl_01.cpp | 7 +- engine/3rdparty/zlib/xmake.lua | 3 +- 17 files changed, 345 insertions(+), 217 deletions(-) create mode 100644 engine/3rdparty/zlib/include/refl/std/parray.h delete mode 100644 engine/3rdparty/zlib/include/refl/std/sarray.h delete mode 100644 engine/3rdparty/zlib/include/refl/std/svector.h create mode 100644 engine/3rdparty/zlib/include/zstd/harray.h rename engine/3rdparty/zlib/{src/zcoro/co_context.cpp => include/zstd/hvector.h} (100%) delete mode 100644 engine/3rdparty/zlib/include/zstd/parray.h create mode 100644 engine/3rdparty/zlib/include/zstd/sarray.h create mode 100644 engine/3rdparty/zlib/include/zstd/svector.h create mode 100644 engine/3rdparty/zlib/test/refl/vertex.cpp diff --git a/engine/3rdparty/zlib/include/refl/detail/field.h b/engine/3rdparty/zlib/include/refl/detail/field.h index faa2652..9ced667 100644 --- a/engine/3rdparty/zlib/include/refl/detail/field.h +++ b/engine/3rdparty/zlib/include/refl/detail/field.h @@ -1,6 +1,6 @@ #pragma once #include "UTemplate/Type.hpp" -#include "../std/sarray.h" +#include "zstd/sarray.h" #include "any.h" #include #include @@ -10,6 +10,9 @@ consteval Name FName(const char* buf) { return Name(buf); } namespace refl { + using zstd::sarray; + using zstd::svector; + using Offset = uint32_t; using Method = void*; struct MemberData { diff --git a/engine/3rdparty/zlib/include/refl/detail/type.h b/engine/3rdparty/zlib/include/refl/detail/type.h index 1f0826b..a75473f 100644 --- a/engine/3rdparty/zlib/include/refl/detail/type.h +++ b/engine/3rdparty/zlib/include/refl/detail/type.h @@ -11,16 +11,6 @@ namespace refl { template concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor && !_ReflCheck_UClass; - // 检查类型是否具有特定的静态成员函数 - template - struct has_init_object : std::false_type {}; - - template - struct has_init_object> : std::true_type {}; - - //templateerror::"expected unqualified-id" - //using has_init_object_v = has_init_object::value; - //类型接口 template struct TypeInfoImpl; diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.h b/engine/3rdparty/zlib/include/refl/detail/uclass.h index 1d4cd7b..b4201fa 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.h +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.h @@ -18,7 +18,8 @@ namespace refl { sarray(*GetParams)(const UClass*); //function void (*Call)(const FieldPtr*, const sarray& ArgsList); - + //meta + const UClass* (*GetMeta)(const Name&); //object void (*InitObject)(void*); void (*CopyObject)(void*, const void*); diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl index 47e56c3..aa8f5d3 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.inl +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -129,12 +129,15 @@ namespace refl { if constexpr (!std::is_same_v) { parent = &TypeInfo

::StaticClass; } - if constexpr (has_init_object::value) { + if constexpr (requires(void* ptr) {T::__InitObject(ptr);}) { vtable.InitObject = &T::__InitObject; } else { vtable.InitObject = &UClass::InitObject; } + if constexpr (requires(const Name & name) { T::__GetMeta(name); }) { + vtable.GetMeta = &T::__GetMeta; + } vtable.GetField = &UClass_Meta::GetField; } const FieldPtr* GetField(int index) const { diff --git a/engine/3rdparty/zlib/include/refl/refl.h b/engine/3rdparty/zlib/include/refl/refl.h index b3925e0..b61798a 100644 --- a/engine/3rdparty/zlib/include/refl/refl.h +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -4,7 +4,6 @@ #include "detail/field.inl" #include "detail/convert.inl" #include "detail/meta.inl" -#include "std/sarray.h" #include "macro.h" namespace refl { template diff --git a/engine/3rdparty/zlib/include/refl/std/parray.h b/engine/3rdparty/zlib/include/refl/std/parray.h new file mode 100644 index 0000000..cb8ad0b --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/std/parray.h @@ -0,0 +1,69 @@ +#include "refl/refl.h" +namespace refl { + template + void malloc_list(T*& ptr, int count) { + ptr = (T*)malloc(count * sizeof(T)); + for (size_t i = 0; i < count; i++) + { + std::construct_at(ptr + i); + } + } + template + class parray { + protected: + const UClass* m_cls; + T* m_ptr; + int m_count; + public: + template + requires std::is_base_of_v + parray(std::vector& vec) : m_cls(&TypeInfo::StaticClass){ + m_count = vec.size(); + if (m_count > 0) { + malloc_list(m_ptr, m_count); + std::move(vec.begin(), vec.end(), m_ptr); + } + } + parray(parray& parr) : m_cls(parr.m_cls) + , m_ptr(parr.data()), m_count(parr.size()) { + parr.reset(); + } + ~parray() { + if (!m_count || !m_cls) + return; + auto dest = m_cls->vtable.DestObject; + if (dest) { + for (int i = 0; i < m_count; i++) { + dest((char*)m_ptr + (i * m_cls->size)); + } + } + m_count = 0; + m_ptr = nullptr; + } + T* at(int i)noexcept { + if(i < m_count) + return (T*)((char*)m_ptr + (i * m_cls->size)); + return nullptr; + } + T* data() noexcept{ + return m_ptr; + } + void reset() noexcept { + m_ptr = nullptr; + m_count = 0; + } + int size() const noexcept { + return m_count; + } + int capicty() const noexcept { + return m_count * m_cls->size; + } + const UClass* uclass()const noexcept { + return m_cls; + } + }; + template + parray ToParray(std::vector& vec){ + return parray{vec}; + } +} diff --git a/engine/3rdparty/zlib/include/refl/std/sarray.h b/engine/3rdparty/zlib/include/refl/std/sarray.h deleted file mode 100644 index 1d62302..0000000 --- a/engine/3rdparty/zlib/include/refl/std/sarray.h +++ /dev/null @@ -1,72 +0,0 @@ -#pragma once -#include "svector.h" -#include -template -class sarray { -protected: - const T* m_ptr; - int m_count; -public: - 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()) {} - constexpr const T* front() const noexcept { - return m_ptr; - } - constexpr const T* back()const noexcept { - return m_ptr + m_count; - } - constexpr const T* at(int i) const noexcept { - if (i < m_count) { - return m_ptr + i; - } - return nullptr; - } - constexpr bool IsValid() const noexcept { - return m_count > 0; - } - constexpr bool empty() const noexcept { - return m_count == 0; - } - 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/include/refl/std/svector.h b/engine/3rdparty/zlib/include/refl/std/svector.h deleted file mode 100644 index 68f4cf9..0000000 --- a/engine/3rdparty/zlib/include/refl/std/svector.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once -#include -#include -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){} - constexpr T* front()const noexcept { - return m_ptr; - } - constexpr T* back()const noexcept { - return m_ptr + m_count; - } - constexpr void push_back(const T& t) { - if (m_count < m_capicty) { - *(m_ptr + m_count) = t; - m_count++; - } - } - constexpr bool IsValid() const noexcept { - return m_count > 0; - } - constexpr bool empty() const noexcept { - return m_count == 0; - } - 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/include/zstd/harray.h b/engine/3rdparty/zlib/include/zstd/harray.h new file mode 100644 index 0000000..0a67ade --- /dev/null +++ b/engine/3rdparty/zlib/include/zstd/harray.h @@ -0,0 +1,100 @@ +#pragma once +#include +#include +namespace zstd { + template + void malloc_list(T*& ptr, int count) { + ptr = (T*)malloc(count * sizeof(T)); + for (size_t i = 0; i < count; i++) + { + std::construct_at(ptr + i); + } + } + template + class harray { + protected: + T* m_ptr{nullptr}; + int m_count{0}; + public: + constexpr harray() {} + harray(const std::vector& vec)noexcept { + m_count = vec.size(); + if (m_count > 0) { + malloc_list(m_ptr, m_count); + std::move(vec.begin(), vec.end(), m_ptr); + } + } + ~harray() { + for (int i = 0; i < m_count; i++) { + std::destroy_at(m_ptr + i); + } + m_ptr = nullptr; + m_count = 0; + } + constexpr void reset() noexcept { + m_ptr = nullptr; + m_count = 0; + } + constexpr T* data() noexcept { + return m_ptr; + } + constexpr T* front() noexcept { + return m_ptr; + } + constexpr T* back() noexcept { + return m_ptr + m_count; + } + constexpr T* at(int i) noexcept { + if (i < m_count) { + return m_ptr + i; + } + return nullptr; + } + constexpr bool IsValid() const noexcept { + return m_count > 0; + } + constexpr bool empty() const noexcept { + return m_count == 0; + } + constexpr int size() const noexcept { + return m_count; + } + constexpr auto begin() noexcept { + return iterator{ m_ptr }; + } + constexpr auto end() 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/src/zcoro/co_context.cpp b/engine/3rdparty/zlib/include/zstd/hvector.h similarity index 100% rename from engine/3rdparty/zlib/src/zcoro/co_context.cpp rename to engine/3rdparty/zlib/include/zstd/hvector.h diff --git a/engine/3rdparty/zlib/include/zstd/parray.h b/engine/3rdparty/zlib/include/zstd/parray.h deleted file mode 100644 index e44b36a..0000000 --- a/engine/3rdparty/zlib/include/zstd/parray.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include -namespace zstd { - template - class parray { - protected: - uint32_t m_size; - uint32_t m_head{0}; - uint32_t m_tsize; - char* m_buf; - public: - ~parray() { - if (m_buf) { - free(m_buf); - } - } - parray(uint32_t size = 1, uint32_t tsize = sizeof(T)) : m_buf((char*)malloc(tsize * size)), m_size(size) , m_tsize(tsize){} - parray(parray& other) noexcept{ - m_size = other.m_size; - m_head = other.m_head; - m_tsize = other.m_tsize; - m_buf = other.m_buf; - other.m_buf = nullptr; - } - uint32_t size() { - return m_head; - } - uint32_t data_size() { - return m_tsize * m_head; - } - void* data() { - return m_buf; - } - template - requires std::is_base_of::value - void push_back(S& s) { - if (m_head >= m_size) { - return; - } - *(S*)(m_buf + m_head * m_tsize) = s; - m_head++; - }; - T* at(int i) { - if (i >= m_head) { - return nullptr; - } - return (T*)(m_buf + i * m_tsize); - }; - }; -} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/zstd/sarray.h b/engine/3rdparty/zlib/include/zstd/sarray.h new file mode 100644 index 0000000..4eb96d5 --- /dev/null +++ b/engine/3rdparty/zlib/include/zstd/sarray.h @@ -0,0 +1,74 @@ +#pragma once +#include "svector.h" +#include +namespace zstd { + template + class sarray { + protected: + const T* m_ptr; + int m_count; + public: + 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()) {} + constexpr const T* front() const noexcept { + return m_ptr; + } + constexpr const T* back()const noexcept { + return m_ptr + m_count; + } + constexpr const T* at(int i) const noexcept { + if (i < m_count) { + return m_ptr + i; + } + return nullptr; + } + constexpr bool IsValid() const noexcept { + return m_count > 0; + } + constexpr bool empty() const noexcept { + return m_count == 0; + } + 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/include/zstd/svector.h b/engine/3rdparty/zlib/include/zstd/svector.h new file mode 100644 index 0000000..52d6fd4 --- /dev/null +++ b/engine/3rdparty/zlib/include/zstd/svector.h @@ -0,0 +1,75 @@ +#pragma once +#include +#include +namespace zstd { + //栈容器,大小可变,只能从std::array安全构造 + 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) {} + template + constexpr svector(std::array& arr, int count) : m_ptr(arr.data()), m_capicty(N), m_count(count) {} + constexpr T* front()const noexcept { + return m_ptr; + } + constexpr T* back()const noexcept { + return m_ptr + m_count; + } + constexpr void push_back(const T& t) { + if (m_count < m_capicty) { + *(m_ptr + m_count) = t; + m_count++; + } + } + constexpr bool IsValid() const noexcept { + return m_count > 0; + } + constexpr bool empty() const noexcept { + return m_count == 0; + } + 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; + } + }; + }; +} diff --git a/engine/3rdparty/zlib/test/refl/vertex.cpp b/engine/3rdparty/zlib/test/refl/vertex.cpp new file mode 100644 index 0000000..d7f1971 --- /dev/null +++ b/engine/3rdparty/zlib/test/refl/vertex.cpp @@ -0,0 +1,6 @@ +#include "vertex.h" +#include "uimeta_vertex_gen.inl" +const UClass* vec3::__GetMeta(const Name& name) +{ + return fetch_meta_uclass(name); +} diff --git a/engine/3rdparty/zlib/test/refl/vertex.h b/engine/3rdparty/zlib/test/refl/vertex.h index 81fe330..711ef2b 100644 --- a/engine/3rdparty/zlib/test/refl/vertex.h +++ b/engine/3rdparty/zlib/test/refl/vertex.h @@ -12,10 +12,14 @@ struct vec3_parent { struct vec4 { string name{ "hello" }; }; -class vec3_Meta; +template +const UClass* fetch_meta_uclass(const Name& name) { + return &TypeInfo::StaticClass; +}; struct vec3 : public vec3_parent { - using MyMeta = vec3_Meta; - __cppast(Meta = { 1.f}) + using MyMeta = class vec3_Meta; + using MyUIMeta = class vec3_UIMeta; + __cppast(UIMeta = { 1.f}) float x = 1; __cppast(Meta = { 2.f}) float y = 2; @@ -49,5 +53,6 @@ struct vec3 : public vec3_parent { x1 = x1 * 10; cout << x1 << "::norm3" << endl; } + static const UClass* __GetMeta(const Name& name); }; #include "meta_vertex_gen.inl" \ 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 e458218..6cd3ec4 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -2,12 +2,9 @@ #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); void TestRefl1(benchmark::State& state) { int x = 1, y = 2; - auto rx = &vec3_Meta::s_data[0]; auto f = cls->GetField(GetStaticFieldID(FName("name"))); string hello; ov.Get(FName("name"), hello); @@ -16,7 +13,7 @@ void TestRefl1(benchmark::State& state) { constexpr auto r = StaticMemberField(&vec3::name, FName("name"), { ("hello meta")}); for (auto _ : state) { std::array arr{&x, ov.ptr}; - svector svec(&arr.front(), arr.size(), 2); + svector svec(arr, 2); ov.Invoke(FName("norm"), svec); } } @@ -27,7 +24,7 @@ void TestRefl2(benchmark::State& state) { auto field = cls->GetField(id); for (auto _ : state) { std::array arr{&x, ov.ptr}; - svector svec(&arr.front(), arr.size(), 2); + svector svec(arr, 2); field->Invoke(svec); } } diff --git a/engine/3rdparty/zlib/xmake.lua b/engine/3rdparty/zlib/xmake.lua index 02107fd..0ea8275 100644 --- a/engine/3rdparty/zlib/xmake.lua +++ b/engine/3rdparty/zlib/xmake.lua @@ -4,7 +4,6 @@ target("zlib") set_kind("static") add_packages("UTemplate", {public = true}) add_includedirs("include", {public = true}) - add_files("src/**/*.cpp") add_headerfiles("include/**/*.h", "include/**/*.inl") target("zlib_test") set_kind("binary") @@ -38,5 +37,5 @@ target("refl_zlib") add_deps("zlib") add_packages("benchmark") add_includedirs("test/refl") - add_files("test/refl_01.cpp") + add_files("test/refl_01.cpp","test/refl/*.cpp") add_headerfiles("test/refl/*.h") \ No newline at end of file