Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef void (*EventFunction)(int nEvent);
- class A
- {
- private:
- EventFunction handler;
- public:
- void SetEvent(EventFunction func) { handler = func; }
- void EventOne() { handler(1); }
- };
- class B
- {
- private:
- A a;
- public:
- B() { a.SetEvent(EventFromA); } // What do I do here?
- void EventFromA(int nEvent) { // do stuff }
- };
- boost::function<int (int)> f;
- X x;
- f = std::bind1st(
- std::mem_fun(&X::foo), &x);
- f(5); // Call x.foo(5)
- class A;
- class B;
- typedef void (B::*EventFunction)(int nEvent)
- class A
- {
- private:
- EventFunction handler;
- public:
- void SetEvent(EventFunction func) { handler = func; }
- void EventOne(B* delegate) { ((*delegate).*handler)(1); } // note: ".*"
- };
- class B
- {
- private:
- A a;
- public:
- B() { a.SetEvent(&B::EventFromA); } // note: "&::"
- void EventFromA(int nEvent) { /* do stuff */ }
- };
- #define CALLBACKGLUE(classname , func) static void CB_ ## func(void *obj) {static_cast< classname *>(obj)->func();}
- #define CALLBACKGLUEPTR(classname , func, type) static void CB_ ## func(void *obj,type *name) {static_cast< classname *>(obj)->func(name);}
- #define CALLBACKGLUEPTRPTR(classname , func, type,type2) static void CB_ ## func(void *obj,type *name,type2 *name2) {static_cast< classname *>(obj)->func(name,name2);}
- #define CALLBACKGLUEPTRPTRPTR(classname , func, type,type2,type3) static void CB_ ## func(void *obj,type *name,type2 *name2,type3 *name3) {static_cast< classname *>(obj)->func(name,name2,name3);}
- #define CALLBACKGLUEVAL(classname , func, type) static void CB_ ## func(void *obj,type val) {static_cast< classname *>(obj)->func(val);}
- class C
- {
- public:
- int Method1(void) { return 3; }
- int Method2(void) { return x; }
- int x;
- };
- // This structure will hold a thunk to
- struct CCallback
- {
- C *obj; // Instance to callback on
- int (C::*callback)(void); // Class callback method, taking no arguments and returning int
- };
- int CBootstrapper(CCallback *pThunk)
- {
- // Call the thunk
- return ((pThunk->obj) ->* (pThunk->callback))( /* args go here */ );
- }
- void DoIt(C *obj, int (C::*callback)(void))
- {
- // foobar() is some C library function that takes a function which takes no arguments and returns int, and it also takes a void*, and we can't change it
- struct CCallback thunk = {obj, callback};
- foobar(&CBootstrapper, &thunk);
- }
- int main(void)
- {
- C c;
- DoIt(&c, &C::Method1); // Essentially calls foobar() with a callback of C::Method1 on c
- DoIt(&c, &C::Method2); // Ditto for C::Method2
- }
Add Comment
Please, Sign In to add comment