/**************************************************************************/ /* callable.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "callable.h" #include "callable_bind.h" #include "core/object/message_queue.h" #include "core/object/object.h" #include "core/object/ref_counted.h" #include "core/object/script_language.h" void Callable::call_deferredp(const Variant **p_arguments, int p_argcount) const { MessageQueue::get_singleton()->push_callablep(*this, p_arguments, p_argcount); } void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const { if (is_null()) { r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL; r_call_error.argument = 0; r_call_error.expected = 0; r_return_value = Variant(); } else if (is_custom()) { custom->call(p_arguments, p_argcount, r_return_value, r_call_error); } else { Object *obj = ObjectDB::get_instance(ObjectID(object)); #ifdef DEBUG_ENABLED if (!obj) { r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL; r_call_error.argument = 0; r_call_error.expected = 0; r_return_value = Variant(); return; } #endif r_return_value = obj->callp(method, p_arguments, p_argcount, r_call_error); } } Variant Callable::callv(const Array &p_arguments) const { int argcount = p_arguments.size(); const Variant **argptrs = nullptr; if (argcount) { argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount); for (int i = 0; i < argcount; i++) { argptrs[i] = &p_arguments[i]; } } CallError ce; Variant ret; callp(argptrs, argcount, ret, ce); return ret; } Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const { if (is_null()) { r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL; r_call_error.argument = 0; r_call_error.expected = 0; return ERR_UNCONFIGURED; } else if (!is_custom()) { r_call_error.error = CallError::CALL_ERROR_INVALID_METHOD; r_call_error.argument = 0; r_call_error.expected = 0; return ERR_UNCONFIGURED; } else { return custom->rpc(p_id, p_arguments, p_argcount, r_call_error); } } Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const { Vector args; args.resize(p_argcount); for (int i = 0; i < p_argcount; i++) { args.write[i] = *p_arguments[i]; } return Callable(memnew(CallableCustomBind(*this, args))); } Callable Callable::bindv(const Array &p_arguments) { if (p_arguments.is_empty()) { return *this; // No point in creating a new callable if nothing is bound. } Vector args; args.resize(p_arguments.size()); for (int i = 0; i < p_arguments.size(); i++) { args.write[i] = p_arguments[i]; } return Callable(memnew(CallableCustomBind(*this, args))); } Callable Callable::unbind(int p_argcount) const { ERR_FAIL_COND_V_MSG(p_argcount <= 0, Callable(*this), "Amount of unbind() arguments must be 1 or greater."); return Callable(memnew(CallableCustomUnbind(*this, p_argcount))); } bool Callable::is_valid() const { if (is_custom()) { return get_custom()->is_valid(); } else { return get_object() && get_object()->has_method(get_method()); } } Object *Callable::get_object() const { if (is_null()) { return nullptr; } else if (is_custom()) { return ObjectDB::get_instance(custom->get_object()); } else { return ObjectDB::get_instance(ObjectID(object)); } } ObjectID Callable::get_object_id() const { if (is_null()) { return ObjectID(); } else if (is_custom()) { return custom->get_object(); } else { return ObjectID(object); } } StringName Callable::get_method() const { if (is_custom()) { return get_custom()->get_method(); } return method; } int Callable::get_bound_arguments_count() const { if (!is_null() && is_custom()) { return custom->get_bound_arguments_count(); } else { return 0; } } void Callable::get_bound_arguments_ref(Vector &r_arguments, int &r_argcount) const { if (!is_null() && is_custom()) { custom->get_bound_arguments(r_arguments, r_argcount); } else { r_arguments.clear(); r_argcount = 0; } } Array Callable::get_bound_arguments() const { Vector arr; int ac; get_bound_arguments_ref(arr, ac); Array ret; ret.resize(arr.size()); for (int i = 0; i < arr.size(); i++) { ret[i] = arr[i]; } return ret; } CallableCustom *Callable::get_custom() const { ERR_FAIL_COND_V_MSG(!is_custom(), nullptr, vformat("Can't get custom on non-CallableCustom \"%s\".", operator String())); return custom; } const Callable *Callable::get_base_comparator() const { const Callable *comparator = nullptr; if (is_custom()) { comparator = custom->get_base_comparator(); } if (comparator) { return comparator; } else { return this; } } uint32_t Callable::hash() const { if (is_custom()) { return custom->hash(); } else { uint32_t hash = method.hash(); hash = hash_murmur3_one_64(object, hash); return hash_fmix32(hash); } } bool Callable::operator==(const Callable &p_callable) const { bool custom_a = is_custom(); bool custom_b = p_callable.is_custom(); if (custom_a == custom_b) { if (custom_a) { if (custom == p_callable.custom) { return true; //same pointer, don't even compare } CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func(); CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func(); if (eq_a == eq_b) { return eq_a(custom, p_callable.custom); } else { return false; } } else { return object == p_callable.object && method == p_callable.method; } } else { return false; } } bool Callable::operator!=(const Callable &p_callable) const { return !(*this == p_callable); } bool Callable::operator<(const Callable &p_callable) const { bool custom_a = is_custom(); bool custom_b = p_callable.is_custom(); if (custom_a == custom_b) { if (custom_a) { if (custom == p_callable.custom) { return false; //same pointer, don't even compare } CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func(); CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func(); if (less_a == less_b) { return less_a(custom, p_callable.custom); } else { return less_a < less_b; //it's something.. } } else { if (object == p_callable.object) { return method < p_callable.method; } else { return object < p_callable.object; } } } else { return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0); } } void Callable::operator=(const Callable &p_callable) { if (is_custom()) { if (p_callable.is_custom()) { if (custom == p_callable.custom) { return; } } if (custom->ref_count.unref()) { memdelete(custom); } } if (p_callable.is_custom()) { method = StringName(); if (!p_callable.custom->ref_count.ref()) { object = 0; } else { object = 0; custom = p_callable.custom; } } else { method = p_callable.method; object = p_callable.object; } } Callable::operator String() const { if (is_custom()) { return custom->get_as_text(); } else { if (is_null()) { return "null::null"; } Object *base = get_object(); if (base) { String class_name = base->get_class(); Ref