Callbacks in D as void functions
Wsdes via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Nov 13 06:49:59 PST 2014
Hi all,
I already posted this to Code Project, but maybe you have a
solution for my problem.
I am trying to write a wrapper for a C API in D. In C I have the
following definition of a callback type:
typedef void (*Callback)(void*);
In a struct all members are of the type Callback. In the main
function each member is assigned a function like this:
Events.OnData = MyDtaCB;
Here the MyDtaCB function has no prototype but is defined as
follows:
void MyDtaCB(void* pVoid){
// Do stuff
}
Now I tried to port this to D. The wiki says that callback
functions should be declared as integer functions, so my D code
looks like this:
extern (C) alias CEventCallback_t = int function(int, int);
extern (C) CEventCallback_t MyDtaCB(void*);
Here's the catch: when I try to assign the MyDtaCB function to a
member of the Events struct, I get a compilation error because
MyDtaCB is a void function and therefore returns no value. Seems
to work in C, though, but I don't know how to handle this in D.
The D tutorial has a function called getCallback() which returns
the void function but itself has the Callback type as return
value. However, in D this would lead to the same error as before
since the void function has no non-empty return value.
Any suggestions how to accomplish this? My thought now was to
screw the function definitions of the C API and implement them in
D. But what's the point in having the API then?
Thanks in advance.
More information about the Digitalmars-d-learn
mailing list