Function to convert functions to delegates
BCS
BCS at pathilink.com
Sat Dec 23 19:03:44 PST 2006
Jarrett Billingsley wrote:
> I came up with this function to make it easier to make libraries that can
> take function pointers or delegates for things like callbacks. Basically it
> creates a dummy struct to function as the context for the delegate, which
> then calls the original function. I guess that's a thunk? Anyway:
>
[...]
>
> To use it, just make a function:
>
> void func(int x, int y)
> {
> writefln("func: ", x, ", ", y);
> }
>
> And then call it:
>
> auto dg = Delegatize!(func)();
> dg(4, 5); // prints "func: 4, 5"
>
I guess that's a common thing to need to do. I wrote one of my own a
while back. It trades a bit of a runtime hit (notice the new and
assignment) for runtime flexibility (it can take a runtime function
pointer).
T delegate(A) Fn2Dg(T, A...)(T function(A) f)
{
struct tmp
{
typeof(f) fn;
T ret(A args){ return fn(args); }
};
tmp* ret = new tmp;
ret.fn = f;
return &ret.ret;
}
char fn(int i, char j);
char delegate(int, char) dg = Fn2Dg(&fn);
char function(int, char) fp = &fn;
char delegate(int, char) dgr = Fn2Dg(fp);
It comes from my paper on D:
http://www.webpages.uidaho.edu/~shro822/term_008.pdf
More information about the Digitalmars-d
mailing list