Function pointer callback from C

Alan Knowles alan at akbkhome.com
Mon Feb 26 00:11:17 PST 2007


Did you know Ant has committed the SourceView bindings to subversion:

http://svn.dsource.org/projects/dui/trunk/gtkD/srcsv/gsv/

The bindings for addOnChanged() are in TextBuffer
http://svn.dsource.org/projects/dui/trunk/gtkD/src/gtk/TextBuffer.d

Regards
Alan


Mike Johnson wrote:
> Hello,
> 
> I've been beating my head against D's delegate support. I've probably
> read every mention in the docs and post on the news server multiple
> times and I'm just not getting it. Any help would be awesome.
> 
> I'm currently trying to connect a signal handler to a member function.
> 
> The member function needs access to the `this' pointer. The callback
> itself works, but I get undefined weirdness and crashes on trying to
> modify any D member variables.
> 
> (This is GTK.)
> 
> The signal handler is installed:
> 
>         this.sourceBuffer.connectSignal("changed",
>                                         &on_sv_changed);
> 
> The function doing black magic:
> 
>     extern(C) public void connectSignal(
>         char[] signalName,
>         void delegate(int* widget, gpointer data) func,
>         int data = 0,
>         GConnectFlags flags = cast(GConnectFlags) 0) {
> 
>         g_signal_connect_data(this.self,
>                               String.stringz(signalName),
>                               func.funcptr,
>                               cast(gpointer) data,
>                               cast(int *) 0,
>                               flags);
>     }
> 
> Of course, I can pass void* data through the gpointer variable. But I
> can't convert a class instance to an pointer... which would be really
> *cough* cool *cough* :-) But maybe something else?
> 
> The signal handler itself:
> 
>     private extern(C) void on_sv_changed(int *widget, int *data) {
>         printf("changed\n");
>     this.changed = true; // GTK spews warnings and dies weirdly
>     }
> 
> Thanks so much,
> Mike Johnson


More information about the Digitalmars-d-learn mailing list