From 8a9e3524a9128d71b78718d2ba1081d5ad34a050 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 20 Nov 2020 15:51:28 -0300 Subject: Fix variant getters not setting return type The validated getters were only setting the value without changing the type, leading to wrong results. This uses the same path used for methods to the same purpose. --- core/variant/variant_call.cpp | 27 +++------------------------ core/variant/variant_internal.h | 22 ++++++++++++++++++++++ core/variant/variant_setget.cpp | 12 ++++++++++++ 3 files changed, 37 insertions(+), 24 deletions(-) (limited to 'core') diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index f6fe642493..13514b7b9f 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -42,27 +42,6 @@ typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args); typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args); -template -struct TypeAdjust { - _FORCE_INLINE_ static void adjust(Variant *r_ret) { - VariantTypeChanger::type_t>::change(r_ret); - } -}; - -template <> //do nothing for variant -struct TypeAdjust { - _FORCE_INLINE_ static void adjust(Variant *r_ret) { - } -}; - -template <> //do nothing for variant -struct TypeAdjust { - _FORCE_INLINE_ static void adjust(Variant *r_ret) { - VariantInternal::clear(r_ret); - *r_ret = (Object *)nullptr; - } -}; - template static _FORCE_INLINE_ void vc_method_call(R (T::*method)(P...), Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { call_with_variant_args_ret_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, p_defvals); @@ -124,12 +103,12 @@ static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...) const, void *p_bas template static _FORCE_INLINE_ void vc_change_return_type(R (T::*method)(P...), Variant *v) { - TypeAdjust::adjust(v); + VariantTypeAdjust::adjust(v); } template static _FORCE_INLINE_ void vc_change_return_type(R (T::*method)(P...) const, Variant *v) { - TypeAdjust::adjust(v); + VariantTypeAdjust::adjust(v); } template @@ -144,7 +123,7 @@ static _FORCE_INLINE_ void vc_change_return_type(void (T::*method)(P...) const, template static _FORCE_INLINE_ void vc_change_return_type(R (*method)(P...), Variant *v) { - TypeAdjust::adjust(v); + VariantTypeAdjust::adjust(v); } template diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 3ac7f32dec..3c3be44ef7 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -1128,4 +1128,26 @@ struct VariantTypeChanger { } }; +template +struct VariantTypeAdjust { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + VariantTypeChanger::type_t>::change(r_ret); + } +}; + +template <> +struct VariantTypeAdjust { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + // Do nothing for variant. + } +}; + +template <> +struct VariantTypeAdjust { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + VariantInternal::clear(r_ret); + *r_ret = (Object *)nullptr; + } +}; + #endif // VARIANT_INTERNAL_H diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index 05fe2b80d9..59dde707c7 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -44,6 +44,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ static void ptr_get(const void *base, void *member) { \ @@ -74,6 +75,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ static void ptr_get(const void *base, void *member) { \ @@ -107,6 +109,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ static void ptr_get(const void *base, void *member) { \ @@ -137,6 +140,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ static void ptr_get(const void *base, void *member) { \ @@ -170,6 +174,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ static void ptr_get(const void *base, void *member) { \ @@ -200,6 +205,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ static void ptr_get(const void *base, void *member) { \ @@ -233,6 +239,7 @@ *member = VariantGetInternalPtr::get_ptr(base)->m_getter(m_index); \ } \ static void validated_get(const Variant *base, Variant *member) { \ + VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(m_index); \ } \ static void ptr_get(const void *base, void *member) { \ @@ -617,6 +624,7 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { oob = true; \ return; \ } \ + VariantTypeAdjust::adjust(value); \ *VariantGetInternalPtr::get_ptr(value) = (*VariantGetInternalPtr::get_ptr(base))[index]; \ oob = false; \ } \ @@ -694,6 +702,7 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { oob = true; \ return; \ } \ + VariantTypeAdjust::adjust(value); \ *VariantGetInternalPtr::get_ptr(value) = (*VariantGetInternalPtr::get_ptr(base))[index]; \ oob = false; \ } \ @@ -768,6 +777,7 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { oob = true; \ return; \ } \ + VariantTypeAdjust::adjust(value); \ *VariantGetInternalPtr::get_ptr(value) = (*VariantGetInternalPtr::get_ptr(base))[index]; \ oob = false; \ } \ @@ -830,6 +840,7 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { oob = true; \ return; \ } \ + VariantTypeAdjust::adjust(value); \ *VariantGetInternalPtr::get_ptr(value) = (*VariantGetInternalPtr::get_ptr(base))m_accessor[index]; \ oob = false; \ } \ @@ -886,6 +897,7 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { oob = true; \ return; \ } \ + VariantTypeAdjust::adjust(value); \ *VariantGetInternalPtr::get_ptr(value) = VariantGetInternalPtr::get_ptr(base)->m_get(index); \ oob = false; \ } \ -- cgit v1.2.3 From c8e3fb8b05ccbbc3bd05919e91d7bee1eed96c9e Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 20 Nov 2020 17:26:19 -0300 Subject: Variant: Merge get() and validated_get() Since they do pretty much the same thing. The validated_get() was renamed to get() since that is more performant. --- core/variant/variant_setget.cpp | 94 +---------------------------------------- 1 file changed, 2 insertions(+), 92 deletions(-) (limited to 'core') diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index 59dde707c7..f6a2c11830 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -41,9 +41,6 @@ #define SETGET_STRUCT(m_base_type, m_member_type, m_member) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_member; \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ @@ -72,9 +69,6 @@ #define SETGET_NUMBER_STRUCT(m_base_type, m_member_type, m_member) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_member; \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_member; \ } \ @@ -106,9 +100,6 @@ #define SETGET_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_custom; \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ @@ -137,9 +128,6 @@ #define SETGET_NUMBER_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_custom; \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_custom; \ } \ @@ -171,9 +159,6 @@ #define SETGET_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ @@ -202,9 +187,6 @@ #define SETGET_NUMBER_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(); \ } \ @@ -236,9 +218,6 @@ #define SETGET_STRUCT_FUNC_INDEX(m_base_type, m_member_type, m_member, m_setter, m_getter, m_index) \ struct VariantSetGet_##m_base_type##_##m_member { \ static void get(const Variant *base, Variant *member) { \ - *member = VariantGetInternalPtr::get_ptr(base)->m_getter(m_index); \ - } \ - static void validated_get(const Variant *base, Variant *member) { \ VariantTypeAdjust::adjust(member); \ *VariantGetInternalPtr::get_ptr(member) = VariantGetInternalPtr::get_ptr(base)->m_getter(m_index); \ } \ @@ -347,7 +326,7 @@ static void register_member(Variant::Type p_type, const StringName &p_member) { sgi.ptr_setter = T::ptr_set; sgi.getter = T::get; - sgi.validated_getter = T::validated_get; + sgi.validated_getter = T::get; sgi.ptr_getter = T::ptr_get; sgi.member_type = T::get_type(); @@ -604,18 +583,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { #define INDEXED_SETGET_STRUCT_TYPED(m_base_type, m_elem_type) \ struct VariantIndexedSetGet_##m_base_type { \ static void get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - int64_t size = VariantGetInternalPtr::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr::get_ptr(base))[index]; \ - oob = false; \ - } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ int64_t size = VariantGetInternalPtr::get_ptr(base)->size(); \ if (index < 0) { \ index += size; \ @@ -682,18 +649,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { #define INDEXED_SETGET_STRUCT_TYPED_NUMERIC(m_base_type, m_elem_type, m_assign_type) \ struct VariantIndexedSetGet_##m_base_type { \ static void get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - int64_t size = VariantGetInternalPtr::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr::get_ptr(base))[index]; \ - oob = false; \ - } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ int64_t size = VariantGetInternalPtr::get_ptr(base)->size(); \ if (index < 0) { \ index += size; \ @@ -765,14 +720,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { #define INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(m_base_type, m_elem_type, m_assign_type, m_max) \ struct VariantIndexedSetGet_##m_base_type { \ static void get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - if (index < 0 || index >= m_max) { \ - oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr::get_ptr(base))[index]; \ - oob = false; \ - } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ if (index < 0 || index >= m_max) { \ oob = true; \ return; \ @@ -828,14 +775,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { #define INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(m_base_type, m_elem_type, m_accessor, m_max) \ struct VariantIndexedSetGet_##m_base_type { \ static void get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - if (index < 0 || index >= m_max) { \ - oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr::get_ptr(base))m_accessor[index]; \ - oob = false; \ - } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ if (index < 0 || index >= m_max) { \ oob = true; \ return; \ @@ -885,14 +824,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { #define INDEXED_SETGET_STRUCT_BULTIN_FUNC(m_base_type, m_elem_type, m_set, m_get, m_max) \ struct VariantIndexedSetGet_##m_base_type { \ static void get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - if (index < 0 || index >= m_max) { \ - oob = true; \ - return; \ - } \ - *value = VariantGetInternalPtr::get_ptr(base)->m_get(index); \ - oob = false; \ - } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ if (index < 0 || index >= m_max) { \ oob = true; \ return; \ @@ -953,18 +884,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { *value = (*VariantGetInternalPtr::get_ptr(base))[index]; \ oob = false; \ } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - int64_t size = VariantGetInternalPtr::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr::get_ptr(base))[index]; \ - oob = false; \ - } \ static void ptr_get(const void *base, int64_t index, void *member) { \ /* avoid ptrconvert for performance*/ \ const m_base_type &v = *reinterpret_cast(base); \ @@ -1022,15 +941,6 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { *value = *ptr; \ oob = false; \ } \ - static void validated_get(const Variant *base, int64_t index, Variant *value, bool &oob) { \ - const Variant *ptr = VariantGetInternalPtr::get_ptr(base)->getptr(index); \ - if (!ptr) { \ - oob = true; \ - return; \ - } \ - *value = *ptr; \ - oob = false; \ - } \ static void ptr_get(const void *base, int64_t index, void *member) { \ /* avoid ptrconvert for performance*/ \ const m_base_type &v = *reinterpret_cast(base); \ @@ -1106,7 +1016,7 @@ static void register_indexed_member(Variant::Type p_type) { sgi.ptr_setter = T::ptr_set; sgi.getter = T::get; - sgi.validated_getter = T::validated_get; + sgi.validated_getter = T::get; sgi.ptr_getter = T::ptr_get; sgi.index_type = T::get_index_type(); -- cgit v1.2.3