diff options
author | George Marques <george@gmarqu.es> | 2021-03-09 12:30:42 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2021-03-18 10:18:59 -0300 |
commit | 997a8ae9e8fc04a1a0ea222cbd4922a43861a959 (patch) | |
tree | fa172a1a4bbc797ed31f74592e11159184e19fdb /core/variant/variant_setget.cpp | |
parent | fbfdd5e1104a883c311fddf3a7ed6763f35f3781 (diff) |
Make Variant setget use set() method of Array
This ensure that typed arrays are properly checked when setting an
element.
Moved the macro to a straight declaration since the macro was only used
for Array and it now is quite specific to the Array class.
Diffstat (limited to 'core/variant/variant_setget.cpp')
-rw-r--r-- | core/variant/variant_setget.cpp | 118 |
1 files changed, 58 insertions, 60 deletions
diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index f9cc7c4ff4..b86ae3ebb2 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -875,65 +875,64 @@ Variant Variant::get_named(const StringName &p_member, bool &r_valid) const { static uint64_t get_indexed_size(const Variant *base) { return m_max; } \ }; -#define INDEXED_SETGET_STRUCT_VARIANT(m_base_type) \ - struct VariantIndexedSetGet_##m_base_type { \ - static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \ - int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - *oob = true; \ - return; \ - } \ - *value = (*VariantGetInternalPtr<m_base_type>::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<const m_base_type *>(base); \ - if (index < 0) \ - index += v.size(); \ - OOB_TEST(index, v.size()); \ - PtrToArg<Variant>::encode(v[index], member); \ - } \ - static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \ - int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - *oob = true; \ - *valid = false; \ - return; \ - } \ - (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = *value; \ - *oob = false; \ - *valid = true; \ - } \ - static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \ - int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \ - if (index < 0) { \ - index += size; \ - } \ - if (index < 0 || index >= size) { \ - *oob = true; \ - return; \ - } \ - (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = *value; \ - *oob = false; \ - } \ - static void ptr_set(void *base, int64_t index, const void *member) { \ - /* avoid ptrconvert for performance*/ \ - m_base_type &v = *reinterpret_cast<m_base_type *>(base); \ - if (index < 0) \ - index += v.size(); \ - OOB_TEST(index, v.size()); \ - v[index] = PtrToArg<Variant>::convert(member); \ - } \ - static Variant::Type get_index_type() { return Variant::NIL; } \ - static uint64_t get_indexed_size(const Variant *base) { return 0; } \ - }; +struct VariantIndexedSetGet_Array { + static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { + int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size(); + if (index < 0) { + index += size; + } + if (index < 0 || index >= size) { + *oob = true; + return; + } + *value = (*VariantGetInternalPtr<Array>::get_ptr(base))[index]; + *oob = false; + } + static void ptr_get(const void *base, int64_t index, void *member) { + /* avoid ptrconvert for performance*/ + const Array &v = *reinterpret_cast<const Array *>(base); + if (index < 0) + index += v.size(); + OOB_TEST(index, v.size()); + PtrToArg<Variant>::encode(v[index], member); + } + static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { + int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size(); + if (index < 0) { + index += size; + } + if (index < 0 || index >= size) { + *oob = true; + *valid = false; + return; + } + VariantGetInternalPtr<Array>::get_ptr(base)->set(index, *value); + *oob = false; + *valid = true; + } + static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { + int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size(); + if (index < 0) { + index += size; + } + if (index < 0 || index >= size) { + *oob = true; + return; + } + VariantGetInternalPtr<Array>::get_ptr(base)->set(index, *value); + *oob = false; + } + static void ptr_set(void *base, int64_t index, const void *member) { + /* avoid ptrconvert for performance*/ + Array &v = *reinterpret_cast<Array *>(base); + if (index < 0) + index += v.size(); + OOB_TEST(index, v.size()); + v.set(index, PtrToArg<Variant>::convert(member)); + } + static Variant::Type get_index_type() { return Variant::NIL; } + static uint64_t get_indexed_size(const Variant *base) { return 0; } +}; #define INDEXED_SETGET_STRUCT_DICT(m_base_type) \ struct VariantIndexedSetGet_##m_base_type { \ @@ -990,7 +989,6 @@ INDEXED_SETGET_STRUCT_TYPED(PackedVector3Array, Vector3) INDEXED_SETGET_STRUCT_TYPED(PackedStringArray, String) INDEXED_SETGET_STRUCT_TYPED(PackedColorArray, Color) -INDEXED_SETGET_STRUCT_VARIANT(Array) INDEXED_SETGET_STRUCT_DICT(Dictionary) struct VariantIndexedSetterGetterInfo { |