code_junkie

Calling a C++ function pointer on a specific object instance

Nov 11th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. typedef void (*EventFunction)(int nEvent);
  2.  
  3. class A
  4. {
  5. private:
  6. EventFunction handler;
  7.  
  8. public:
  9. void SetEvent(EventFunction func) { handler = func; }
  10.  
  11. void EventOne() { handler(1); }
  12. };
  13.  
  14. class B
  15. {
  16. private:
  17. A a;
  18. public:
  19. B() { a.SetEvent(EventFromA); } // What do I do here?
  20.  
  21. void EventFromA(int nEvent) { // do stuff }
  22. };
  23.  
  24. boost::function<int (int)> f;
  25. X x;
  26. f = std::bind1st(
  27. std::mem_fun(&X::foo), &x);
  28. f(5); // Call x.foo(5)
  29.  
  30. class A;
  31. class B;
  32. typedef void (B::*EventFunction)(int nEvent)
  33.  
  34. class A
  35. {
  36. private:
  37. EventFunction handler;
  38.  
  39. public:
  40. void SetEvent(EventFunction func) { handler = func; }
  41.  
  42. void EventOne(B* delegate) { ((*delegate).*handler)(1); } // note: ".*"
  43. };
  44.  
  45. class B
  46. {
  47. private:
  48. A a;
  49. public:
  50. B() { a.SetEvent(&B::EventFromA); } // note: "&::"
  51.  
  52. void EventFromA(int nEvent) { /* do stuff */ }
  53. };
  54.  
  55. #define CALLBACKGLUE(classname , func) static void CB_ ## func(void *obj) {static_cast< classname *>(obj)->func();}
  56. #define CALLBACKGLUEPTR(classname , func, type) static void CB_ ## func(void *obj,type *name) {static_cast< classname *>(obj)->func(name);}
  57. #define CALLBACKGLUEPTRPTR(classname , func, type,type2) static void CB_ ## func(void *obj,type *name,type2 *name2) {static_cast< classname *>(obj)->func(name,name2);}
  58. #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);}
  59. #define CALLBACKGLUEVAL(classname , func, type) static void CB_ ## func(void *obj,type val) {static_cast< classname *>(obj)->func(val);}
  60.  
  61. class C
  62. {
  63. public:
  64. int Method1(void) { return 3; }
  65. int Method2(void) { return x; }
  66.  
  67. int x;
  68. };
  69.  
  70. // This structure will hold a thunk to
  71. struct CCallback
  72. {
  73. C *obj; // Instance to callback on
  74. int (C::*callback)(void); // Class callback method, taking no arguments and returning int
  75. };
  76.  
  77. int CBootstrapper(CCallback *pThunk)
  78. {
  79. // Call the thunk
  80. return ((pThunk->obj) ->* (pThunk->callback))( /* args go here */ );
  81. }
  82.  
  83. void DoIt(C *obj, int (C::*callback)(void))
  84. {
  85. // 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
  86. struct CCallback thunk = {obj, callback};
  87. foobar(&CBootstrapper, &thunk);
  88. }
  89.  
  90. int main(void)
  91. {
  92. C c;
  93. DoIt(&c, &C::Method1); // Essentially calls foobar() with a callback of C::Method1 on c
  94. DoIt(&c, &C::Method2); // Ditto for C::Method2
  95. }
Add Comment
Please, Sign In to add comment