// Callback_12.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms. // #include #include #include /** * Trampolines to methods for C-style callbacks. * * The void* / callback data is the instance you want the method * called on. */ template struct trampoline { /** * Trampoline for callbacks with void* argument at the end of the * argument list */ template static void postvoid(Types... args, void* object) { T* o = static_cast(object); (o->*ptmf)(std::forward(args)...); } /** * Trampoline for callbacks with void* as the first argument */ template static void prevoid(void* object, Types... args) { T* o = static_cast(object); (o->*ptmf)(std::forward(args)...); } }; void c_style_callback_caller(void (*cb)(int, void*), void* data) { cb(42, data); } void c_style_callback_caller_pre(void (*cb)(void*, int), void* data) { cb(data, 41); } class C { public: void p0() { std::cout << "p0()\n"; } void p1(int x) { std::cout << "p1(" << x << ")\n"; } static void classic_cb_p1(int x, void* object) { C* c = static_cast(object); c->p1(x); } }; int main() { C inst; // both produce EXACTLY the same code c_style_callback_caller(trampoline::postvoid<&C::p1>, &inst); c_style_callback_caller(&C::classic_cb_p1, &inst); // also supports void* as the first argument c_style_callback_caller_pre(trampoline::prevoid<&C::p1>, &inst); // special case: no parameters on callback means postvoid and // prevoid are identical trampoline::postvoid<&C::p0>(&inst); trampoline::prevoid<&C::p0>(&inst); return 0; }