wrapping a C style delegate

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 25 17:27:47 PDT 2017


On 08/25/2017 04:00 PM, Nicholas Wilson wrote:
> On Friday, 25 August 2017 at 13:49:20 UTC, Kagamin wrote:
>> You're not specific enough. What would be semantics of such wrapper?
>
> The C function I'm trying to wrap takes a function pointer which is
> essentially a delegate, but not quite:
>
> ErrorEnum function(Struct* s, void function(Struct*, ErrorEnum status,
> void *userData) callback, void *userData, uint flags) SomeAPIaddCallback;
>
> I want to make it a member function of a wrapping struct so I can call
> it like
>
> MyStruct ms = ...
>
> ms.addCallback((ErrorEnum ee) { ... });
>
> instead of
>
> SomeAPIaddCallback(ms.s,(Struct*, ErrorEnum status, void *userData) {
> ... } /*doesn't become a delegate */,null,0);
>
> I'm not sure how to do it.
>
>

I think you need a variation of intermediateCallback() below. I passed 
the address of the delegate as userData but you can construct any 
context that contains everything that you need (e.g. the address of ms).

import std.stdio;

// The C struct
struct Struct {
     int i;
}

// Some C type
enum ErrorEnum {
     zero
}

// Some C function taking a callback
ErrorEnum SomeAPIaddCallback(Struct* s, void function(Struct*, ErrorEnum 
status, void *userData) callback, void *userData, uint flags) {
     writeln("SomeAPIaddCallback called for object ", s);
     writeln("Calling the callback...");
     callback(s, ErrorEnum.zero, userData);
     return ErrorEnum.zero;
}

// The callback to pass to the C function
void intermediateCallback(Struct * s, ErrorEnum status, void *userData) {
     writeln("intermediateCallback called");
     auto cb = cast(void delegate(ErrorEnum)*)userData;
     (*cb)(status);
}

// The D wrapper always passes intermediateCallback to the C function
struct MyStruct {
     Struct * s;
     void addCallback(void delegate(ErrorEnum ee) callback) {
         SomeAPIaddCallback(s, &intermediateCallback, &callback, 0);
     }
}

void main() {
     auto s = Struct(42);
     auto ms = MyStruct(&s);
     ms.addCallback((ErrorEnum ee) {
             writefln("The callback is called with %s for %s", ee, ms.s);
     });
}

Ali



More information about the Digitalmars-d-learn mailing list