Guest User

Untitled

a guest
Jan 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "xbyak/xbyak.h"
  3.  
  4. template <class T>
  5. struct Delegate : public Xbyak::CodeGenerator {
  6. Delegate(T *t, void (T::*p)()) {
  7. mov(ecx, reinterpret_cast<intptr_t>(t));
  8. jmp(*reinterpret_cast<void **>(&p));
  9. }
  10.  
  11. void (*getPtr())() {
  12. return reinterpret_cast<void (*)()>(
  13. const_cast<Xbyak::uint8 *>(getCode()));
  14. }
  15. };
  16.  
  17. struct Test1 {
  18. int n;
  19. Test1(int n) : n(n) {}
  20. void show() {
  21. printf("%d\n", n);
  22. }
  23. };
  24.  
  25. struct Test2 {
  26. int n;
  27. Test2(int n) : n(n) {}
  28. void show() {
  29. printf("%d\n", n);
  30. }
  31. };
  32.  
  33. struct Test3 {
  34. static void show() {
  35. printf("?\n");
  36. }
  37. };
  38.  
  39. void show() {
  40. printf("!\n");
  41. }
  42.  
  43. void call(void (*f)()) { f(); }
  44.  
  45. int main() {
  46. Test1 t1(1);
  47. Test2 t2(2);
  48. Delegate<Test1> d1(&t1, &Test1::show);
  49. Delegate<Test2> d2(&t2, &Test2::show);
  50. call(d1.getPtr());
  51. call(d2.getPtr());
  52. call(Test3::show);
  53. call(show);
  54. }
Add Comment
Please, Sign In to add comment