Using a delegate when interfacing with C

Marco Cosentino via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 6 11:55:20 PDT 2014


Hey Adam,
an interesting aspect of what I'd like to achieve is to use
compile-time reflection to generate the wrapper functions for all
the delegates (there are ~ 10).

The pattern is like what I presented eariler and in addition to
that there are some delegates which have no return type (void).

I managed to write a template like this (D is awesome):

alias ProcessDelegate             = int delegate(NFrames nframes);

import std.traits;
private  template CallbackWrapper(alias T) if(isDelegate!T) {
    extern(C) static auto wrapper(ParameterTypeTuple!T params, void
* data) {
      auto client = cast(ClientImplementation) data;
      return mixin("client." ~ __traits(identifier, T) ~
"(params)");
    }
}

    void setProcessDelegate(ProcessDelegate deleg) {
      processDelegate = deleg;
      setProcessCallback( &
CallbackWrapper!(processDelegate).wrapper, cast(void *) this);
    }


More information about the Digitalmars-d-learn mailing list