xlab / c-for-go

Automatic C-Go Bindings Generator for Go Programming Language
https://c.for-go.com
MIT License
1.5k stars 119 forks source link

Callback generation #158

Closed grzesl closed 12 months ago

grzesl commented 12 months ago

Hello, First of all - thank You for developing the c-for-go.
I develop little library in GO using Your generator. I Figured out that there is a issue with callbacks. For example i have using one function to manage two callbacks:

1) ug.UG_WindowCreate(&window1, obj, MAX_OBJECTS, window_1_callback) 2) ug.UG_WindowCreate(&window2, obj, MAX_OBJECTS, window_2_callback)

The result is that all events are received by window_1_callback . I have keep digging and found that in generated code is something like that:

(...)
    if uG_Message_Callback1FD3C4E6Func == nil {
        uG_Message_Callback1FD3C4E6Func = x
    }
(...)

and where the callback i called:

func uG_Message_Callback1FD3C4E6(cArg0 *C.UG_MESSAGE) {
    if uG_Message_Callback1FD3C4E6Func != nil {
        Arg01fd3c4e6 := NewUG_MESSAGERef(unsafe.Pointer(cArg0))
        uG_Message_Callback1FD3C4E6Func(Arg01fd3c4e6)
        return
    }

So, there is only one callback managed by c-for-go (the first one defined)?

I thing in code i have mentioned should be a slice of callbacks functions proxy or map:

uG_Message_Callback1FD3C4E6Func[id] = x

(...)

and call:

uG_Message_Callback1FD3C4E6Func[id](Arg01fd3c4e6)

Thank You grzesl

xlab commented 12 months ago

Thanks! yes, callbacks were really hard to implement, so I had this problem of multiplexing different callbacks, usually it passes some reference with it.. all my uses cases were using global callbacks, so generating just one was fine.

But trick is that you can copy generated code into separate human-made file and name those callbacks as you want. Yes, it will have some generated ugliness within, but you can give them nice public names and actually use multiple.

grzesl commented 12 months ago

Thanks for your advice. I will try to implement the custom managment.