From 4177e5c7c755e7f85e7778d2d0602ebe7a74e350 Mon Sep 17 00:00:00 2001 From: reduz Date: Sat, 21 May 2022 18:48:50 +0200 Subject: Add callable_mp_static Allows using a static (C) function as a callable. --- core/object/callable_method_pointer.h | 82 +++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/core/object/callable_method_pointer.h b/core/object/callable_method_pointer.h index 577d4b9fbd..f2a440b49a 100644 --- a/core/object/callable_method_pointer.h +++ b/core/object/callable_method_pointer.h @@ -245,4 +245,86 @@ Callable create_custom_callable_function_pointer(T *p_instance, #define callable_mp(I, M) create_custom_callable_function_pointer(I, M) #endif +// STATIC VERSIONS + +template +class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase { + struct Data { + void (*method)(P...); + } data; + +public: + virtual ObjectID get_object() const { + return ObjectID(); + } + + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { + call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error); + r_return_value = Variant(); + } + + CallableCustomStaticMethodPointer(void (*p_method)(P...)) { + memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes. + data.method = p_method; + _setup((uint32_t *)&data, sizeof(Data)); + } +}; + +template +Callable create_custom_callable_static_function_pointer( +#ifdef DEBUG_METHODS_ENABLED + const char *p_func_text, +#endif + void (*p_method)(P...)) { + typedef CallableCustomStaticMethodPointer CCMP; // Messes with memnew otherwise. + CCMP *ccmp = memnew(CCMP(p_method)); +#ifdef DEBUG_METHODS_ENABLED + ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand. +#endif + return Callable(ccmp); +} + +template +class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase { + struct Data { + R(*method) + (P...); + } data; + +public: + virtual ObjectID get_object() const { + return ObjectID(); + } + + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { + call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error); + } + + CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) { + memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes. + data.method = p_method; + _setup((uint32_t *)&data, sizeof(Data)); + } +}; + +template +Callable create_custom_callable_static_function_pointer( +#ifdef DEBUG_METHODS_ENABLED + const char *p_func_text, +#endif + R (*p_method)(P...)) { + typedef CallableCustomStaticMethodPointerRet CCMP; // Messes with memnew otherwise. + CCMP *ccmp = memnew(CCMP(p_method)); +#ifdef DEBUG_METHODS_ENABLED + ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand. +#endif + return Callable(ccmp); +} + +#ifdef DEBUG_METHODS_ENABLED +#define callable_mp_static(M) create_custom_callable_static_function_pointer(#M, M) +#else +#define callable_mp_static(M) create_custom_callable_static_function_pointer(M) +#endif + #endif // CALLABLE_METHOD_POINTER_H -- cgit v1.2.3