pass a delegate to an API as a context pointer?

teo teo.ubuntu at yahoo.com
Mon Jul 2 15:03:25 PDT 2007


Hi *, is it possible to pass a delegate to an API function which takes a pointer to a callback function and a context pointer? The code bellow describes what I'm trying to achieve.

// C-Library
typedef void (*callback)(void *context);
void xyz(callback handler, void *context);

// D-Program
alias void function(void *context) callback;
extern(C) void xyz(callback handler, void *context);
alias int delegate() foo;

class A
{
	int abc() { return 1; }
}
class B
{
	static void handler(void *context)
	{
		foo f = cast(foo)context;
		int i = f();	// call A.abc();
	}
	void test(foo f)
	{
		xyz(cast(callback)handler, cast(void*)f);
	}
}
void main()
{
	A a = new A();
	B b = new B();
	b.test(&a.abc);
}

Thanks for any tips!
-- teo



More information about the Digitalmars-d mailing list