diff options
Diffstat (limited to 'core/object')
-rw-r--r-- | core/object/method_bind.h | 121 | ||||
-rw-r--r-- | core/object/object.cpp | 10 | ||||
-rw-r--r-- | core/object/object.h | 2 | ||||
-rw-r--r-- | core/object/undo_redo.cpp | 18 | ||||
-rw-r--r-- | core/object/undo_redo.h | 4 |
5 files changed, 101 insertions, 54 deletions
diff --git a/core/object/method_bind.h b/core/object/method_bind.h index 1518c8d793..27098571e3 100644 --- a/core/object/method_bind.h +++ b/core/object/method_bind.h @@ -143,21 +143,20 @@ public: virtual ~MethodBind(); }; -template <class T> -class MethodBindVarArg : public MethodBind { -public: - typedef Variant (T::*NativeCall)(const Variant **, int, Callable::CallError &); - +// MethodBindVarArg base CRTP +template <class Derived, class T, class R, bool should_returns> +class MethodBindVarArgBase : public MethodBind { protected: - NativeCall call_method = nullptr; - MethodInfo arguments; + R(T::*method) + (const Variant **, int, Callable::CallError &); + MethodInfo method_info; public: virtual PropertyInfo _gen_argument_type_info(int p_arg) const { if (p_arg < 0) { - return arguments.return_val; - } else if (p_arg < arguments.arguments.size()) { - return arguments.arguments[p_arg]; + return _gen_return_type_info(); + } else if (p_arg < method_info.arguments.size()) { + return method_info.arguments[p_arg]; } else { return PropertyInfo(Variant::NIL, "arg_" + itos(p_arg), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT); } @@ -173,24 +172,31 @@ public: } #endif - virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - T *instance = static_cast<T *>(p_object); - return (instance->*call_method)(p_args, p_arg_count, r_error); + virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) { + ERR_FAIL(); // Can't call. } - void set_method_info(const MethodInfo &p_info, bool p_return_nil_is_variant) { - set_argument_count(p_info.arguments.size()); - Variant::Type *at = memnew_arr(Variant::Type, p_info.arguments.size() + 1); - at[0] = p_info.return_val.type; - if (p_info.arguments.size()) { + virtual bool is_const() const { return false; } + + virtual bool is_vararg() const { return true; } + + MethodBindVarArgBase( + R (T::*p_method)(const Variant **, int, Callable::CallError &), + const MethodInfo &p_method_info, + bool p_return_nil_is_variant) : + method(p_method), method_info(p_method_info) { + set_argument_count(method_info.arguments.size()); + Variant::Type *at = memnew_arr(Variant::Type, method_info.arguments.size() + 1); + at[0] = _gen_return_type_info().type; + if (method_info.arguments.size()) { #ifdef DEBUG_METHODS_ENABLED Vector<StringName> names; - names.resize(p_info.arguments.size()); + names.resize(method_info.arguments.size()); #endif - for (int i = 0; i < p_info.arguments.size(); i++) { - at[i + 1] = p_info.arguments[i].type; + for (int i = 0; i < method_info.arguments.size(); i++) { + at[i + 1] = method_info.arguments[i].type; #ifdef DEBUG_METHODS_ENABLED - names.write[i] = p_info.arguments[i].name; + names.write[i] = method_info.arguments[i].name; #endif } @@ -199,31 +205,76 @@ public: #endif } argument_types = at; - arguments = p_info; if (p_return_nil_is_variant) { - arguments.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + method_info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; } + + _set_returns(should_returns); } - virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) { - ERR_FAIL(); // Can't call. +private: + PropertyInfo _gen_return_type_info() const { + return reinterpret_cast<const Derived *>(this)->_gen_return_type_info_impl(); } +}; - void set_method(NativeCall p_method) { call_method = p_method; } - virtual bool is_const() const { return false; } +// variadic, no return +template <class T> +class MethodBindVarArgT : public MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false> { + friend class MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>; - virtual bool is_vararg() const { return true; } +public: + virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>::method)(p_args, p_arg_count, r_error); + return {}; + } - MethodBindVarArg() { - _set_returns(true); + MethodBindVarArgT( + void (T::*p_method)(const Variant **, int, Callable::CallError &), + const MethodInfo &p_method_info, + bool p_return_nil_is_variant) : + MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>(p_method, p_method_info, p_return_nil_is_variant) { + } + +private: + PropertyInfo _gen_return_type_info_impl() const { + return {}; } }; template <class T> -MethodBind *create_vararg_method_bind(Variant (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) { - MethodBindVarArg<T> *a = memnew((MethodBindVarArg<T>)); - a->set_method(p_method); - a->set_method_info(p_info, p_return_nil_is_variant); +MethodBind *create_vararg_method_bind(void (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) { + MethodBind *a = memnew((MethodBindVarArgT<T>)(p_method, p_info, p_return_nil_is_variant)); + a->set_instance_class(T::get_class_static()); + return a; +} + +// variadic, return +template <class T, class R> +class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true> { + friend class MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>; + +public: + virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + return (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error); + } + + MethodBindVarArgTR( + R (T::*p_method)(const Variant **, int, Callable::CallError &), + const MethodInfo &p_info, + bool p_return_nil_is_variant) : + MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>(p_method, p_info, p_return_nil_is_variant) { + } + +private: + PropertyInfo _gen_return_type_info_impl() const { + return GetTypeInfo<R>::get_class_info(); + } +}; + +template <class T, class R> +MethodBind *create_vararg_method_bind(R (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) { + MethodBind *a = memnew((MethodBindVarArgTR<T, R>)(p_method, p_info, p_return_nil_is_variant)); a->set_instance_class(T::get_class_static()); return a; } diff --git a/core/object/object.cpp b/core/object/object.cpp index 0a7879c774..c2cd42ff91 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -1028,15 +1028,15 @@ struct _ObjectSignalDisconnectData { Callable callable; }; -Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { +Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - ERR_FAIL_COND_V(p_argcount < 1, Variant()); + ERR_FAIL_COND_V(p_argcount < 1, Error::ERR_INVALID_PARAMETER); if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING_NAME; - ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Variant()); + ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Error::ERR_INVALID_PARAMETER); } r_error.error = Callable::CallError::CALL_OK; @@ -1050,9 +1050,7 @@ Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::C args = &p_args[1]; } - emit_signalp(signal, args, argc); - - return Variant(); + return emit_signalp(signal, args, argc); } Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) { diff --git a/core/object/object.h b/core/object/object.h index f58a79b49c..b640c4e78a 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -522,7 +522,7 @@ private: void _add_user_signal(const String &p_name, const Array &p_args = Array()); bool _has_user_signal(const StringName &p_name) const; - Variant _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error); + Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error); Array _get_signal_list() const; Array _get_signal_connection_list(const String &p_signal) const; Array _get_incoming_connections() const; diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp index ee8eb97a93..d3c48853f1 100644 --- a/core/object/undo_redo.cpp +++ b/core/object/undo_redo.cpp @@ -444,25 +444,25 @@ UndoRedo::~UndoRedo() { clear_history(); } -Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { +void UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; - return Variant(); + return; } if (p_args[0]->get_type() != Variant::OBJECT) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; - return Variant(); + return; } if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 1; r_error.expected = Variant::STRING_NAME; - return Variant(); + return; } r_error.error = Callable::CallError::CALL_OK; @@ -471,28 +471,27 @@ Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callabl StringName method = *p_args[1]; add_do_methodp(object, method, p_args + 2, p_argcount - 2); - return Variant(); } -Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { +void UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; - return Variant(); + return; } if (p_args[0]->get_type() != Variant::OBJECT) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; - return Variant(); + return; } if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 1; r_error.expected = Variant::STRING_NAME; - return Variant(); + return; } r_error.error = Callable::CallError::CALL_OK; @@ -501,7 +500,6 @@ Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Calla StringName method = *p_args[1]; add_undo_methodp(object, method, p_args + 2, p_argcount - 2); - return Variant(); } void UndoRedo::_bind_methods() { diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h index ecd7a21167..63cf3e5cbe 100644 --- a/core/object/undo_redo.h +++ b/core/object/undo_redo.h @@ -46,8 +46,8 @@ public: }; typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name); - Variant _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); - Variant _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); + void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); + void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount); typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value); |