(Solved) Re: Function pointer callback from C

Mike Johnson mrjohnson at miketec.org
Sun Feb 25 22:45:46 PST 2007


Aw, geez. You're right. Works perfectly. I guess I misunderstood 
something I had read.

Thanks,
Mike

For google cache:

connect the signal:

         this.sourceBuffer.connectSignal("changed",
                                         &on_sv_changed,
                                         cast(gpointer) this);

callback:

     private extern(C) void on_sv_changed(int *widget, gpointer data) {
         printf("changed\n");

         GtkSourceView v = cast(GtkSourceView) data;
         if(v)
             v.changed = true;
     }


Tyler Knott wrote:
> Mike Johnson wrote:
>>
>> 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?
>>
> 
> You can't convert /instances/, but you can cast any object /references/ 
> to void pointers (because references are just pointers with syntactic 
> sugar that hides all the details).  When you cast back from void* to a 
> class type, D even checks to make sure it's a sane cast (the cast will 
> return null if it's not sane).  The only caveat is that you're going to 
> be careful to keep a reference to that object in memory the GC is aware 
> of, otherwise it could get prematurely collected and cause weird runtime 
> crashes.


More information about the Digitalmars-d-learn mailing list