Skip to content

Commit

Permalink
Remove Signal connect binds
Browse files Browse the repository at this point in the history
Remove the optional argument p_binds from `Object::connect` since it was deprecated by Callable.bind().
Changed all uses of it to Callable.bind()
  • Loading branch information
reduz committed Jul 29, 2022
1 parent 14d0212 commit d4433ae
Show file tree
Hide file tree
Showing 149 changed files with 645 additions and 708 deletions.
4 changes: 2 additions & 2 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@ void Thread::_start_func(void *ud) {
::Thread::set_name(func_name);

Callable::CallError ce;
t->target_callable.call(nullptr, 0, t->ret, ce);
t->target_callable.callp(nullptr, 0, t->ret, ce);
if (ce.error != Callable::CallError::CALL_OK) {
t->running.clear();
ERR_FAIL_MSG("Could not call function '" + func_name + "' to start thread " + t->get_id() + ": " + Variant::get_callable_error_text(t->target_callable, nullptr, 0, ce) + ".");
Expand Down Expand Up @@ -2428,7 +2428,7 @@ Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Arra
const Variant *args[2] = { &cmd, &data };
Variant retval;
Callable::CallError err;
capture.call(args, 2, retval, err);
capture.callp(args, 2, retval, err);
ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, FAILED, "Error calling 'capture' to callable: " + Variant::get_callable_error_text(capture, args, 2, err));
ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
r_captured = retval;
Expand Down
2 changes: 1 addition & 1 deletion core/object/message_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void MessageQueue::_call_function(const Callable &p_callable, const Variant *p_a

Callable::CallError ce;
Variant ret;
p_callable.call(argptrs, p_argcount, ret, ce);
p_callable.callp(argptrs, p_argcount, ret, ce);
if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, argptrs, p_argcount, ce) + ".");
}
Expand Down
28 changes: 3 additions & 25 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ Object::Connection::operator Variant() const {
d["signal"] = signal;
d["callable"] = callable;
d["flags"] = flags;
d["binds"] = binds;
return d;
}

Expand All @@ -189,9 +188,6 @@ Object::Connection::Connection(const Variant &p_variant) {
if (d.has("flags")) {
flags = d["flags"];
}
if (d.has("binds")) {
binds = d["binds"];
}
}

bool Object::_predelete() {
Expand Down Expand Up @@ -973,8 +969,6 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int

OBJ_DEBUG_LOCK

Vector<const Variant *> bind_mem;

Error err = OK;

for (int i = 0; i < ssize; i++) {
Expand All @@ -989,28 +983,13 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int
const Variant **args = p_args;
int argc = p_argcount;

if (c.binds.size()) {
//handle binds
bind_mem.resize(p_argcount + c.binds.size());

for (int j = 0; j < p_argcount; j++) {
bind_mem.write[j] = p_args[j];
}
for (int j = 0; j < c.binds.size(); j++) {
bind_mem.write[p_argcount + j] = &c.binds[j];
}

args = (const Variant **)bind_mem.ptr();
argc = bind_mem.size();
}

if (c.flags & CONNECT_DEFERRED) {
MessageQueue::get_singleton()->push_callablep(c.callable, args, argc, true);
} else {
Callable::CallError ce;
_emitting = true;
Variant ret;
c.callable.call(args, argc, ret, ce);
c.callable.callp(args, argc, ret, ce);
_emitting = false;

if (ce.error != Callable::CallError::CALL_OK) {
Expand Down Expand Up @@ -1196,7 +1175,7 @@ void Object::get_signals_connected_to_this(List<Connection> *p_connections) cons
}
}

Error Object::connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "': the provided callable is null.");

Object *target_object = p_callable.get_object();
Expand Down Expand Up @@ -1244,7 +1223,6 @@ Error Object::connect(const StringName &p_signal, const Callable &p_callable, co
conn.callable = target;
conn.signal = ::Signal(this, p_signal);
conn.flags = p_flags;
conn.binds = p_binds;
slot.conn = conn;
slot.cE = target_object->connections.push_back(conn);
if (p_flags & CONNECT_REFERENCE_COUNTED) {
Expand Down Expand Up @@ -1492,7 +1470,7 @@ void Object::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);

ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);

Expand Down
3 changes: 1 addition & 2 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ class Object {
Callable callable;

uint32_t flags = 0;
Vector<Variant> binds;
bool operator<(const Connection &p_conn) const;

operator Variant() const;
Expand Down Expand Up @@ -829,7 +828,7 @@ class Object {
int get_persistent_signal_connection_count() const;
void get_signals_connected_to_this(List<Connection> *p_connections) const;

Error connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds = Vector<Variant>(), uint32_t p_flags = 0);
Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
void disconnect(const StringName &p_signal, const Callable &p_callable);
bool is_connected(const StringName &p_signal, const Callable &p_callable) const;

Expand Down
4 changes: 2 additions & 2 deletions core/object/worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void WorkerThreadPool::_process_task(Task *p_task) {
p_task->template_userdata->callback_indexed(work_index);
} else {
arg = work_index;
p_task->callable.call((const Variant **)&argptr, 1, ret, ce);
p_task->callable.callp((const Variant **)&argptr, 1, ret, ce);
}

// This is the only way to ensure posting is done when all tasks are really complete.
Expand Down Expand Up @@ -123,7 +123,7 @@ void WorkerThreadPool::_process_task(Task *p_task) {
} else {
Callable::CallError ce;
Variant ret;
p_task->callable.call(nullptr, 0, ret, ce);
p_task->callable.callp(nullptr, 0, ret, ce);
}

p_task->completed = true;
Expand Down
10 changes: 5 additions & 5 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ Array Array::filter(const Callable &p_callable) const {

Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 1, result, ce);
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(Array(), "Error calling method from 'filter': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}
Expand All @@ -467,7 +467,7 @@ Array Array::map(const Callable &p_callable) const {

Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 1, result, ce);
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(Array(), "Error calling method from 'map': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}
Expand All @@ -493,7 +493,7 @@ Variant Array::reduce(const Callable &p_callable, const Variant &p_accum) const

Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 2, result, ce);
p_callable.callp(argptrs, 2, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(Variant(), "Error calling method from 'reduce': " + Variant::get_callable_error_text(p_callable, argptrs, 2, ce));
}
Expand All @@ -510,7 +510,7 @@ bool Array::any(const Callable &p_callable) const {

Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 1, result, ce);
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(false, "Error calling method from 'any': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}
Expand All @@ -532,7 +532,7 @@ bool Array::all(const Callable &p_callable) const {

Variant result;
Callable::CallError ce;
p_callable.call(argptrs, 1, result, ce);
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(false, "Error calling method from 'all': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}
Expand Down
12 changes: 6 additions & 6 deletions core/variant/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
#include "core/object/ref_counted.h"
#include "core/object/script_language.h"

void Callable::call_deferred(const Variant **p_arguments, int p_argcount) const {
void Callable::call_deferredp(const Variant **p_arguments, int p_argcount) const {
MessageQueue::get_singleton()->push_callablep(*this, p_arguments, p_argcount);
}

void Callable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const {
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;
Expand All @@ -63,7 +63,7 @@ void Callable::call(const Variant **p_arguments, int p_argcount, Variant &r_retu
}
}

Error Callable::rpc(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
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;
Expand All @@ -79,7 +79,7 @@ Error Callable::rpc(int p_id, const Variant **p_arguments, int p_argcount, CallE
}
}

Callable Callable::bind(const Variant **p_arguments, int p_argcount) const {
Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
Vector<Variant> args;
args.resize(p_argcount);
for (int i = 0; i < p_argcount; i++) {
Expand Down Expand Up @@ -390,7 +390,7 @@ Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
Object *object = get_object();
ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);

return object->connect(name, p_callable, varray(), p_flags);
return object->connect(name, p_callable, p_flags);
}

void Signal::disconnect(const Callable &p_callable) {
Expand Down Expand Up @@ -438,7 +438,7 @@ bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) cons
const Variant *args[2] = { &p_l, &p_r };
Callable::CallError err;
Variant res;
func.call(args, 2, res, err);
func.callp(args, 2, res, err);
ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
"Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
return res;
Expand Down
12 changes: 8 additions & 4 deletions core/variant/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CallableCustom;
// but can be optimized or customized.

// Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.

class Callable {
alignas(8) StringName method;
union {
Expand All @@ -68,10 +69,10 @@ class Callable {
int expected = 0;
};

void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
void call_deferred(const Variant **p_arguments, int p_argcount) const;
void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
void call_deferredp(const Variant **p_arguments, int p_argcount) const;

Error rpc(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;
Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;

_FORCE_INLINE_ bool is_null() const {
return method == StringName() && object == 0;
Expand All @@ -84,7 +85,10 @@ class Callable {
}
bool is_valid() const;

Callable bind(const Variant **p_arguments, int p_argcount) const;
template <typename... VarArgs>
Callable bind(VarArgs... p_args);

Callable bindp(const Variant **p_arguments, int p_argcount) const;
Callable unbind(int p_argcount) const;

Object *get_object() const;
Expand Down
29 changes: 2 additions & 27 deletions core/variant/callable_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Varia
args[i + p_argcount] = (const Variant *)&binds[i];
}

callable.call(args, p_argcount + binds.size(), r_return_value, r_call_error);
callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
}

CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
Expand Down Expand Up @@ -171,7 +171,7 @@ void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Var
r_call_error.expected = argcount;
return;
}
callable.call(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
}

CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
Expand All @@ -181,28 +181,3 @@ CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_arg

CallableCustomUnbind::~CallableCustomUnbind() {
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1) {
const Variant *args[1] = { &p_arg1 };
return p_callable.bind(args, 1);
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2) {
const Variant *args[2] = { &p_arg1, &p_arg2 };
return p_callable.bind(args, 2);
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
const Variant *args[3] = { &p_arg1, &p_arg2, &p_arg3 };
return p_callable.bind(args, 3);
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
const Variant *args[4] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4 };
return p_callable.bind(args, 4);
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
const Variant *args[5] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5 };
return p_callable.bind(args, 5);
}
6 changes: 0 additions & 6 deletions core/variant/callable_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,4 @@ class CallableCustomUnbind : public CallableCustom {
virtual ~CallableCustomUnbind();
};

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1);
Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2);
Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3);
Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4);
Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5);

#endif // CALLABLE_BIND_H
10 changes: 10 additions & 0 deletions core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,14 @@ const Variant::ObjData &Variant::_get_obj() const {

String vformat(const String &p_text, const Variant &p1 = Variant(), const Variant &p2 = Variant(), const Variant &p3 = Variant(), const Variant &p4 = Variant(), const Variant &p5 = Variant());

template <typename... VarArgs>
Callable Callable::bind(VarArgs... p_args) {
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
const Variant *argptrs[sizeof...(p_args) + 1];
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
argptrs[i] = &args[i];
}
return bindp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
}

#endif // VARIANT_H
10 changes: 5 additions & 5 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,17 @@ struct _VariantCall {

static void func_Callable_call(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v);
callable->call(p_args, p_argcount, r_ret, r_error);
callable->callp(p_args, p_argcount, r_ret, r_error);
}

static void func_Callable_call_deferred(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v);
callable->call_deferred(p_args, p_argcount);
callable->call_deferredp(p_args, p_argcount);
}

static void func_Callable_rpc(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v);
callable->rpc(0, p_args, p_argcount, r_error);
callable->rpcp(0, p_args, p_argcount, r_error);
}

static void func_Callable_rpc_id(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Expand All @@ -920,13 +920,13 @@ struct _VariantCall {
r_error.expected = Variant::INT;
} else {
Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v);
callable->rpc(*p_args[0], &p_args[1], p_argcount - 1, r_error);
callable->rpcp(*p_args[0], &p_args[1], p_argcount - 1, r_error);
}
}

static void func_Callable_bind(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v);
r_ret = callable->bind(p_args, p_argcount);
r_ret = callable->bindp(p_args, p_argcount);
}

static void func_Signal_emit(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Expand Down
6 changes: 2 additions & 4 deletions doc/classes/Object.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@
<return type="int" enum="Error" />
<argument index="0" name="signal" type="StringName" />
<argument index="1" name="callable" type="Callable" />
<argument index="2" name="binds" type="Array" default="[]" />
<argument index="3" name="flags" type="int" default="0" />
<argument index="2" name="flags" type="int" default="0" />
<description>
Connects a [code]signal[/code] to a [code]callable[/code]. Pass optional [code]binds[/code] to the call as an [Array] of parameters. These parameters will be passed to the [Callable]'s method after any parameter used in the call to [method emit_signal]. Use [code]flags[/code] to set deferred or one-shot connections. See [enum ConnectFlags] constants.
[b]Note:[/b] This method is the legacy implementation for connecting signals. The recommended modern approach is to use [method Signal.connect] and to use [method Callable.bind] to add and validate parameter binds. Both syntaxes are shown below.
Connects a [code]signal[/code] to a [code]callable[/code]. Use [code]flags[/code] to set deferred or one-shot connections. See [enum ConnectFlags] constants.
A signal can only be connected once to a [Callable]. It will print an error if already connected, unless the signal was connected with [constant CONNECT_REFERENCE_COUNTED]. To avoid this, first, use [method is_connected] to check for existing connections.
If the callable's target is destroyed in the game's lifecycle, the connection will be lost.
[b]Examples with recommended syntax:[/b]
Expand Down
Loading

0 comments on commit d4433ae

Please sign in to comment.