From a8113a70b190cfae00f54e352f039f2500047947 Mon Sep 17 00:00:00 2001 From: ouczbs Date: Thu, 4 Apr 2024 18:18:04 +0800 Subject: [PATCH] update refl member field --- engine/3rdparty/zlib/include/refl/object.h | 22 ++++ engine/3rdparty/zlib/include/refl/type.h | 12 +- engine/3rdparty/zlib/include/refl/uclass.h | 121 +++++++++++++++++-- engine/3rdparty/zlib/test/refl/tstring.h | 23 ++++ engine/3rdparty/zlib/test/refl_01.cpp | 56 +++++++-- engine/src/engine/vulkanapi/vulkan_context.h | 2 +- engine/src/engine/vulkanapi/vulkanapi.h | 20 +-- 7 files changed, 219 insertions(+), 37 deletions(-) create mode 100644 engine/3rdparty/zlib/include/refl/object.h create mode 100644 engine/3rdparty/zlib/test/refl/tstring.h diff --git a/engine/3rdparty/zlib/include/refl/object.h b/engine/3rdparty/zlib/include/refl/object.h new file mode 100644 index 0000000..d677ded --- /dev/null +++ b/engine/3rdparty/zlib/include/refl/object.h @@ -0,0 +1,22 @@ +#include +namespace refl { + class UClass; + class FieldPtr; + class ObjectView { + public: + const char* ptr; + const UClass* cls; + const FieldPtr* cache{nullptr}; + ObjectView(void* ptr, const UClass* cls) : ptr((const char*)ptr), cls(cls){} + public: + + template + bool Get(const Name& name, T& t); + + template + bool Set(const Name& name, const T& t); + + template + bool Invoke(const Name& name, Args... args); + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/type.h b/engine/3rdparty/zlib/include/refl/type.h index f184037..d980fc6 100644 --- a/engine/3rdparty/zlib/include/refl/type.h +++ b/engine/3rdparty/zlib/include/refl/type.h @@ -1,13 +1,19 @@ #pragma once #include namespace refl { - template + template + concept _CheckDefaultConstruct = requires { T(); }; + template + concept _CheckBuildUClass = requires{{T::UClass::BuildClass()} -> std::same_as; }; + + template + concept _ReflCheck = _CheckDefaultConstruct || _CheckBuildUClass; + template<_ReflCheck T, typename = void> struct TypeInfo; - template + template<_ReflCheck T> struct TypeInfo> { using UClass = typename T::UClass; - //这里的特性怎么走不到呢 inline static UClass StaticClass = UClass::BuildClass(); }; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/uclass.h b/engine/3rdparty/zlib/include/refl/uclass.h index 0d2009c..d96d784 100644 --- a/engine/3rdparty/zlib/include/refl/uclass.h +++ b/engine/3rdparty/zlib/include/refl/uclass.h @@ -1,20 +1,55 @@ #pragma once #include "type.h" #include "field.h" +#include "object.h" namespace refl { class UClass{ public: Name name; uint32_t size; + UClass* parent; public: - void Var() { - + constexpr UClass(Name name, uint32_t size, 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 }; } - - public: template - constexpr static UClass BuildClass() { - return {type_name().View(), sizeof(T)}; + T* New(T* ptr = nullptr) const{ + if (!IsChildOf()) { + return nullptr; + } + if (ptr == nullptr) { + ptr = (T*)malloc(size); + } + InitObject(ptr); + return ptr; + } + 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; + } + //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)const { + return nullptr; } protected: template @@ -22,11 +57,77 @@ namespace refl { FieldPtr::Data member = { offset }; return { name, &TypeInfo::StaticClass, {member} }; } + template + FieldPtr MakeMethodField(T U::* ptr, Name name) { + FieldPtr::Data member = { offset }; + return { name, &TypeInfo::StaticClass, {member} }; + } }; - template + template + requires std::is_default_constructible_v + class UClass_Auto : public UClass{ + public: + using UClass::UClass; + protected: + void InitObject(void* ptr)const override { + constexpr bool is_shallow_copyable = std::is_trivially_copyable::value; + if constexpr (!is_shallow_copyable){ + constexpr T obj{}; + std::construct_at((T*)ptr, obj); + } + else { + memset(ptr, 0, size); + } + } + public: + static UClass_Auto BuildClass() { + auto cls = UClass_Auto(type_name().View(), sizeof(T)); + return cls; + } + }; + template<_ReflCheck T, typename> struct TypeInfo { - using UClass = UClass; - //这里的语法我也看不懂呀,防警告的,显示声明为模板调用 - inline constexpr static UClass StaticClass = UClass::template BuildClass(); + using UClass = UClass_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); + if (field) { + cache = field; + goto _cache_get; + } + 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); + if (field) { + cache = field; + goto _cache_set; + } + return false; + } + template + bool ObjectView::Invoke(const Name& name, Args... args) + { + return false; + } } \ No newline at end of file diff --git a/engine/3rdparty/zlib/test/refl/tstring.h b/engine/3rdparty/zlib/test/refl/tstring.h new file mode 100644 index 0000000..9b17b16 --- /dev/null +++ b/engine/3rdparty/zlib/test/refl/tstring.h @@ -0,0 +1,23 @@ +#include +#include +using std::string; + +template +void PrintHex(T& obj) { + auto size = sizeof(T); + std::cout << type_name().View() << "<" << size <<"> " << " = 0x"; + char* ptr = (char*)& obj; + for (int i = 0; i < size; i++) { + std::cout << std::hex << std::setw(2) << std::setfill('0') <<(int)*(ptr + i); + } + std::cout << "FF" << std::endl; +} +void TestString1() { + string hello = "abcdef"; + int size = sizeof(string); + PrintHex(hello); + hello = "abcdefabcdefabcdefabcdef"; + PrintHex(hello); + //hello.~basic_string(); + PrintHex(size); +} \ 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 829c443..f91e1df 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -2,39 +2,69 @@ #include #include "refl/uclass.h" #include "vertex.h" +#include "tstring.h" using namespace std; using namespace refl; -struct vec3 { +struct vec3_parent { + +}; +struct vec3 : public vec3_parent { using UClass = class vec3_UClass; - float x; - float y; - float z; + float x = 1; + float y = 2; + float z = 3; + string name{ "hellohellohellohellohellohello" }; float norm() { return x * y * z; } }; struct vec3_UClass : public UClass { - array FList; - constexpr static vec3_UClass BuildClass() { - vec3_UClass cls{}; - cls.name = type_name().View(); - cls.size = sizeof(vec3); + 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(&vec3::x, "x"); cls.FList[1] = cls.MakeMemberField(&vec3::y, "y"); cls.FList[2] = cls.MakeMemberField(&vec3::z, "z"); + cls.FList[3] = cls.MakeMemberField(&vec3::name, "name"); return cls; } }; int main() { - using uclass = TypeInfo::UClass; auto& res = TypeInfo::StaticClass; - auto& res2 = TypeInfo::StaticClass; + auto& res2 = TypeInfo::StaticClass; auto& res3 = TypeInfo::StaticClass; + vec3 v1; + auto res4 = TypeInfo::StaticClass; + auto res5 = TypeInfo::StaticClass; auto t1 = res3.FList[0]; - //using t2 = t1::UClass; - std::cout << sizeof(Name) << "Type2: " << typeid(vec3).name() << std::endl; + vec3_parent* v2 = res3.New(); + vec3* v22 = (vec3*)v2; + auto v3 = res3.New(); + int x1 = 110; + float x = *res.New(&x1); + v3.Get("x", x); + v3.Set("z", (float)33); + v2 = (vec3*)v3.ptr; + using T = int; + auto b1 = res3.IsChildOf(); + constexpr auto cls = UClass_Auto(type_name().View(), sizeof(T)); + cout << sizeof(Name) << "Type2: " << typeid(vec3).name() << endl; + delete v2; cout << "hello world\n"; } \ No newline at end of file diff --git a/engine/src/engine/vulkanapi/vulkan_context.h b/engine/src/engine/vulkanapi/vulkan_context.h index 6d9d02f..e44e96e 100644 --- a/engine/src/engine/vulkanapi/vulkan_context.h +++ b/engine/src/engine/vulkanapi/vulkan_context.h @@ -16,7 +16,7 @@ namespace vulkanapi { VulkanContext(RenderVulkanAPI* _API){ API = _API; }; - virtual void UseContext()override; + void UseContext()override; public: static VkDescriptorSetLayout CreateDescriptorSetLayout(const ShaderInfo& info); diff --git a/engine/src/engine/vulkanapi/vulkanapi.h b/engine/src/engine/vulkanapi/vulkanapi.h index 4558d57..1d4e921 100644 --- a/engine/src/engine/vulkanapi/vulkanapi.h +++ b/engine/src/engine/vulkanapi/vulkanapi.h @@ -20,24 +20,24 @@ namespace vulkanapi vector PiPelineList; public: RenderVulkanAPI(); - virtual ~RenderVulkanAPI()override; + ~RenderVulkanAPI()override; public: - virtual RenderContext* GetContext() override { + RenderContext* GetContext() override { return &context; }; public: - virtual void SwitchContext()override; + void SwitchContext()override; public: - virtual void InitRenderPass()override; + void InitRenderPass()override; public: - virtual void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override; + void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override; - virtual void BeginFrame()override; - virtual void EndFrame()override; - virtual void SetStaticMesh(Mesh* mesh)override; - virtual void DrawStaticMesh(Mesh* mesh)override; + void BeginFrame()override; + void EndFrame()override; + void SetStaticMesh(Mesh* mesh)override; + void DrawStaticMesh(Mesh* mesh)override; - virtual void LoadShader(Shader* shader)override; + void LoadShader(Shader* shader)override; public: VulkanVAO* GetNextVAO(uint32_t& index); VulkanPipeline* GetNextPipeline(uint32_t& index);