#ifndef DELEGATE_H #define DELEGATE_H #include /* Here be dragons... */ template class delegate { public: delegate() : object_ptr(NULL), stub_ptr(NULL) { } template static delegate from_method(T* object_ptr) { delegate d; d.object_ptr = object_ptr; d.stub_ptr = &method_stub; return d; } void operator()(A0 a0, A1 a1) const { if (!object_ptr) return; return stub_ptr(object_ptr, a0, a1); } operator bool() const { return object_ptr != NULL; } private: typedef void (*stub_type)(void* object_ptr, A0 a0, A1 a1); void* object_ptr; stub_type stub_ptr; template static void method_stub(void* object_ptr, A0 a0, A1 a1) { T* p = static_cast(object_ptr); return (p->*TMethod)(a0, a1); } }; #define DELEGATE(DelegateType, Type, Object, Method) (DelegateType::from_method(&Object)) #endif//DELEGATE_H