How could I wrap the following C macro in function...
# kotlin-native
b
How could I wrap the following C macro in function at *.def file? Cinterop doesn't pick it up as per https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#macros
Copy code
#define G_CALLBACK(f)         ((GCallback) (f))
k
depends on what types you'd be passing to it
function pointers?
b
kotlin's
(x,y) -> Unit
So yes, i think those will have to be function pointers
k
and what type is GCallback?
i assume that's a typealias
b
Copy code
// G_CALLBACK() macro is used to cast the callback function pointer
    // to generic void pointer
So generic void pointer if that means anything to you?
k
interesting
b
Trying to work with GTK3 😄
b
Hmm, not quite there. Essentially what I'm looking for is a C function that takes function pointer, call G_CALLBACK with it and returns void pointer
k
i'm also curious how the block is translated to C
since it's not just a function pointer
b
In other works what's the C function signature for a function that takes function pointer and returns void pointer
k
Copy code
void* f(void (*a)(int))
b
what's a and arg?
i mean what's the name of the argument to this function?
k
a
b
Next question: how do i convert
() -> Unit
to
CPointer<CFunction<() -> Unit>>
m
In C any pointer is compatible to void pointer, so it is just noop.
k
converting the lambda to a C type is really the crux of it
m
Converting Kotlin lambda to C callback?
k
i would probably start by adding a function that accepts
() -> Unit
as an argument, and see how the K/N compiler handles that in the generated bindings
n
Martynas - Can you please provide a C example where GCallback is being used? What C function are you trying to use?
There might be a workaround which doesn't require having to deal with the C macro.
As an example in GTK there are many macros for casting pointers. With Kotlin Native the pointer casting can be handled with the
reinterpret
extension function, which comes from the Kotlin Native C Interop library.
b
I've resolved it with
staticCfunction
and unchecked casts. Everything's working now.
I'm using the following for callbacks:
staticCFunction<(CPointer<GtkWidget>,gpointer) -> Unit>() as GCallback
But it seems reinterpret is more elegant solution
✔️ 1
n
That line can be made more readable by extracting
(CPointer<GtkWidget>, gpointer) -> Unit>()
into a Type Alias. In many cases the Kotlin compiler can apply type inference to the
reinterpret
function.