Skip to content

Commit

Permalink
Add ability to bind an unbind arguments to Callable.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed Oct 9, 2020
1 parent 7c4d1e9 commit 351a122
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 10 deletions.
29 changes: 29 additions & 0 deletions core/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "callable.h"

#include "callable_bind.h"
#include "core/script_language.h"
#include "message_queue.h"
#include "object.h"
Expand All @@ -53,6 +54,18 @@ void Callable::call(const Variant **p_arguments, int p_argcount, Variant &r_retu
}
}

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)));
}

Object *Callable::get_object() const {
if (is_null()) {
return nullptr;
Expand Down Expand Up @@ -85,6 +98,18 @@ CallableCustom *Callable::get_custom() const {
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();
Expand Down Expand Up @@ -258,6 +283,10 @@ Callable::~Callable() {
}
}

const Callable *CallableCustom::get_base_comparator() const {
return nullptr;
}

CallableCustom::CallableCustom() {
ref_count.init();
}
Expand Down
6 changes: 6 additions & 0 deletions core/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,18 @@ class Callable {
return method != StringName();
}

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

Object *get_object() const;
ObjectID get_object_id() const;
StringName get_method() const;
CallableCustom *get_custom() 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

bool operator==(const Callable &p_callable) const;
bool operator!=(const Callable &p_callable) const;
bool operator<(const Callable &p_callable) const;
Expand Down Expand Up @@ -119,6 +124,7 @@ class CallableCustom {
virtual CompareLessFunc get_compare_less_func() const = 0;
virtual ObjectID get_object() const = 0; //must always be able to provide an object
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0;
virtual const Callable *get_base_comparator() const;

CallableCustom();
virtual ~CallableCustom() {}
Expand Down
193 changes: 193 additions & 0 deletions core/callable_bind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*************************************************************************/
/* callable_bind.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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_bind.h"

//////////////////////////////////

uint32_t CallableCustomBind::hash() const {
return callable.hash();
}
String CallableCustomBind::get_as_text() const {
return callable.operator String();
}

bool CallableCustomBind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
const CallableCustomBind *a = (const CallableCustomBind *)p_a;
const CallableCustomBind *b = (const CallableCustomBind *)p_b;

if (!(a->callable != b->callable)) {
return false;
}

if (a->binds.size() != b->binds.size()) {
return false;
}

return true;
}

bool CallableCustomBind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
const CallableCustomBind *a = (const CallableCustomBind *)p_a;
const CallableCustomBind *b = (const CallableCustomBind *)p_b;

if (a->callable < b->callable) {
return true;
} else if (b->callable < a->callable) {
return false;
}

return a->binds.size() < b->binds.size();
}

CallableCustom::CompareEqualFunc CallableCustomBind::get_compare_equal_func() const {
return _equal_func;
}
CallableCustom::CompareLessFunc CallableCustomBind::get_compare_less_func() const {
return _less_func;
}
ObjectID CallableCustomBind::get_object() const {
return callable.get_object_id();
}
const Callable *CallableCustomBind::get_base_comparator() const {
return &callable;
}

void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * (binds.size() + p_argcount));
for (int i = 0; i < p_argcount; i++) {
args[i] = (const Variant *)p_arguments[i];
}
for (int i = 0; i < binds.size(); i++) {
args[i + p_argcount] = (const Variant *)&binds[i];
}

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

CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
callable = p_callable;
binds = p_binds;
}

CallableCustomBind::~CallableCustomBind() {
}

//////////////////////////////////

uint32_t CallableCustomUnbind::hash() const {
return callable.hash();
}
String CallableCustomUnbind::get_as_text() const {
return callable.operator String();
}

bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
const CallableCustomUnbind *a = (const CallableCustomUnbind *)p_a;
const CallableCustomUnbind *b = (const CallableCustomUnbind *)p_b;

if (!(a->callable != b->callable)) {
return false;
}

if (a->argcount != b->argcount) {
return false;
}

return true;
}

bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
const CallableCustomUnbind *a = (const CallableCustomUnbind *)p_a;
const CallableCustomUnbind *b = (const CallableCustomUnbind *)p_b;

if (a->callable < b->callable) {
return true;
} else if (b->callable < a->callable) {
return false;
}

return a->argcount < b->argcount;
}

CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
return _equal_func;
}
CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
return _less_func;
}
ObjectID CallableCustomUnbind::get_object() const {
return callable.get_object_id();
}
const Callable *CallableCustomUnbind::get_base_comparator() const {
return &callable;
}

void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
if (argcount > p_argcount) {
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_call_error.argument = 0;
r_call_error.expected = argcount;
return;
}
callable.call(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
}

CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
callable = p_callable;
argcount = p_argcount;
}

CallableCustomUnbind::~CallableCustomUnbind() {
}

Callable callable_bind(const Callable &p_callable, const Variant &p_arg1) {
return p_callable.bind((const Variant **)&p_arg1, 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);
}
85 changes: 85 additions & 0 deletions core/callable_bind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*************************************************************************/
/* callable_bind.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/

#ifndef CALLABLE_BIND_H
#define CALLABLE_BIND_H

#include "core/callable.h"
#include "core/variant.h"

class CallableCustomBind : public CallableCustom {
Callable callable;
Vector<Variant> binds;

static bool _equal_func(const CallableCustom *p_a, const CallableCustom *p_b);
static bool _less_func(const CallableCustom *p_a, const CallableCustom *p_b);

public:
//for every type that inherits, these must always be the same for this type
virtual uint32_t hash() const;
virtual String get_as_text() const;
virtual CompareEqualFunc get_compare_equal_func() const;
virtual CompareLessFunc get_compare_less_func() const;
virtual ObjectID get_object() const; //must always be able to provide an object
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const;
virtual const Callable *get_base_comparator() const;

CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds);
virtual ~CallableCustomBind();
};

class CallableCustomUnbind : public CallableCustom {
Callable callable;
int argcount;

static bool _equal_func(const CallableCustom *p_a, const CallableCustom *p_b);
static bool _less_func(const CallableCustom *p_a, const CallableCustom *p_b);

public:
//for every type that inherits, these must always be the same for this type
virtual uint32_t hash() const;
virtual String get_as_text() const;
virtual CompareEqualFunc get_compare_equal_func() const;
virtual CompareLessFunc get_compare_less_func() const;
virtual ObjectID get_object() const; //must always be able to provide an object
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const;
virtual const Callable *get_base_comparator() const;

CallableCustomUnbind(const Callable &p_callable, int p_argcount);
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
2 changes: 2 additions & 0 deletions core/core_string_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ CoreStringNames::CoreStringNames() :
a8(StaticCString::create("a8")),
call(StaticCString::create("call")),
call_deferred(StaticCString::create("call_deferred")),
bind(StaticCString::create("bind")),
unbind(StaticCString::create("unbind")),
emit(StaticCString::create("emit")),
notification(StaticCString::create("notification")) {
}
2 changes: 2 additions & 0 deletions core/core_string_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class CoreStringNames {

StringName call;
StringName call_deferred;
StringName bind;
StringName unbind;
StringName emit;
StringName notification;
};
Expand Down
Loading

0 comments on commit 351a122

Please sign in to comment.