GATE
Loading...
Searching...
No Matches
delegates.h File Reference

Generic encapsulation of callbacks for functions and objects. More...

#include "gate/gate_core_api.h"
#include "gate/gatetypes.h"
#include "gate/memalloc.h"
#include <stdarg.h>

Macros

#define GATE_DELEGATE_BIND_OBJ(delegate_name, delegate_ptr, function, obj_ptr)
 Initializes a gate_delegate_t instance with dispatchers for a specific target function bound to an object pointer.
 
#define GATE_DELEGATE_BIND_FUNC(delegate_name, delegate_ptr, function)
 Initializes a gate_delegate_t instance with dispatchers for a specific target function.
 
#define GATE_DELEGATE_DECLARE_0(delegate_name)
 Builds delegate dispatcher types to be used for target code with no arguments.
 
#define GATE_DELEGATE_DECLARE_1(delegate_name, type1)
 Builds delegate dispatcher types to be used for target code with 1 argument.
 
#define GATE_DELEGATE_DECLARE_2(delegate_name, type1, type2)
 Builds delegate dispatcher types to be used for target code with 2 arguments.
 
#define GATE_DELEGATE_DECLARE_3(delegate_name, type1, type2, type3)
 Builds delegate dispatcher types to be used for target code with 3 arguments.
 
#define GATE_DELEGATE_DECLARE_4(delegate_name, type1, type2, type3, type4)
 Builds delegate dispatcher types to be used for target code with 4 arguments.
 
#define GATE_DELEGATE_DECLARE_5(delegate_name, type1, type2, type3, type4, type5)
 Builds delegate dispatcher types to be used for target code with 5 arguments.
 
#define GATE_DELEGATE_DECLARE_6(delegate_name, type1, type2, type3, type4, type5, type6)
 Builds delegate dispatcher types to be used for target code with 6 arguments.
 

Functions

GATE_CORE_API gate_result_t gate_delegate_invoke (gate_delegate_t const *delegate_ptr,...)
 Invokes a delegate object with all provided variable arguments.
 

Detailed Description

Generic encapsulation of callbacks for functions and objects.

Macro Definition Documentation

◆ GATE_DELEGATE_BIND_OBJ

#define GATE_DELEGATE_BIND_OBJ ( delegate_name,
delegate_ptr,
function,
obj_ptr )
Value:
do { \
gate_delegate_generic_t generic_function = (gate_delegate_generic_t)&function; \
(delegate_ptr)->dispatcher = & GATE_DELEGATE_OBJ_DISPATCHER_NAME(delegate_name); \
gate_mem_copy(&(delegate_ptr)->function_storage, &generic_function, sizeof(generic_function)); \
(delegate_ptr)->object_ptr = obj_ptr; \
(delegate_ptr)->native_attachment = NULL; \
} while(0)

Initializes a gate_delegate_t instance with dispatchers for a specific target function bound to an object pointer.

Parameters
delegate_nameBase name of delegate-types that was created my GATE_DELEGATE_DECLARE_* macros
delegate_ptrPointer to gate_delegate_t type to be initialized
functionPointer to target method function to be covered by delegate
obj_ptrPointer to target object that is used as first argument (this-pointer) for function

◆ GATE_DELEGATE_BIND_FUNC

#define GATE_DELEGATE_BIND_FUNC ( delegate_name,
delegate_ptr,
function )
Value:
do { \
gate_delegate_generic_t generic_function = (gate_delegate_generic_t)&function; \
(delegate_ptr)->dispatcher = & GATE_DELEGATE_FUNC_DISPATCHER_NAME(delegate_name); \
gate_mem_copy(&(delegate_ptr)->function_storage, &generic_function, sizeof(generic_function)); \
(delegate_ptr)->object_ptr = NULL; \
(delegate_ptr)->native_attachment = NULL; \
} while(0)

Initializes a gate_delegate_t instance with dispatchers for a specific target function.

Parameters
delegate_nameBase name of delegate-types that was created my GATE_DELEGATE_DECLARE_* macros
delegate_ptrPointer to gate_delegate_t type to be initialized
functionPointer to target function to be covered by delegate

◆ GATE_DELEGATE_DECLARE_0

#define GATE_DELEGATE_DECLARE_0 ( delegate_name)
Value:
typedef gate_result_t(* delegate_name ## _obj_callback_t)(void* object_ptr); \
typedef gate_result_t(* delegate_name ## _func_callback_t)(void); \
static gate_result_t GATE_DELEGATE_OBJ_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _obj_callback_t callback; \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(delegate_ptr->object_ptr); \
return result; \
} \
static gate_result_t GATE_DELEGATE_FUNC_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _func_callback_t callback; \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(); \
return result; \
}
int gate_result_t
Definition gatetypes.h:601

Builds delegate dispatcher types to be used for target code with no arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions

◆ GATE_DELEGATE_DECLARE_1

#define GATE_DELEGATE_DECLARE_1 ( delegate_name,
type1 )
Value:
typedef gate_result_t(* delegate_name ## _obj_callback_t)(void* object_ptr, type1 arg1); \
typedef gate_result_t(* delegate_name ## _func_callback_t)(type1 arg1); \
static gate_result_t GATE_DELEGATE_OBJ_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _obj_callback_t callback; \
type1 arg1; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(delegate_ptr->object_ptr, arg1); \
return result; \
} \
static gate_result_t GATE_DELEGATE_FUNC_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _func_callback_t callback; \
type1 arg1; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(arg1); \
return result; \
}

Builds delegate dispatcher types to be used for target code with 1 argument.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of first argument

◆ GATE_DELEGATE_DECLARE_2

#define GATE_DELEGATE_DECLARE_2 ( delegate_name,
type1,
type2 )
Value:
typedef gate_result_t(* delegate_name ## _obj_callback_t)(void* object_ptr, type1 arg1, type2 arg2); \
typedef gate_result_t(* delegate_name ## _func_callback_t)(type1 arg1, type2 arg2); \
static gate_result_t GATE_DELEGATE_OBJ_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _obj_callback_t callback; \
type1 arg1; \
type2 arg2; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
GATE_DELEGATE_GET_VA_ARG(args->data, type2, &arg2); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(delegate_ptr->object_ptr, arg1, arg2); \
return result; \
} \
static gate_result_t GATE_DELEGATE_FUNC_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _func_callback_t callback; \
type1 arg1; \
type2 arg2; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
GATE_DELEGATE_GET_VA_ARG(args->data, type2, &arg2); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(arg1, arg2); \
return result; \
}

Builds delegate dispatcher types to be used for target code with 2 arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of first argument
type2Type of second argument

◆ GATE_DELEGATE_DECLARE_3

#define GATE_DELEGATE_DECLARE_3 ( delegate_name,
type1,
type2,
type3 )
Value:
typedef gate_result_t(* delegate_name ## _obj_callback_t)(void* object_ptr, type1 arg1, type2 arg2, type3 arg3); \
typedef gate_result_t(* delegate_name ## _func_callback_t)(type1 arg1, type2 arg2, type3 arg3); \
static gate_result_t GATE_DELEGATE_OBJ_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _obj_callback_t callback; \
type1 arg1; \
type2 arg2; \
type3 arg3; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
GATE_DELEGATE_GET_VA_ARG(args->data, type2, &arg2); \
GATE_DELEGATE_GET_VA_ARG(args->data, type3, &arg3); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(delegate_ptr->object_ptr, arg1, arg2, arg3); \
return result; \
} \
static gate_result_t GATE_DELEGATE_FUNC_DISPATCHER_NAME(delegate_name) (gate_delegate_t const* delegate_ptr, gate_va_list_carrier_t* args) \
{ \
gate_result_t result; \
delegate_name ## _func_callback_t callback; \
type1 arg1; \
type2 arg2; \
type3 arg3; \
GATE_DELEGATE_GET_VA_ARG(args->data, type1, &arg1); \
GATE_DELEGATE_GET_VA_ARG(args->data, type2, &arg2); \
GATE_DELEGATE_GET_VA_ARG(args->data, type3, &arg3); \
gate_mem_copy(&callback, &delegate_ptr->function_storage, sizeof(callback)); \
result = callback(arg1, arg2, arg3); \
return result; \
}

Builds delegate dispatcher types to be used for target code with 3 arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of 1st argument
type2Type of 2nd argument
type3Type of 3rd argument

◆ GATE_DELEGATE_DECLARE_4

#define GATE_DELEGATE_DECLARE_4 ( delegate_name,
type1,
type2,
type3,
type4 )

Builds delegate dispatcher types to be used for target code with 4 arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of 1st argument
type2Type of 2nd argument
type3Type of 3rd argument
type4Type of 4th argument

◆ GATE_DELEGATE_DECLARE_5

#define GATE_DELEGATE_DECLARE_5 ( delegate_name,
type1,
type2,
type3,
type4,
type5 )

Builds delegate dispatcher types to be used for target code with 5 arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of 1st argument
type2Type of 2nd argument
type3Type of 3rd argument
type4Type of 4th argument
type5Type of 5th argument

◆ GATE_DELEGATE_DECLARE_6

#define GATE_DELEGATE_DECLARE_6 ( delegate_name,
type1,
type2,
type3,
type4,
type5,
type6 )

Builds delegate dispatcher types to be used for target code with 6 arguments.

Parameters
delegate_nameBase name of delegate-types that is used as prefix for internally generated dispatcher functions
type1Type of 1st argument
type2Type of 2nd argument
type3Type of 3rd argument
type4Type of 4th argument
type5Type of 5th argument
type6Type of 6th argument

Function Documentation

◆ gate_delegate_invoke()

GATE_CORE_API gate_result_t gate_delegate_invoke ( gate_delegate_t const * delegate_ptr,
... )

Invokes a delegate object with all provided variable arguments.

Parameters
[in]delegate_ptrPointer to delegate object that covers the destination function or method
Returns
GATE_RESULT_* result code