From 35e7490c9583f5fed9c419666be7451d1a4f035c Mon Sep 17 00:00:00 2001 From: George Marques Date: Tue, 15 Dec 2020 16:31:18 -0300 Subject: [PATCH 1/2] Add PtrConstruct template to use in Variant constructors Since the PtrToArg::encode requires the value to be constructed previously. With PtrConstruct this is not required. --- core/variant/variant_construct.cpp | 74 ++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 3bb3fa3634f..02866b44754 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -39,6 +39,60 @@ #include "core/templates/local_vector.h" #include "core/templates/oa_hash_map.h" +template +struct PtrConstruct {}; + +#define MAKE_PTRCONSTRUCT(m_type) \ + template <> \ + struct PtrConstruct { \ + _FORCE_INLINE_ static void construct(const m_type &p_value, void *p_ptr) { \ + memnew_placement(p_ptr, m_type(p_value)); \ + } \ + }; + +MAKE_PTRCONSTRUCT(bool); +MAKE_PTRCONSTRUCT(int64_t); +MAKE_PTRCONSTRUCT(double); +MAKE_PTRCONSTRUCT(String); +MAKE_PTRCONSTRUCT(Vector2); +MAKE_PTRCONSTRUCT(Vector2i); +MAKE_PTRCONSTRUCT(Rect2); +MAKE_PTRCONSTRUCT(Rect2i); +MAKE_PTRCONSTRUCT(Vector3); +MAKE_PTRCONSTRUCT(Vector3i); +MAKE_PTRCONSTRUCT(Transform2D); +MAKE_PTRCONSTRUCT(Plane); +MAKE_PTRCONSTRUCT(Quat); +MAKE_PTRCONSTRUCT(AABB); +MAKE_PTRCONSTRUCT(Basis); +MAKE_PTRCONSTRUCT(Transform); +MAKE_PTRCONSTRUCT(Color); +MAKE_PTRCONSTRUCT(StringName); +MAKE_PTRCONSTRUCT(NodePath); +MAKE_PTRCONSTRUCT(RID); + +template <> +struct PtrConstruct { + _FORCE_INLINE_ static void construct(Object *p_value, void *p_ptr) { + *((Object **)p_ptr) = p_value; + } +}; + +MAKE_PTRCONSTRUCT(Callable); +MAKE_PTRCONSTRUCT(Signal); +MAKE_PTRCONSTRUCT(Dictionary); +MAKE_PTRCONSTRUCT(Array); +MAKE_PTRCONSTRUCT(PackedByteArray); +MAKE_PTRCONSTRUCT(PackedInt32Array); +MAKE_PTRCONSTRUCT(PackedInt64Array); +MAKE_PTRCONSTRUCT(PackedFloat32Array); +MAKE_PTRCONSTRUCT(PackedFloat64Array); +MAKE_PTRCONSTRUCT(PackedStringArray); +MAKE_PTRCONSTRUCT(PackedVector2Array); +MAKE_PTRCONSTRUCT(PackedVector3Array); +MAKE_PTRCONSTRUCT(PackedColorArray); +MAKE_PTRCONSTRUCT(Variant); + template class VariantConstructor { template @@ -59,7 +113,7 @@ class VariantConstructor { template static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence) { - PtrToArg::encode(T(PtrToArg

::convert(p_args[Is])...), base); + PtrConstruct::construct(T(PtrToArg

::convert(p_args[Is])...), base); } public: @@ -112,7 +166,7 @@ public: VariantInternal::object_assign(&r_ret, p_args[0]); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(PtrToArg::convert(p_args[0]), base); + PtrConstruct::construct(PtrToArg::convert(p_args[0]), base); } static int get_argument_count() { @@ -146,7 +200,7 @@ public: VariantInternal::object_assign_null(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(nullptr, base); + PtrConstruct::construct(nullptr, base); } static int get_argument_count() { @@ -199,7 +253,7 @@ public: *VariantGetInternalPtr::get_ptr(&r_ret) = Callable(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr::get_ptr(p_args[1])); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(Callable(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1])), base); + PtrConstruct::construct(Callable(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1])), base); } static int get_argument_count() { @@ -256,7 +310,7 @@ public: *VariantGetInternalPtr::get_ptr(&r_ret) = Signal(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr::get_ptr(p_args[1])); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(Signal(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1])), base); + PtrConstruct::construct(Signal(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1])), base); } static int get_argument_count() { @@ -319,7 +373,7 @@ public: dst_arr[i] = src_arr[i]; } - PtrToArg::encode(dst_arr, base); + PtrConstruct::construct(dst_arr, base); } static int get_argument_count() { @@ -378,7 +432,7 @@ public: dst_arr.write[i] = src_arr[i]; } - PtrToArg::encode(dst_arr, base); + PtrConstruct::construct(dst_arr, base); } static int get_argument_count() { @@ -412,7 +466,7 @@ public: VariantInternal::clear(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(Variant(), base); + PtrConstruct::construct(Variant(), base); } static int get_argument_count() { @@ -440,7 +494,7 @@ public: VariantTypeChanger::change_and_reset(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(T(), base); + PtrConstruct::construct(T(), base); } static int get_argument_count() { @@ -496,7 +550,7 @@ public: VariantInternal::object_assign_null(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg::encode(nullptr, base); + PtrConstruct::construct(nullptr, base); } static int get_argument_count() { From b3f452b1d2e3a02a1e9b3926cb0039ce7257ae8a Mon Sep 17 00:00:00 2001 From: George Marques Date: Tue, 15 Dec 2020 17:02:50 -0300 Subject: [PATCH 2/2] Change template order in method_ptrcall.h To be consistent with the enum in Variant so missing types can be more easily spotted. --- core/variant/method_ptrcall.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/variant/method_ptrcall.h b/core/variant/method_ptrcall.h index 40fa3543dce..b00455f8ad6 100644 --- a/core/variant/method_ptrcall.h +++ b/core/variant/method_ptrcall.h @@ -100,6 +100,7 @@ struct PtrToArg {}; } MAKE_PTRARG(bool); +// Integer types. MAKE_PTRARGCONV(uint8_t, int64_t); MAKE_PTRARGCONV(int8_t, int64_t); MAKE_PTRARGCONV(uint16_t, int64_t); @@ -108,15 +109,16 @@ MAKE_PTRARGCONV(uint32_t, int64_t); MAKE_PTRARGCONV(int32_t, int64_t); MAKE_PTRARG(int64_t); MAKE_PTRARG(uint64_t); +// Float types MAKE_PTRARGCONV(float, double); MAKE_PTRARG(double); MAKE_PTRARG(String); MAKE_PTRARG(Vector2); -MAKE_PTRARG(Rect2); -MAKE_PTRARG_BY_REFERENCE(Vector3); MAKE_PTRARG(Vector2i); +MAKE_PTRARG(Rect2); MAKE_PTRARG(Rect2i); +MAKE_PTRARG_BY_REFERENCE(Vector3); MAKE_PTRARG_BY_REFERENCE(Vector3i); MAKE_PTRARG(Transform2D); MAKE_PTRARG_BY_REFERENCE(Plane); @@ -128,9 +130,10 @@ MAKE_PTRARG_BY_REFERENCE(Color); MAKE_PTRARG(StringName); MAKE_PTRARG(NodePath); MAKE_PTRARG(RID); -MAKE_PTRARG(Dictionary); +// Object doesn't need this. MAKE_PTRARG(Callable); MAKE_PTRARG(Signal); +MAKE_PTRARG(Dictionary); MAKE_PTRARG(Array); MAKE_PTRARG(PackedByteArray); MAKE_PTRARG(PackedInt32Array);