callback parameter order question

evilrat evilrat666 at gmail.com
Sat May 11 18:01:03 PDT 2013


On Saturday, 11 May 2013 at 17:34:53 UTC, gedaiu wrote:
>
> yeah... what a shameful mistake.. i vave a new tricky 
> question... how i can provide a class method instead a simple 
> function as callback?

with D code use delegates as Ali commented, it's simple. with C 
code use static method and pass instance pointer as callback 
target, something like this

---------------

// C function
alias extern(C) function(void*) callbackfunc;
extern(C) setCallback(callbackfunc cb, void* userptr);


class Handler {
  static extern(C) callback(void* instance) {
   if ( auto self = cast(Handler) instance )
     self.onCallback();
  }

  void onCallback() {
   // ...
   // your code here
   // ...
  }
}


void setCAPI_callback(Handler hnd) {
  setCallback(Handler.callback, hnd); // don't remember if needed 
to cast hnd to void
}

however this would require C API which has userptr argument in 
callbacks(hopefully most libs i've seen has this)


More information about the Digitalmars-d-learn mailing list