| 1 | /**************************************************************************/ |
| 2 | /* variant_callable.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "variant_callable.h" |
| 32 | |
| 33 | #include "core/templates/hashfuncs.h" |
| 34 | |
| 35 | bool VariantCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { |
| 36 | return p_a->hash() == p_b->hash(); |
| 37 | } |
| 38 | |
| 39 | bool VariantCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) { |
| 40 | return p_a->hash() < p_b->hash(); |
| 41 | } |
| 42 | |
| 43 | uint32_t VariantCallable::hash() const { |
| 44 | return h; |
| 45 | } |
| 46 | |
| 47 | String VariantCallable::get_as_text() const { |
| 48 | return vformat("%s::%s", Variant::get_type_name(variant.get_type()), method); |
| 49 | } |
| 50 | |
| 51 | CallableCustom::CompareEqualFunc VariantCallable::get_compare_equal_func() const { |
| 52 | return compare_equal; |
| 53 | } |
| 54 | |
| 55 | CallableCustom::CompareLessFunc VariantCallable::get_compare_less_func() const { |
| 56 | return compare_less; |
| 57 | } |
| 58 | |
| 59 | bool VariantCallable::is_valid() const { |
| 60 | return Variant::has_builtin_method(variant.get_type(), method); |
| 61 | } |
| 62 | |
| 63 | StringName VariantCallable::get_method() const { |
| 64 | return method; |
| 65 | } |
| 66 | |
| 67 | ObjectID VariantCallable::get_object() const { |
| 68 | return ObjectID(); |
| 69 | } |
| 70 | |
| 71 | int VariantCallable::get_argument_count(bool &r_is_valid) const { |
| 72 | if (!Variant::has_builtin_method(variant.get_type(), method)) { |
| 73 | r_is_valid = false; |
| 74 | return 0; |
| 75 | } |
| 76 | r_is_valid = true; |
| 77 | return Variant::get_builtin_method_argument_count(variant.get_type(), method); |
| 78 | } |
| 79 | |
| 80 | void VariantCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { |
| 81 | Variant v = variant; |
| 82 | v.callp(method, p_arguments, p_argcount, r_return_value, r_call_error); |
| 83 | } |
| 84 | |
| 85 | VariantCallable::VariantCallable(const Variant &p_variant, const StringName &p_method) { |
| 86 | variant = p_variant; |
| 87 | method = p_method; |
| 88 | h = variant.hash(); |
| 89 | h = hash_murmur3_one_64(Variant::get_builtin_method_hash(variant.get_type(), method), h); |
| 90 | } |