Is there a way to pass through an array of structs...
# kotlin-native
n
Is there a way to pass through an array of structs (by reference) to a c function as a parameter? Need to supply entries to g_action_map_add_action_entries function ( https://developer.gnome.org/gio/stable/GActionMap.html#g-action-map-add-action-entries ). Currently I have a single entry (quitAction) passed through as a parameter which works fine:
Copy code
// ...
val quitAction = cValue<GActionEntry> {
        name = quitActionName
        parameter_type = null
        activate = staticCFunction(::quitActivated)
}.ptr
// ...
g_action_map_add_action_entries(app.reinterpret(), quitAction, 1, win)
o
Copy code
val actions = nativeHeap.allocArray<GActionEntry>(2) { index: Int ->
        when (index) {
            1 -> apply {
                name = "quit".cstr.ptr
                parameter_type = null
                activate = staticCFunction { action, variant, pointer ->
                    println("quit $action")
                }
            }
            2 -> apply {
                name = "help".cstr.ptr
                parameter_type = null
                activate = staticCFunction { action, variant, pointer ->
                    println("help $action")
                }
            }
        }
    }
    g_action_map_add_action_entries(app.reinterpret(), actions, 2, window)
👍 1
n
Works as expected after decreasing the indexes by 1 (0, 1 etc), and using memScoped instead of nativeHeap. Really mind blowing how apply can be used in the allocArray block, and type inference is applied automatically simple smile. Must have overlooked the apply trick in the documentation.