toContextDg

Generates delegate that stores specified context as a delegate context pointer and, when called, forwards it to function F as a regular argument.

Intended to be used as a performance optimization hack to create no-allocation closures that only need to capture one pointer size argument.

ReturnTypeOf!(F) delegate
()
toContextDg
()
(
void* context
)

Parameters

F

function to call when delegate is called, must take exactly one void* argument which is the passed context

context void*

context pointer to forward to F when resulting delegate is called

Return Value

Type: ReturnTypeOf!(F) delegate
()

forged delegate that can be passed to any API expecting regular `T delegate()` where T is the return type of F

Examples

static bool done = false;

static void handler ( void* context )
{
    test!("==")(cast(size_t) context, 42);
    done = true;
}

void delegate() dg = toContextDg!(handler)(cast(void*) 42);
test!("==")(cast(size_t) dg.ptr, 42);
dg();
test(done);

Meta