diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index f092905a3ab0..8e429b0c3cd1 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -186,11 +186,21 @@ Variant::Type Variant::get_type_by_name(const String &p_type_name) { return (ptr == nullptr) ? VARIANT_MAX : *ptr; } +template +constexpr bool _contains(const Variant::Type (&p_types)[N], Variant::Type p_type) { + for (const Variant::Type type : p_types) { + if (type == p_type) { + return true; + } + } + return false; +} + bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { if (p_type_from == p_type_to) { return true; } - if (p_type_to == NIL) { //nil can convert to anything + if (p_type_to == NIL) { // Anything can convert to NIL. return true; } @@ -198,215 +208,132 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { return (p_type_to == OBJECT); } - const Type *valid_types = nullptr; - const Type *invalid_types = nullptr; - + // clang-format off switch (p_type_to) { case BOOL: { - static const Type valid[] = { + return _contains({ INT, FLOAT, STRING, - NIL, - }; - - valid_types = valid; - } break; + }, p_type_from); + } case INT: { - static const Type valid[] = { + return _contains({ BOOL, FLOAT, STRING, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case FLOAT: { - static const Type valid[] = { + return _contains({ BOOL, INT, STRING, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case STRING: { - static const Type invalid[] = { + return !_contains({ OBJECT, - NIL - }; - - invalid_types = invalid; - } break; + }, p_type_from); + } case VECTOR2: { - static const Type valid[] = { + return _contains({ VECTOR2I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR2I: { - static const Type valid[] = { + return _contains({ VECTOR2, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RECT2: { - static const Type valid[] = { + return _contains({ RECT2I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RECT2I: { - static const Type valid[] = { + return _contains({ RECT2, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case TRANSFORM2D: { - static const Type valid[] = { + return _contains({ TRANSFORM3D, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case VECTOR3: { - static const Type valid[] = { + return _contains({ VECTOR3I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR3I: { - static const Type valid[] = { + return _contains({ VECTOR3, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR4: { - static const Type valid[] = { + return _contains({ VECTOR4I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR4I: { - static const Type valid[] = { + return _contains({ VECTOR4, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case QUATERNION: { - static const Type valid[] = { + return _contains({ BASIS, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case BASIS: { - static const Type valid[] = { + return _contains({ QUATERNION, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case TRANSFORM3D: { - static const Type valid[] = { + return _contains({ TRANSFORM2D, QUATERNION, BASIS, PROJECTION, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case PROJECTION: { - static const Type valid[] = { + return _contains({ TRANSFORM3D, NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case COLOR: { - static const Type valid[] = { + return _contains({ STRING, INT, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RID: { - static const Type valid[] = { + return _contains({ OBJECT, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case OBJECT: { - static const Type valid[] = { - NIL - }; - - valid_types = valid; - } break; + return false; + } case STRING_NAME: { - static const Type valid[] = { + return _contains({ STRING, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case NODE_PATH: { - static const Type valid[] = { + return _contains({ STRING, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case ARRAY: { - static const Type valid[] = { + return _contains({ PACKED_BYTE_ARRAY, PACKED_INT32_ARRAY, PACKED_INT64_ARRAY, @@ -417,123 +344,71 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { PACKED_VECTOR2_ARRAY, PACKED_VECTOR3_ARRAY, PACKED_VECTOR4_ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } // arrays case PACKED_BYTE_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_INT32_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_INT64_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_FLOAT32_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_FLOAT64_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_STRING_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_VECTOR2_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_VECTOR3_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_COLOR_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_VECTOR4_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; - default: { - } - } - - if (valid_types) { - int i = 0; - while (valid_types[i] != NIL) { - if (p_type_from == valid_types[i]) { - return true; - } - i++; + }, p_type_from); } - - } else if (invalid_types) { - int i = 0; - while (invalid_types[i] != NIL) { - if (p_type_from == invalid_types[i]) { - return false; - } - i++; + default: { + return false; } - - return true; } - - return false; + // clang-format on } bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) { if (p_type_from == p_type_to) { return true; } - if (p_type_to == NIL) { //nil can convert to anything + if (p_type_to == NIL) { // Anything can convert to NIL. return true; } @@ -541,215 +416,132 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type return (p_type_to == OBJECT); } - const Type *valid_types = nullptr; - + // clang-format off switch (p_type_to) { case BOOL: { - static const Type valid[] = { + return _contains({ INT, FLOAT, //STRING, - NIL, - }; - - valid_types = valid; - } break; + }, p_type_from); + } case INT: { - static const Type valid[] = { + return _contains({ BOOL, FLOAT, //STRING, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case FLOAT: { - static const Type valid[] = { + return _contains({ BOOL, INT, //STRING, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case STRING: { - static const Type valid[] = { + return _contains({ NODE_PATH, STRING_NAME, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case VECTOR2: { - static const Type valid[] = { + return _contains({ VECTOR2I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR2I: { - static const Type valid[] = { + return _contains({ VECTOR2, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RECT2: { - static const Type valid[] = { + return _contains({ RECT2I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RECT2I: { - static const Type valid[] = { + return _contains({ RECT2, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case TRANSFORM2D: { - static const Type valid[] = { + return _contains({ TRANSFORM3D, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case VECTOR3: { - static const Type valid[] = { + return _contains({ VECTOR3I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR3I: { - static const Type valid[] = { + return _contains({ VECTOR3, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR4: { - static const Type valid[] = { + return _contains({ VECTOR4I, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case VECTOR4I: { - static const Type valid[] = { + return _contains({ VECTOR4, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case QUATERNION: { - static const Type valid[] = { + return _contains({ BASIS, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case BASIS: { - static const Type valid[] = { + return _contains({ QUATERNION, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case TRANSFORM3D: { - static const Type valid[] = { + return _contains({ TRANSFORM2D, QUATERNION, BASIS, PROJECTION, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case PROJECTION: { - static const Type valid[] = { + return _contains({ TRANSFORM3D, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case COLOR: { - static const Type valid[] = { + return _contains({ STRING, INT, - NIL, - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case RID: { - static const Type valid[] = { + return _contains({ OBJECT, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case OBJECT: { - static const Type valid[] = { - NIL - }; - - valid_types = valid; - } break; + return false; + } case STRING_NAME: { - static const Type valid[] = { + return _contains({ STRING, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case NODE_PATH: { - static const Type valid[] = { + return _contains({ STRING, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case ARRAY: { - static const Type valid[] = { + return _contains({ PACKED_BYTE_ARRAY, PACKED_INT32_ARRAY, PACKED_INT64_ARRAY, @@ -760,105 +552,64 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type PACKED_VECTOR2_ARRAY, PACKED_VECTOR3_ARRAY, PACKED_VECTOR4_ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } // arrays case PACKED_BYTE_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_INT32_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_INT64_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_FLOAT32_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_FLOAT64_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_STRING_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - } break; + }, p_type_from); + } case PACKED_VECTOR2_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_VECTOR3_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_COLOR_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - - valid_types = valid; - - } break; + }, p_type_from); + } case PACKED_VECTOR4_ARRAY: { - static const Type valid[] = { + return _contains({ ARRAY, - NIL - }; - valid_types = valid; - - } break; - default: { + }, p_type_from); } - } - - if (valid_types) { - int i = 0; - while (valid_types[i] != NIL) { - if (p_type_from == valid_types[i]) { - return true; - } - i++; + default: { + return false; } } - - return false; + // clang-format on } bool Variant::operator==(const Variant &p_variant) const {