diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index f092905a3ab0..147ae88f2671 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -1486,147 +1486,35 @@ void Variant::_clear_internal() { } Variant::operator int64_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return int64_t(_data._int); - case FLOAT: - return int64_t(_data._float); - case STRING: - return int64_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator int32_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return int32_t(_data._int); - case FLOAT: - return int32_t(_data._float); - case STRING: - return int32_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator int16_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return int16_t(_data._int); - case FLOAT: - return int16_t(_data._float); - case STRING: - return int16_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator int8_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return int8_t(_data._int); - case FLOAT: - return int8_t(_data._float); - case STRING: - return int8_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator uint64_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return uint64_t(_data._int); - case FLOAT: - return uint64_t(_data._float); - case STRING: - return uint64_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator uint32_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return uint32_t(_data._int); - case FLOAT: - return uint32_t(_data._float); - case STRING: - return uint32_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator uint16_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return uint16_t(_data._int); - case FLOAT: - return uint16_t(_data._float); - case STRING: - return uint16_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator uint8_t() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1 : 0; - case INT: - return uint8_t(_data._int); - case FLOAT: - return uint8_t(_data._float); - case STRING: - return uint8_t(operator String().to_int()); - default: { - return 0; - } - } + return _to_int(); } Variant::operator ObjectID() const { @@ -1644,39 +1532,11 @@ Variant::operator char32_t() const { } Variant::operator float() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1.0 : 0.0; - case INT: - return (float)_data._int; - case FLOAT: - return _data._float; - case STRING: - return operator String().to_float(); - default: { - return 0; - } - } + return _to_float(); } Variant::operator double() const { - switch (type) { - case NIL: - return 0; - case BOOL: - return _data._bool ? 1.0 : 0.0; - case INT: - return (double)_data._int; - case FLOAT: - return _data._float; - case STRING: - return operator String().to_float(); - default: { - return 0; - } - } + return _to_float(); } Variant::operator StringName() const { diff --git a/core/variant/variant.h b/core/variant/variant.h index 736e03264b29..1675a2fcc1a4 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -343,6 +343,44 @@ class Variant { void _variant_call_error(const String &p_method, Callable::CallError &error); + template + T _to_int() const { + switch (get_type()) { + case NIL: + return 0; + case BOOL: + return _data._bool ? 1 : 0; + case INT: + return T(_data._int); + case FLOAT: + return T(_data._float); + case STRING: + return reinterpret_cast(_data._mem)->to_int(); + default: { + return 0; + } + } + } + + template + T _to_float() const { + switch (type) { + case NIL: + return 0; + case BOOL: + return _data._bool ? 1 : 0; + case INT: + return T(_data._int); + case FLOAT: + return T(_data._float); + case STRING: + return reinterpret_cast(_data._mem)->to_float(); + default: { + return 0; + } + } + } + // Avoid accidental conversion. If you reached this point, it's because you most likely forgot to dereference // a Variant pointer (so add * like this: *variant_pointer).