From 3edb671b8a8b16658da7d9ab6dca406004697f4c Mon Sep 17 00:00:00 2001 From: Micky Date: Mon, 10 Oct 2022 23:10:59 +0200 Subject: [PATCH] Use Packed*Array over Vector in Variant Also shuffles some method definitions and declarations to be more consistent with the way the Variant types are ordered across the codebase. And removes an unnecessary JSON assign (`JSON::stringify` can now be accessed statically) --- core/variant/variant.cpp | 182 +++++++++++++++++++-------------------- core/variant/variant.h | 44 +++++----- 2 files changed, 113 insertions(+), 113 deletions(-) diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index d918e7275583..67f2c10099ec 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -1793,31 +1793,31 @@ String Variant::stringify(int recursion_count) const { } // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed. case PACKED_VECTOR2_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedVector2Array(), recursion_count); } case PACKED_VECTOR3_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedVector3Array(), recursion_count); } case PACKED_COLOR_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedColorArray(), recursion_count); } case PACKED_STRING_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedStringArray(), recursion_count); } case PACKED_BYTE_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedByteArray(), recursion_count); } case PACKED_INT32_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedInt32Array(), recursion_count); } case PACKED_INT64_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedInt64Array(), recursion_count); } case PACKED_FLOAT32_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedFloat32Array(), recursion_count); } case PACKED_FLOAT64_ARRAY: { - return stringify_vector(operator Vector(), recursion_count); + return stringify_vector(operator PackedFloat64Array(), recursion_count); } case ARRAY: { ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!"); @@ -2207,31 +2207,31 @@ inline DA _convert_array_from_variant(const Variant &p_variant) { return _convert_array(p_variant.operator Array()); } case Variant::PACKED_BYTE_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedByteArray()); } case Variant::PACKED_INT32_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedInt32Array()); } case Variant::PACKED_INT64_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedInt64Array()); } case Variant::PACKED_FLOAT32_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedFloat32Array()); } case Variant::PACKED_FLOAT64_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedFloat64Array()); } case Variant::PACKED_STRING_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedStringArray()); } case Variant::PACKED_VECTOR2_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedVector2Array()); } case Variant::PACKED_VECTOR3_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedVector3Array()); } case Variant::PACKED_COLOR_ARRAY: { - return _convert_array>(p_variant.operator Vector()); + return _convert_array(p_variant.operator PackedColorArray()); } default: { return DA(); @@ -2247,75 +2247,75 @@ Variant::operator Array() const { } } -Variant::operator Vector() const { +Variant::operator PackedByteArray() const { if (type == PACKED_BYTE_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedInt32Array() const { if (type == PACKED_INT32_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedInt64Array() const { if (type == PACKED_INT64_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedFloat32Array() const { if (type == PACKED_FLOAT32_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedFloat64Array() const { if (type == PACKED_FLOAT64_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedStringArray() const { if (type == PACKED_STRING_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { - if (type == PACKED_VECTOR3_ARRAY) { - return static_cast *>(_data.packed_array)->array; +Variant::operator PackedVector2Array() const { + if (type == PACKED_VECTOR2_ARRAY) { + return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { - if (type == PACKED_VECTOR2_ARRAY) { - return static_cast *>(_data.packed_array)->array; +Variant::operator PackedVector3Array() const { + if (type == PACKED_VECTOR3_ARRAY) { + return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } -Variant::operator Vector() const { +Variant::operator PackedColorArray() const { if (type == PACKED_COLOR_ARRAY) { return static_cast *>(_data.packed_array)->array; } else { - return _convert_array_from_variant>(*this); + return _convert_array_from_variant(*this); } } @@ -2350,7 +2350,7 @@ Variant::operator Vector() const { } Variant::operator Vector() const { - Vector va = operator Vector(); + PackedVector3Array va = operator PackedVector3Array(); Vector faces; int va_size = va.size(); if (va_size == 0) { @@ -2386,7 +2386,7 @@ Variant::operator Vector() const { } Variant::operator Vector() const { - Vector from = operator Vector(); + PackedStringArray from = operator PackedStringArray(); Vector to; int len = from.size(); to.resize(len); @@ -2646,78 +2646,79 @@ Variant::Variant(const Array &p_array) { memnew_placement(_data._mem, Array(p_array)); } -Variant::Variant(const Vector &p_array) { - type = ARRAY; - - Array *plane_array = memnew_placement(_data._mem, Array); - - plane_array->resize(p_array.size()); - - for (int i = 0; i < p_array.size(); i++) { - plane_array->operator[](i) = Variant(p_array[i]); - } -} - -Variant::Variant(const Vector<::RID> &p_array) { - type = ARRAY; - - Array *rid_array = memnew_placement(_data._mem, Array); - - rid_array->resize(p_array.size()); - - for (int i = 0; i < p_array.size(); i++) { - rid_array->set(i, Variant(p_array[i])); - } -} - -Variant::Variant(const Vector &p_byte_array) { +Variant::Variant(const PackedByteArray &p_byte_array) { type = PACKED_BYTE_ARRAY; _data.packed_array = PackedArrayRef::create(p_byte_array); } -Variant::Variant(const Vector &p_int32_array) { +Variant::Variant(const PackedInt32Array &p_int32_array) { type = PACKED_INT32_ARRAY; _data.packed_array = PackedArrayRef::create(p_int32_array); } -Variant::Variant(const Vector &p_int64_array) { +Variant::Variant(const PackedInt64Array &p_int64_array) { type = PACKED_INT64_ARRAY; _data.packed_array = PackedArrayRef::create(p_int64_array); } -Variant::Variant(const Vector &p_float32_array) { +Variant::Variant(const PackedFloat32Array &p_float32_array) { type = PACKED_FLOAT32_ARRAY; _data.packed_array = PackedArrayRef::create(p_float32_array); } -Variant::Variant(const Vector &p_float64_array) { +Variant::Variant(const PackedFloat64Array &p_float64_array) { type = PACKED_FLOAT64_ARRAY; _data.packed_array = PackedArrayRef::create(p_float64_array); } -Variant::Variant(const Vector &p_string_array) { +Variant::Variant(const PackedStringArray &p_string_array) { type = PACKED_STRING_ARRAY; _data.packed_array = PackedArrayRef::create(p_string_array); } -Variant::Variant(const Vector &p_vector3_array) { - type = PACKED_VECTOR3_ARRAY; - _data.packed_array = PackedArrayRef::create(p_vector3_array); -} - -Variant::Variant(const Vector &p_vector2_array) { +Variant::Variant(const PackedVector2Array &p_vector2_array) { type = PACKED_VECTOR2_ARRAY; _data.packed_array = PackedArrayRef::create(p_vector2_array); } -Variant::Variant(const Vector &p_color_array) { +Variant::Variant(const PackedVector3Array &p_vector3_array) { + type = PACKED_VECTOR3_ARRAY; + _data.packed_array = PackedArrayRef::create(p_vector3_array); +} + +Variant::Variant(const PackedColorArray &p_color_array) { type = PACKED_COLOR_ARRAY; _data.packed_array = PackedArrayRef::create(p_color_array); } +/* helpers */ +Variant::Variant(const Vector<::RID> &p_array) { + type = ARRAY; + + Array *rid_array = memnew_placement(_data._mem, Array); + + rid_array->resize(p_array.size()); + + for (int i = 0; i < p_array.size(); i++) { + rid_array->set(i, Variant(p_array[i])); + } +} + +Variant::Variant(const Vector &p_array) { + type = ARRAY; + + Array *plane_array = memnew_placement(_data._mem, Array); + + plane_array->resize(p_array.size()); + + for (int i = 0; i < p_array.size(); i++) { + plane_array->operator[](i) = Variant(p_array[i]); + } +} + Variant::Variant(const Vector &p_face_array) { - Vector vertices; + PackedVector3Array vertices; int face_count = p_face_array.size(); vertices.resize(face_count * 3); @@ -2737,7 +2738,6 @@ Variant::Variant(const Vector &p_face_array) { *this = vertices; } -/* helpers */ Variant::Variant(const Vector &p_array) { type = NIL; Array arr; @@ -2750,7 +2750,7 @@ Variant::Variant(const Vector &p_array) { Variant::Variant(const Vector &p_array) { type = NIL; - Vector v; + PackedStringArray v; int len = p_array.size(); v.resize(len); for (int i = 0; i < len; i++) { @@ -3100,7 +3100,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_BYTE_ARRAY: { - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedByteArray &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { const uint8_t *r = arr.ptr(); @@ -3111,7 +3111,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_INT32_ARRAY: { - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedInt32Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { const int32_t *r = arr.ptr(); @@ -3122,7 +3122,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_INT64_ARRAY: { - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedInt64Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { const int64_t *r = arr.ptr(); @@ -3133,7 +3133,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_FLOAT32_ARRAY: { - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedFloat32Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { @@ -3149,7 +3149,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_FLOAT64_ARRAY: { - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedFloat64Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { @@ -3166,7 +3166,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_STRING_ARRAY: { uint32_t hash = HASH_MURMUR3_SEED; - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedStringArray &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { @@ -3182,7 +3182,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_VECTOR2_ARRAY: { uint32_t hash = HASH_MURMUR3_SEED; - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedVector2Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { @@ -3199,7 +3199,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_VECTOR3_ARRAY: { uint32_t hash = HASH_MURMUR3_SEED; - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedVector3Array &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { @@ -3217,7 +3217,7 @@ uint32_t Variant::recursive_hash(int recursion_count) const { } break; case PACKED_COLOR_ARRAY: { uint32_t hash = HASH_MURMUR3_SEED; - const Vector &arr = PackedArrayRef::get_array(_data.packed_array); + const PackedColorArray &arr = PackedArrayRef::get_array(_data.packed_array); int len = arr.size(); if (likely(len)) { diff --git a/core/variant/variant.h b/core/variant/variant.h index c3c87d4692aa..d685444c30fe 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -404,21 +404,21 @@ class Variant { operator Dictionary() const; operator Array() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; - operator Vector() const; + operator PackedByteArray() const; + operator PackedInt32Array() const; + operator PackedInt64Array() const; + operator PackedFloat32Array() const; + operator PackedFloat64Array() const; + operator PackedStringArray() const; + operator PackedVector3Array() const; + operator PackedVector2Array() const; + operator PackedColorArray() const; + + operator Vector<::RID>() const; operator Vector() const; operator Vector() const; - operator Vector() const; operator Vector() const; - operator Vector<::RID>() const; - operator Vector() const; // some core type enums to convert to operator Side() const; @@ -473,21 +473,21 @@ class Variant { Variant(const Dictionary &p_dictionary); Variant(const Array &p_array); + Variant(const PackedByteArray &p_byte_array); + Variant(const PackedInt32Array &p_int32_array); + Variant(const PackedInt64Array &p_int64_array); + Variant(const PackedFloat32Array &p_float32_array); + Variant(const PackedFloat64Array &p_float64_array); + Variant(const PackedStringArray &p_string_array); + Variant(const PackedVector2Array &p_vector2_array); + Variant(const PackedVector3Array &p_vector3_array); + Variant(const PackedColorArray &p_color_array); + + Variant(const Vector<::RID> &p_array); // helper Variant(const Vector &p_array); // helper - Variant(const Vector &p_byte_array); - Variant(const Vector &p_int32_array); - Variant(const Vector &p_int64_array); - Variant(const Vector &p_float32_array); - Variant(const Vector &p_float64_array); - Variant(const Vector &p_string_array); - Variant(const Vector &p_vector3_array); - Variant(const Vector &p_color_array); Variant(const Vector &p_face_array); - Variant(const Vector &p_array); Variant(const Vector &p_array); - Variant(const Vector<::RID> &p_array); // helper - Variant(const Vector &p_array); // helper Variant(const IPAddress &p_address);