Delegates and C function pointers
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 8 07:28:59 PST 2014
On Saturday, 8 November 2014 at 12:23:45 UTC, Nicolas Sicard
wrote:
> I would like to register a D delegate to a C API that takes a
> function pointer as a callback and a void* pointer to pass data
> to this callback.
>
> My solution is in http://dpaste.dzfl.pl/7d9b504b4b965.
>
> Is this code correct? Is there something simpler or already in
> Phobos that I have overlooked?
>
> Thanks
> -- Nicolas
It looks very similar to what i'm doing here:
https://github.com/nomad-software/tkd/blob/master/source/tkd/element/element.d#L174
I think you can simplify it though by just using a delegate
member in the data struct. Like this (untested):
static struct Data
{
void delegate() callback;
}
static extern(C) void adapter(void* ptr)
{
auto d = *(cast(Data*) ptr);
d.callback()
}
More information about the Digitalmars-d-learn
mailing list