Advertisement
bueddl

C++ call wrapper (hook)

Jun 5th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. void wrapper(SomeClass const *_this, int param)
  2. {
  3.     // Get some stuff done before
  4.     std::cout << "Pre call to SomeClass::mem_fn()" << std::endl
  5.                         << "  this = " << _this << std::endl
  6.                         << "  param = " << param << std::endl;
  7.  
  8.     // Get and call orig
  9.     Hook<SomeClass>::call_once<2>(_this, param);
  10.  
  11.     // Get some stuff done after
  12.     std::cout << "Post call to SomeClass::mem_fn()" << std::endl;
  13. }
  14.  
  15. // This example will print:
  16. //
  17. // $ ./class-test
  18. // Pre call to SomeClass::mem_fn()
  19. //   this = 0x1d8ec20
  20. //   param = 42
  21. // Original SomeClass::mem_fn()
  22. //   this = 0x1d8ec20
  23. //   param = 42
  24. // Post call to SomeClass::mem_fn()
  25.  
  26. int main()
  27. {
  28.     // Create object instance
  29.     SomeClass *some_object = new SomeClass;
  30.  
  31.     // Hook
  32.     Hook<SomeClass>::install<2>(some_object, wrapper);
  33.  
  34.     // Wrapped call
  35.     some_object->mem_fn(42);
  36.  
  37.     // Delete object instance
  38.     delete some_object;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement