diff --git a/engine/3rdparty/zlib/include/refl/detail/type.h b/engine/3rdparty/zlib/include/refl/detail/type.h index a8b140c..bd25c70 100644 --- a/engine/3rdparty/zlib/include/refl/detail/type.h +++ b/engine/3rdparty/zlib/include/refl/detail/type.h @@ -12,13 +12,32 @@ namespace refl { struct TypeInfoImpl; template - struct RealTypeImpl { + struct real_type { using type = std::remove_cv_t>; }; template - struct RealTypeImpl { + struct real_type { using type = std::remove_cv_t*; }; template - using TypeInfo = TypeInfoImpl::type>; + using real_type_t = real_type::type; + + //转化为指针类型 + template + struct args_type { + using type = T; + }; + template + struct args_type { + using type = T; + }; + template + struct args_type { + using type = T; + }; + template + using args_type_t = args_type>::type; + + template + using TypeInfo = TypeInfoImpl>; } \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/refl/detail/uclass.inl b/engine/3rdparty/zlib/include/refl/detail/uclass.inl index d21a017..f4d353a 100644 --- a/engine/3rdparty/zlib/include/refl/detail/uclass.inl +++ b/engine/3rdparty/zlib/include/refl/detail/uclass.inl @@ -21,6 +21,7 @@ namespace refl { * 模板优化 * 成员参数转化为const void* * 引用参数转化为指针 + * 返回引用转化为指针 */ template class UMethod_Auto : public UClass { @@ -44,7 +45,7 @@ namespace refl { MethodType fptr = (MethodType)field.data.method; auto param = ArgsList.rbegin(); auto ret = ArgsList.begin(); - if (ret->cls == &TypeInfo::StaticClass) { + if (ret->cls == &TypeInfo::StaticClass) { *(R*)ret->val = fptr(param++->cast_to()...); } else { @@ -55,11 +56,11 @@ namespace refl { protected: constexpr void BuildUList() { if constexpr (!std::is_same_v) { - UList[0] = &TypeInfo::StaticClass; + UList[0] = &TypeInfo::StaticClass; } if constexpr (sizeof...(Args) > 0) { auto ptr = &UList[1]; - (..., (*ptr = &TypeInfo*>::StaticClass, ptr++)); + (..., (*ptr = &TypeInfo*>::StaticClass, ptr++)); } } public: diff --git a/engine/3rdparty/zlib/include/refl/detail/view.h b/engine/3rdparty/zlib/include/refl/detail/view.h index 54e86e9..fff94f0 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.h +++ b/engine/3rdparty/zlib/include/refl/detail/view.h @@ -21,13 +21,13 @@ namespace refl { template bool Invoke(const Name& name, Args&&... args); - template<_ReflCheck_Ctor R, typename ...Args> + template R Invoke(const Name& name, Args&&... args); ObjectView Parent(); }; //生命周期短,适用于传参,不建议保存数据 - //这里类型丢失了呀,存入的都是指针数据 + //只能指向指针,引用=>指针,指针=>指针,T => T*,T的类型丢失 struct ArgsView { public: const void* val; @@ -36,13 +36,13 @@ namespace refl { //右值=>右值压入栈,caller入栈地址 //左值=>caller变量地址 template - ArgsView(T&& v): val(&v), cls(&TypeInfo*>::StaticClass){ - if constexpr (std::is_same_v, ArgsView>) { + ArgsView(T&& v): val(&v), cls(&TypeInfo*>::StaticClass){ + if constexpr (std::is_same_v, ArgsView>) { val = v.val; cls = v.cls; } } - template + template//参数 T* => T* constexpr inline T cast_to() { if constexpr (std::is_pointer_v) { return (T)val; @@ -54,13 +54,26 @@ namespace refl { return *(T*)val; } } + template + T cast_ret() { + 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 { const UClass* type; template - ArgsValue(T&& t) : ArgsView(t), type(&TypeInfo::StaticClass){} + ArgsValue(T&& t) : ArgsView(t), type(&TypeInfo>::StaticClass){} }; struct ArgsValueList{ void* data{ nullptr }; diff --git a/engine/3rdparty/zlib/include/refl/detail/view.inl b/engine/3rdparty/zlib/include/refl/detail/view.inl index 3c05687..5e31273 100644 --- a/engine/3rdparty/zlib/include/refl/detail/view.inl +++ b/engine/3rdparty/zlib/include/refl/detail/view.inl @@ -89,38 +89,54 @@ namespace refl { field->type->Call(*field, ArgsList); return true; } - template<_ReflCheck_Ctor R, typename ...Args> + template R ObjectView::Invoke(const Name& name, Args&&... args) { + //int x = 1; val = &x; *(int*)val = 2;//(x = 2) return *val; //val=>cast_to + //int* x = nullptr; val = &x; *(int**)val = 0xff1234;//(x = 0xff1234) return *val; + real_type_t data{}; + ArgsView ret{ &data, &TypeInfo*>::StaticClass }; auto field = cls->GetField(name, true); if (!field) { if (cls->parent) { return Parent().Invoke(name, args...); } - return {}; + return ret.cast_ret(); } - R ret{}; constexpr int inputSize = sizeof...(Args); auto params = field->type->GetParams(); int paramsSize = params.size(); - bool member = field->flag & FIELD_MEMBER_FLAG; - if (inputSize + member >= paramsSize) { - return false; - } std::vector ArgsList; ArgsList.reserve(paramsSize); ArgsList.emplace_back(ret); - if (member) { + + if (field->flag & FIELD_MEMBER_FLAG) { ArgsList.emplace_back(ptr, &TypeInfo::StaticClass); } (..., (ArgsList.emplace_back(args))); + + int argsSize = ArgsList.size(); + if (argsSize < paramsSize && field->flag & FIELD_METHOD_VALUE_FLAG) { + auto [argsPtr, valueSize] = field->value.method(); + if (argsSize + valueSize >= paramsSize) { + for (int i = valueSize + argsSize - paramsSize; i < valueSize; i++) { + ArgsList.push_back(*(argsPtr + i)); + } + argsSize = paramsSize; + } + } + + if (argsSize < paramsSize) { + return ret.cast_ret(); + } + for (int i = 0; i < paramsSize; i++) { if (ArgsList[i].cls != params[i] && !ArgsList[i].ConvertTo(params[i])) { - return ret; + return ret.cast_ret(); } } field->type->Call(*field, ArgsList); - return ret; + return ret.cast_ret(); } ObjectView ObjectView::Parent() { return { ptr, cls ? cls->parent : nullptr }; diff --git a/engine/3rdparty/zlib/include/refl/refl.h b/engine/3rdparty/zlib/include/refl/refl.h index ee8d526..ec5de73 100644 --- a/engine/3rdparty/zlib/include/refl/refl.h +++ b/engine/3rdparty/zlib/include/refl/refl.h @@ -22,7 +22,7 @@ namespace refl { }; } FieldPtr::Data method = { *(Method*)&ptr }; - return { name, &TypeInfo::type...)>::StaticClass, method, value, flag }; + return { name, &TypeInfo(*)(real_type_t...)>::StaticClass, method, value, flag }; } template static FieldPtr MakeMethodField(R(T::* ptr)(Args...), Name name, const std::vector& args = {}) { @@ -36,6 +36,6 @@ namespace refl { }; } FieldPtr::Data method = { *(Method*)&ptr }; - return { name, &TypeInfo::type...)>::StaticClass, method, value,flag }; + return { name, &TypeInfo(*)(const void*,real_type_t...)>::StaticClass, method, value,flag }; } } \ 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 6465696..29fbc78 100644 --- a/engine/3rdparty/zlib/test/refl_01.cpp +++ b/engine/3rdparty/zlib/test/refl_01.cpp @@ -21,9 +21,9 @@ struct vec3 : public vec3_parent { x2 = x1 * x2; cout << x2 << "vec3::norm" << endl; } - virtual float norm1(int x1 = 10) { + virtual float norm1(float& x1) { cout << x1 << "::norm1" << endl; - return x * y *z * x1; + return x1; } static void norm2(int x1 = 10) { cout << x1 << "::norm2" << endl; @@ -41,21 +41,56 @@ struct vec3 : public vec3_parent { MakeMemberField(&vec3::y, "y"), MakeMemberField(&vec3::z, "z"), MakeMethodField(&vec3::norm, "norm", {8, 9}), - MakeMethodField(&vec3::norm1, "norm1",{2}) + MakeMethodField(&vec3::norm1, "norm1",{(float)2.0}) }; } }; int main() { + { + //T = > T* + int x = 1; + auto val = &x; + //T* = > T + //*val; + //x = 2; + *val = 2; + } + { + //T* = > T* + int x = 1, y = 2; + int* px = &x; + auto val = px; + //T* => T* + //val; + //px = &y;//做不到哈 + val = &y; + + } + { + //T* => T** + int x = 1, y = 2; + int* px = &x; + auto val = &px; + //T** = > T*; + //*val; + //px = &y; + *val = &y; + } auto cls = &TypeInfo::StaticClass; int x = 2, y = 3; + int* q1; + void* q2 = &q1; + *(int**)q2 = &x; + ArgsView a(&y); + ArgsView b(x); + ArgsView c(&q1); auto ov = cls->New(); ov.Invoke("norm", x); ov.Invoke("norm", x); ov.Invoke("norm", x, y); - ov.Invoke("norm1", y); + auto f = ov.Invoke("norm1"); vec3* v = (vec3*)ov.ptr; - v->norm1(y); - ov.Invoke("norm1", y); + ov.Invoke("norm1", (float)y); delete v; cout << "hello world\n"; } \ No newline at end of file