From 13a2d03ddef05e0eb63885ba8a49875a4c7f0e96 Mon Sep 17 00:00:00 2001 From: George Marques Date: Wed, 6 Oct 2021 22:05:13 -0300 Subject: Add Variant `in` operator for any String/StringName operands Allow using String or StringName types as operand in any position of the `in` operator, which is more convenient in scripting when interacting with data in the engine (such as a Node name). --- core/variant/variant_op.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'core/variant/variant_op.h') diff --git a/core/variant/variant_op.h b/core/variant/variant_op.h index 3c9f849a4f..353524469a 100644 --- a/core/variant/variant_op.h +++ b/core/variant/variant_op.h @@ -1213,22 +1213,44 @@ public: //// +template class OperatorEvaluatorInStringFind { public: static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { - const String &str_a = *VariantGetInternalPtr::get_ptr(&p_left); + const Left &str_a = *VariantGetInternalPtr::get_ptr(&p_left); const String &str_b = *VariantGetInternalPtr::get_ptr(&p_right); *r_ret = str_b.find(str_a) != -1; r_valid = true; } static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { - const String &str_a = *VariantGetInternalPtr::get_ptr(left); + const Left &str_a = *VariantGetInternalPtr::get_ptr(left); const String &str_b = *VariantGetInternalPtr::get_ptr(right); *VariantGetInternalPtr::get_ptr(r_ret) = str_b.find(str_a) != -1; } static void ptr_evaluate(const void *left, const void *right, void *r_ret) { - PtrToArg::encode(PtrToArg::convert(right).find(PtrToArg::convert(left)) != -1, r_ret); + PtrToArg::encode(PtrToArg::convert(right).find(PtrToArg::convert(left)) != -1, r_ret); + } + static Variant::Type get_return_type() { return Variant::BOOL; } +}; + +template +class OperatorEvaluatorInStringNameFind { +public: + static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { + const Left &str_a = *VariantGetInternalPtr::get_ptr(&p_left); + const String str_b = VariantGetInternalPtr::get_ptr(&p_right)->operator String(); + + *r_ret = str_b.find(str_a) != -1; + r_valid = true; + } + static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + const Left &str_a = *VariantGetInternalPtr::get_ptr(left); + const String str_b = VariantGetInternalPtr::get_ptr(right)->operator String(); + *VariantGetInternalPtr::get_ptr(r_ret) = str_b.find(str_a) != -1; + } + static void ptr_evaluate(const void *left, const void *right, void *r_ret) { + PtrToArg::encode(PtrToArg::convert(right).operator String().find(PtrToArg::convert(left)) != -1, r_ret); } static Variant::Type get_return_type() { return Variant::BOOL; } }; -- cgit v1.2.3