In the language guide it says this:Callbacks
When used in the context of a foreign handler definition, a foreign handler type will cause automatic bridging of the LCB handler to a C function pointer which can be called directly by the native code.
The function pointers created in this fashion have lifetime equivalent to that of the calling context. In particular, for widgets they will last as long as the widget does, for all other module types they will last as long as the module is loaded.
For example, suppose you have the following function in myLib:
This can be bound to using the following:Code: Select all
void qsort(void base, size_t nitems, size_t size, int (compar)(const void , const void)) void c_function_with_callback(void* p_context, bool(*func)(void* p_context))
Code: Select all
public foreign handler type CallbackType(in pContext as Pointer) returns CBool foreign handler CFunctionWithCallback(in pContext as Pointer, in pHandler as CallbackType) returns nothing binds to "myLib>c_function_with_callback" handler MyCallback(in pContext as Pointer) returns CBool ... return true end handler handler CallCFunctionWithCallback(in pContext as Pointer) CFunctionWithCallback(pContext, MyCallback) end handler
(I really don't like the way the language guide is written, but I digress)Handler Types
HandlerTypeDefinition
: [ 'foreign' ] 'handler' 'type' <Name: Identifier> '(' [ ParameterList ] ')' [ 'returns' <ReturnType: Type> ]
A handler type definition defines a type which can hold a handler. Variables of such types can hold handlers (just like function pointers in C) which allows them to be called dynamically.
If the handler type is defined as foreign then automatic bridging to a C function pointer will occur when the type appears as the type of a parameter in a foreign handler definition.
Note: Passing an LCB handler to a foreign function requires creation of a function pointer. The lifetime of the function pointer is the same as the widget or module which created it.
So I have a foreign lib with a C Function that sets a callback handler and looks something like this:
Code: Select all
cLibThingThatSetsACallback( SomeOtherThing *, handler_t, void *)
pointer_to_new_thing (pointer to SomeOtherThing, a ref to a callback handler, nothing or pointer to OptionalSomeThing)
the callback handler in c looks like this:
Code: Select all
int (* handler_t) (void *OptionalSomeThing, incomingSecondSomthing *SecondSomething)
integer status code returned from callback handler ( along with returning nothing or a pointer OptionalSomeThing, and a pointer to incoming SecondSomthing )
I'm trying to understand how to translate all of this into LCB:
Code: Select all
public foreign handler type CallbackHandlerType(in pOptionalSomeThing as optional Pointer, in pSecondSomething as Pointer) returns CInt
foreign handler CFunctionThatSetsACallbackBinding(in tSomeOtherThing as Pointer, in pHandler as CallbackHandlerType, in pOptionalSomeThing as optional Pointer) returns nothing binds to "myLib>c_function_that_sets_a_callback"
handler MyCallbackLCBHandler(in pSomething as Pointer,in pSecondSomething as Pointer) returns CInt
--- ...
return true
end handler
handler Call_CFunctionThatSetsACallbackBinding()
variable tSomeOtherThing as Pointer
variable tOptionalSomeThing as optional Pointer
put nothing into tOptionalSomeThing -- void
-- pass SomeOtherThing pointer, and the LCB Callback Handler ref, and the tOptionalSomeThing pointer:
CFunctionThatSetsACallbackBinding(tSomeOtherThing, MyCallbackLCBHandler, tOptionalSomeThing) -
end handler
These are the actual c lib stuff I'm trying to deal with:
Code: Select all
fluid_midi_driver_t* new_fluid_midi_driver( fluid_settings_t * settings, handle_midi_event_func_t handler, void * event_handler_data )
typedef int(* handle_midi_event_func_t) (void *data, fluid_midi_event_t *event)