2020-02-19 19:27:19 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* callable.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-02-19 19:27:19 +00:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2020-10-09 21:41:53 +00:00
|
|
|
#include "callable_bind.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/object/message_queue.h"
|
|
|
|
#include "core/object/object.h"
|
|
|
|
#include "core/object/reference.h"
|
|
|
|
#include "core/object/script_language.h"
|
2020-02-19 19:27:19 +00:00
|
|
|
|
|
|
|
void Callable::call_deferred(const Variant **p_arguments, int p_argcount) const {
|
|
|
|
MessageQueue::get_singleton()->push_callable(*this, p_arguments, p_argcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Callable::call(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));
|
|
|
|
r_return_value = obj->call(method, p_arguments, p_argcount, r_call_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 21:00:44 +00:00
|
|
|
void Callable::rpc(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;
|
|
|
|
} else if (!is_custom()) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
} else {
|
|
|
|
custom->rpc(p_id, p_arguments, p_argcount, r_call_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:41:53 +00:00
|
|
|
Callable Callable::bind(const Variant **p_arguments, int p_argcount) const {
|
|
|
|
Vector<Variant> 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::unbind(int p_argcount) const {
|
|
|
|
return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
|
|
|
|
}
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
StringName Callable::get_method() const {
|
2020-02-28 13:32:36 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(is_custom(), StringName(),
|
|
|
|
vformat("Can't get method on CallableCustom \"%s\".", operator String()));
|
2020-02-19 19:27:19 +00:00
|
|
|
return method;
|
|
|
|
}
|
2020-02-28 13:32:36 +00:00
|
|
|
|
2020-03-14 18:20:17 +00:00
|
|
|
CallableCustom *Callable::get_custom() const {
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
|
2020-03-14 18:20:17 +00:00
|
|
|
vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
|
|
|
|
return custom;
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:41:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
uint32_t Callable::hash() const {
|
|
|
|
if (is_custom()) {
|
|
|
|
return custom->hash();
|
|
|
|
} else {
|
|
|
|
uint32_t hash = method.hash();
|
|
|
|
return hash_djb2_one_64(object, 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) {
|
2021-03-12 13:35:16 +00:00
|
|
|
return true; //same pointer, don't even compare
|
2020-02-19 19:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
bool Callable::operator!=(const Callable &p_callable) const {
|
|
|
|
return !(*this == p_callable);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
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) {
|
2021-03-12 13:35:16 +00:00
|
|
|
return false; //same pointer, don't even compare
|
2020-02-19 19:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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<Script> script = base->get_script();
|
|
|
|
if (script.is_valid() && script->get_path().is_resource_file()) {
|
|
|
|
class_name += "(" + script->get_path().get_file() + ")";
|
|
|
|
}
|
|
|
|
return class_name + "::" + String(method);
|
|
|
|
} else {
|
|
|
|
return "null::" + String(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::Callable(const Object *p_object, const StringName &p_method) {
|
|
|
|
if (p_method == StringName()) {
|
|
|
|
object = 0;
|
|
|
|
ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
|
|
|
|
}
|
|
|
|
if (p_object == nullptr) {
|
|
|
|
object = 0;
|
|
|
|
ERR_FAIL_MSG("Object argument to Callable constructor must be non-null");
|
|
|
|
}
|
|
|
|
|
|
|
|
object = p_object->get_instance_id();
|
|
|
|
method = p_method;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::Callable(ObjectID p_object, const StringName &p_method) {
|
|
|
|
if (p_method == StringName()) {
|
|
|
|
object = 0;
|
|
|
|
ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
|
|
|
|
}
|
|
|
|
|
|
|
|
object = p_object;
|
|
|
|
method = p_method;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Callable::Callable(CallableCustom *p_custom) {
|
|
|
|
if (p_custom->referenced) {
|
|
|
|
object = 0;
|
|
|
|
ERR_FAIL_MSG("Callable custom is already referenced");
|
|
|
|
}
|
|
|
|
p_custom->referenced = true;
|
|
|
|
object = 0; //ensure object is all zero, since pointer may be 32 bits
|
|
|
|
custom = p_custom;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Callable::Callable(const Callable &p_callable) {
|
|
|
|
if (p_callable.is_custom()) {
|
|
|
|
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::~Callable() {
|
|
|
|
if (is_custom()) {
|
|
|
|
if (custom->ref_count.unref()) {
|
|
|
|
memdelete(custom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 21:00:44 +00:00
|
|
|
void CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
|
|
|
|
r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:41:53 +00:00
|
|
|
const Callable *CallableCustom::get_base_comparator() const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
CallableCustom::CallableCustom() {
|
|
|
|
ref_count.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
|
|
|
|
Object *Signal::get_object() const {
|
|
|
|
return ObjectDB::get_instance(object);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
ObjectID Signal::get_object_id() const {
|
|
|
|
return object;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
StringName Signal::get_name() const {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator==(const Signal &p_signal) const {
|
|
|
|
return object == p_signal.object && name == p_signal.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator!=(const Signal &p_signal) const {
|
|
|
|
return object != p_signal.object || name != p_signal.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator<(const Signal &p_signal) const {
|
|
|
|
if (object == p_signal.object) {
|
|
|
|
return name < p_signal.name;
|
|
|
|
} else {
|
|
|
|
return object < p_signal.object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Signal::operator String() const {
|
|
|
|
Object *base = get_object();
|
|
|
|
if (base) {
|
|
|
|
String class_name = base->get_class();
|
|
|
|
Ref<Script> script = base->get_script();
|
|
|
|
if (script.is_valid() && script->get_path().is_resource_file()) {
|
|
|
|
class_name += "(" + script->get_path().get_file() + ")";
|
|
|
|
}
|
|
|
|
return class_name + "::[signal]" + String(name);
|
|
|
|
} else {
|
|
|
|
return "null::[signal]" + String(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
|
|
|
|
Object *obj = ObjectDB::get_instance(object);
|
|
|
|
if (!obj) {
|
|
|
|
return ERR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj->emit_signal(name, p_arguments, p_argcount);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Error Signal::connect(const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
|
|
|
|
Object *object = get_object();
|
|
|
|
ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
return object->connect(name, p_callable, p_binds, p_flags);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
void Signal::disconnect(const Callable &p_callable) {
|
|
|
|
Object *object = get_object();
|
|
|
|
ERR_FAIL_COND(!object);
|
|
|
|
object->disconnect(name, p_callable);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
bool Signal::is_connected(const Callable &p_callable) const {
|
|
|
|
Object *object = get_object();
|
|
|
|
ERR_FAIL_COND_V(!object, false);
|
|
|
|
|
|
|
|
return object->is_connected(name, p_callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Array Signal::get_connections() const {
|
|
|
|
Object *object = get_object();
|
|
|
|
if (!object) {
|
|
|
|
return Array();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Object::Connection> connections;
|
|
|
|
object->get_signal_connection_list(name, &connections);
|
|
|
|
|
|
|
|
Array arr;
|
|
|
|
for (List<Object::Connection>::Element *E = connections.front(); E; E = E->next()) {
|
|
|
|
arr.push_back(E->get());
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Signal::Signal(const Object *p_object, const StringName &p_name) {
|
|
|
|
ERR_FAIL_COND_MSG(p_object == nullptr, "Object argument to Signal constructor must be non-null");
|
|
|
|
|
|
|
|
object = p_object->get_instance_id();
|
|
|
|
name = p_name;
|
|
|
|
}
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Signal::Signal(ObjectID p_object, const StringName &p_name) {
|
|
|
|
object = p_object;
|
|
|
|
name = p_name;
|
|
|
|
}
|