2020-02-20 03:27:19 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* callable.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CALLABLE_H
|
|
|
|
#define CALLABLE_H
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/object_id.h"
|
|
|
|
#include "core/string/string_name.h"
|
|
|
|
#include "core/templates/list.h"
|
2020-02-20 03:27:19 +08:00
|
|
|
|
|
|
|
class Object;
|
|
|
|
class Variant;
|
|
|
|
class CallableCustom;
|
|
|
|
|
2020-02-21 02:32:36 +08:00
|
|
|
// This is an abstraction of things that can be called.
|
|
|
|
// It is used for signals and other cases where efficient calling of functions
|
|
|
|
// is required. It is designed for the standard case (object and method)
|
2020-02-20 03:27:19 +08:00
|
|
|
// but can be optimized or customized.
|
|
|
|
|
2021-05-12 00:35:03 +08:00
|
|
|
// Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
|
2022-07-29 04:56:41 +08:00
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
class Callable {
|
2021-05-12 00:35:03 +08:00
|
|
|
alignas(8) StringName method;
|
2020-02-20 03:27:19 +08:00
|
|
|
union {
|
2020-05-12 23:01:17 +08:00
|
|
|
uint64_t object = 0;
|
2020-02-20 03:27:19 +08:00
|
|
|
CallableCustom *custom;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct CallError {
|
|
|
|
enum Error {
|
|
|
|
CALL_OK,
|
|
|
|
CALL_ERROR_INVALID_METHOD,
|
|
|
|
CALL_ERROR_INVALID_ARGUMENT, // expected is variant type
|
|
|
|
CALL_ERROR_TOO_MANY_ARGUMENTS, // expected is number of arguments
|
|
|
|
CALL_ERROR_TOO_FEW_ARGUMENTS, // expected is number of arguments
|
|
|
|
CALL_ERROR_INSTANCE_IS_NULL,
|
2022-06-28 04:10:04 +08:00
|
|
|
CALL_ERROR_METHOD_NOT_CONST,
|
2020-02-20 03:27:19 +08:00
|
|
|
};
|
2020-11-24 00:38:46 +08:00
|
|
|
Error error = Error::CALL_OK;
|
|
|
|
int argument = 0;
|
|
|
|
int expected = 0;
|
2020-02-20 03:27:19 +08:00
|
|
|
};
|
|
|
|
|
2023-07-11 22:18:10 +08:00
|
|
|
template <typename... VarArgs>
|
|
|
|
Variant call(VarArgs... p_args) const;
|
2022-07-29 04:56:41 +08:00
|
|
|
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;
|
2022-09-21 04:32:46 +08:00
|
|
|
Variant callv(const Array &p_arguments) const;
|
2020-02-20 03:27:19 +08:00
|
|
|
|
2022-10-22 05:40:06 +08:00
|
|
|
template <typename... VarArgs>
|
|
|
|
void call_deferred(VarArgs... p_args) const {
|
|
|
|
Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +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 call_deferredp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
|
|
|
|
}
|
|
|
|
|
2022-07-29 04:56:41 +08:00
|
|
|
Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;
|
2021-05-04 05:00:44 +08:00
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
_FORCE_INLINE_ bool is_null() const {
|
|
|
|
return method == StringName() && object == 0;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ bool is_custom() const {
|
2020-05-14 16:15:48 +08:00
|
|
|
return method == StringName() && custom != nullptr;
|
2020-02-20 03:27:19 +08:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ bool is_standard() const {
|
|
|
|
return method != StringName();
|
|
|
|
}
|
2021-06-30 22:36:46 +08:00
|
|
|
bool is_valid() const;
|
2020-02-20 03:27:19 +08:00
|
|
|
|
2022-07-29 04:56:41 +08:00
|
|
|
template <typename... VarArgs>
|
2024-01-31 14:38:11 +08:00
|
|
|
Callable bind(VarArgs... p_args) const;
|
2023-01-06 23:35:42 +08:00
|
|
|
Callable bindv(const Array &p_arguments);
|
2022-07-29 04:56:41 +08:00
|
|
|
|
|
|
|
Callable bindp(const Variant **p_arguments, int p_argcount) const;
|
2020-10-10 05:41:53 +08:00
|
|
|
Callable unbind(int p_argcount) const;
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
Object *get_object() const;
|
|
|
|
ObjectID get_object_id() const;
|
|
|
|
StringName get_method() const;
|
2020-03-15 02:20:17 +08:00
|
|
|
CallableCustom *get_custom() const;
|
2024-01-28 22:16:09 +08:00
|
|
|
int get_argument_count(bool *r_is_valid = nullptr) const;
|
2023-01-06 22:37:53 +08:00
|
|
|
int get_bound_arguments_count() const;
|
2023-01-10 20:08:10 +08:00
|
|
|
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;
|
2020-02-20 03:27:19 +08:00
|
|
|
|
|
|
|
uint32_t hash() const;
|
|
|
|
|
2020-10-10 05:41:53 +08:00
|
|
|
const Callable *get_base_comparator() const; //used for bind/unbind to do less precise comparisons (ignoring binds) in signal connect/disconnect
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
bool operator==(const Callable &p_callable) const;
|
|
|
|
bool operator!=(const Callable &p_callable) const;
|
|
|
|
bool operator<(const Callable &p_callable) const;
|
|
|
|
|
|
|
|
void operator=(const Callable &p_callable);
|
|
|
|
|
|
|
|
operator String() const;
|
|
|
|
|
2024-02-28 17:53:34 +08:00
|
|
|
static Callable create(const Variant &p_variant, const StringName &p_method);
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable(const Object *p_object, const StringName &p_method);
|
|
|
|
Callable(ObjectID p_object, const StringName &p_method);
|
|
|
|
Callable(CallableCustom *p_custom);
|
|
|
|
Callable(const Callable &p_callable);
|
2020-05-12 23:01:17 +08:00
|
|
|
Callable() {}
|
2020-02-20 03:27:19 +08:00
|
|
|
~Callable();
|
|
|
|
};
|
|
|
|
|
|
|
|
class CallableCustom {
|
|
|
|
friend class Callable;
|
|
|
|
SafeRefCount ref_count;
|
2020-05-12 23:01:17 +08:00
|
|
|
bool referenced = false;
|
2020-02-20 03:27:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef bool (*CompareEqualFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
|
|
|
|
typedef bool (*CompareLessFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
|
|
|
|
|
|
|
|
//for every type that inherits, these must always be the same for this type
|
|
|
|
virtual uint32_t hash() const = 0;
|
|
|
|
virtual String get_as_text() const = 0;
|
|
|
|
virtual CompareEqualFunc get_compare_equal_func() const = 0;
|
|
|
|
virtual CompareLessFunc get_compare_less_func() const = 0;
|
2023-01-19 05:04:09 +08:00
|
|
|
virtual bool is_valid() const;
|
2022-02-07 02:02:53 +08:00
|
|
|
virtual StringName get_method() const;
|
2023-01-19 05:04:09 +08:00
|
|
|
virtual ObjectID get_object() const = 0;
|
2020-02-20 03:27:19 +08:00
|
|
|
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0;
|
2022-07-13 05:12:42 +08:00
|
|
|
virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const;
|
2020-10-10 05:41:53 +08:00
|
|
|
virtual const Callable *get_base_comparator() const;
|
2024-01-28 22:16:09 +08:00
|
|
|
virtual int get_argument_count(bool &r_is_valid) const;
|
2023-01-06 22:37:53 +08:00
|
|
|
virtual int get_bound_arguments_count() const;
|
2023-01-10 20:08:10 +08:00
|
|
|
virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const;
|
2020-02-20 03:27:19 +08:00
|
|
|
|
|
|
|
CallableCustom();
|
|
|
|
virtual ~CallableCustom() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is just a proxy object to object signals, its only
|
|
|
|
// allocated on demand by/for scripting languages so it can
|
|
|
|
// be put inside a Variant, but it is not
|
|
|
|
// used by the engine itself.
|
|
|
|
|
2021-05-12 00:35:03 +08:00
|
|
|
// Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
|
2020-02-20 03:27:19 +08:00
|
|
|
class Signal {
|
2021-05-12 00:35:03 +08:00
|
|
|
alignas(8) StringName name;
|
2020-02-20 03:27:19 +08:00
|
|
|
ObjectID object;
|
|
|
|
|
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ bool is_null() const {
|
|
|
|
return object.is_null() && name == StringName();
|
|
|
|
}
|
|
|
|
Object *get_object() const;
|
|
|
|
ObjectID get_object_id() const;
|
|
|
|
StringName get_name() const;
|
|
|
|
|
|
|
|
bool operator==(const Signal &p_signal) const;
|
|
|
|
bool operator!=(const Signal &p_signal) const;
|
|
|
|
bool operator<(const Signal &p_signal) const;
|
|
|
|
|
|
|
|
operator String() const;
|
|
|
|
|
|
|
|
Error emit(const Variant **p_arguments, int p_argcount) const;
|
2021-09-20 00:55:23 +08:00
|
|
|
Error connect(const Callable &p_callable, uint32_t p_flags = 0);
|
2020-02-20 03:27:19 +08:00
|
|
|
void disconnect(const Callable &p_callable);
|
|
|
|
bool is_connected(const Callable &p_callable) const;
|
|
|
|
|
|
|
|
Array get_connections() const;
|
|
|
|
Signal(const Object *p_object, const StringName &p_name);
|
|
|
|
Signal(ObjectID p_object, const StringName &p_name);
|
2020-05-12 23:01:17 +08:00
|
|
|
Signal() {}
|
2020-02-20 03:27:19 +08:00
|
|
|
};
|
|
|
|
|
2022-03-28 09:57:20 +08:00
|
|
|
struct CallableComparator {
|
|
|
|
const Callable &func;
|
|
|
|
|
|
|
|
bool operator()(const Variant &p_l, const Variant &p_r) const;
|
|
|
|
};
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
#endif // CALLABLE_H
|