Skip to content

Commit

Permalink
Test rebind
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Mar 27, 2023
1 parent 2659fc1 commit 06aecb3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/variant/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ const Callable *Callable::get_base_comparator() const {
}
}

Callable Callable::rebind(const Object *p_object) const {
if (is_custom()) {
CallableCustom *new_custom = custom->rebind(p_object);
if (new_custom) {
return Callable(new_custom);
}
return Callable();
} else {
return Callable(p_object, method);
}
}

uint32_t Callable::hash() const {
if (is_custom()) {
return custom->hash();
Expand Down
4 changes: 4 additions & 0 deletions core/variant/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class Callable {
void get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const; // Internal engine use, the exposed one is below.
Array get_bound_arguments() const;

Callable rebind(const Object *p_object) const;

uint32_t hash() const;

const Callable *get_base_comparator() const; //used for bind/unbind to do less precise comparisons (ignoring binds) in signal connect/disconnect
Expand Down Expand Up @@ -153,6 +155,8 @@ class CallableCustom {
virtual int get_bound_arguments_count() const;
virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const;

virtual CallableCustom *rebind(const Object *p_object) const { return nullptr; };

CallableCustom();
virtual ~CallableCustom() {}
};
Expand Down
8 changes: 8 additions & 0 deletions core/variant/callable_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Varia
callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
}

CallableCustom *CallableCustomBind::rebind(const Object *p_object) const {
return memnew(CallableCustomBind(callable.rebind(p_object), binds));
}

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

CallableCustom *CallableCustomUnbind::rebind(const Object *p_object) const {
return memnew(CallableCustomUnbind(callable.rebind(p_object), argcount));
}

CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
callable = p_callable;
argcount = p_argcount;
Expand Down
4 changes: 4 additions & 0 deletions core/variant/callable_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class CallableCustomBind : public CallableCustom {
Callable get_callable() { return callable; }
Vector<Variant> get_binds() { return binds; }

CallableCustom *rebind(const Object *p_object) const override;

CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds);
virtual ~CallableCustomBind();
};
Expand Down Expand Up @@ -83,6 +85,8 @@ class CallableCustomUnbind : public CallableCustom {
Callable get_callable() { return callable; }
int get_unbinds() { return argcount; }

CallableCustom *rebind(const Object *p_object) const override;

CallableCustomUnbind(const Callable &p_callable, int p_argcount);
virtual ~CallableCustomUnbind();
};
Expand Down
6 changes: 6 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@ struct _VariantCall {
r_ret = callable->bindp(p_args, p_argcount);
}

static Callable func_Callable_rebind(Callable *p_callable, Object *p_object) {
return p_callable->rebind(p_object);
}

static void func_Signal_emit(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
Signal *signal = VariantGetInternalPtr<Signal>::get_ptr(v);
signal->emit(p_args, p_argcount);
Expand Down Expand Up @@ -2036,6 +2040,8 @@ static void _register_variant_builtin_methods() {
bind_method(Callable, hash, sarray(), varray());
bind_method(Callable, bindv, sarray("arguments"), varray());
bind_method(Callable, unbind, sarray("argcount"), varray());
// bind_method(Callable, rebind, sarray("object"), varray());
bind_functionnc(Callable, rebind, _VariantCall::func_Callable_rebind, sarray("object"), varray());

bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant);
bind_custom(Callable, call_deferred, _VariantCall::func_Callable_call_deferred, false, Variant);
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/Callable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@
Returns [code]true[/code] if the callable's object exists and has a valid method name assigned, or is a custom callable.
</description>
</method>
<method name="rebind">
<return type="Callable" />
<param index="0" name="object" type="Object" />
<description>
</description>
</method>
<method name="rpc" qualifiers="vararg const">
<return type="void" />
<description>
Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript_lambda_callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ void GDScriptLambdaSelfCallable::call(const Variant **p_arguments, int p_argcoun
}
}

CallableCustom *GDScriptLambdaSelfCallable::rebind(const Object *p_object) const {
return memnew(GDScriptLambdaSelfCallable(const_cast<Object *>(p_object), function, captures));
}

GDScriptLambdaSelfCallable::GDScriptLambdaSelfCallable(Ref<RefCounted> p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures) {
reference = p_self;
object = p_self.ptr();
Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_lambda_callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class GDScriptLambdaSelfCallable : public CallableCustom {
ObjectID get_object() const override;
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;

CallableCustom *rebind(const Object *p_object) const override;

GDScriptLambdaSelfCallable(Ref<RefCounted> p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures);
GDScriptLambdaSelfCallable(Object *p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures);
virtual ~GDScriptLambdaSelfCallable() = default;
Expand Down

0 comments on commit 06aecb3

Please sign in to comment.