How to use a member function (delegate) as a function pointer
Daniel Murphy
yebblies at nospamgmail.com
Sun Oct 31 23:19:40 PDT 2010
"Peter Federighi" <pfederighi at yahoo.com> wrote in message
news:ial8hq$2137$1 at digitalmars.com...
> Is there a way to do it without removing handler() from the class? When I
> try
> compiling, I get: Error: cannot implicitly convert expression
> (&this.handler)
> of type void delegate(int signal) to void C function(int).
Yes, but it requires some nasty code.
(Not tested on anything except windows)
http://yebblies.com/thunk.d
(Also not tested)
import yebblies.thunk.d;
import std.c.linux.linux;
import std.stdio;
class FOO
{
this()
{
th = thunk!"C"(&handler);
sa.sa_handler = th.ptr;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, null);
}
~this()
{
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, null);
}
private:
sigaction_t sa;
typeof(thunk!"C"(&handler)) th;
void handler(int signal)
{
writeln("Got an alarm signal.");
}
}
It basically gets around the restriction by writing some code into memory
that passes the delegate params in unused registers. I don't even know if
the page allocation code works on anything except windows.
Daniel
More information about the Digitalmars-d-learn
mailing list