Advertisement
Guest User

delegate.h

a guest
Oct 28th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #ifndef DELEGATE_H
  2. #define DELEGATE_H
  3.  
  4. #include <iostream>
  5.  
  6. template<typename Signature>
  7. class delegate;
  8.  
  9.  
  10. class delegate_base
  11. {
  12. protected:
  13.     delegate_base()
  14.         : m_ptr(0),
  15.           m_proc(0),
  16.           m_procWrapper(0)
  17.     {}
  18.  
  19.     // Copying
  20.     delegate_base(const delegate_base& other)
  21.         : m_ptr(other.m_ptr),
  22.           m_proc(other.m_proc),
  23.           m_procWrapper(other.m_procWrapper)
  24.     {}
  25.  
  26.     delegate_base& operator=(const delegate_base& rhs)
  27.     {
  28.         delegate_base(rhs).swap(*this);
  29.         return *this;
  30.     }
  31.  
  32.     ~delegate_base() {}
  33.  
  34.     void swap(delegate_base& other) throw()
  35.     {
  36.         std::swap(m_ptr, other.m_ptr);
  37.         std::swap(m_proc, other.m_proc);
  38.         std::swap(m_procWrapper, other.m_procWrapper);
  39.     }
  40.  
  41. public:
  42.     void dump() const
  43.     {
  44.         uintptr_t *tmp  = reinterpret_cast<uintptr_t*>(m_proc);
  45.  
  46.         std::cout << "-> dump" << std::endl
  47.                   << "   proc = " << m_proc << ", *proc = 0x" << std::hex <<  *tmp  << std::endl;
  48.     }
  49.  
  50.     void reset()
  51.     {
  52.         delegate_base().swap(*this);
  53.     }
  54.  
  55.     operator bool()
  56.     {
  57.         return (m_proc && m_procWrapper);
  58.     }
  59.  
  60. protected:
  61.     void *m_ptr;
  62.     void *m_proc;
  63.     void *m_procWrapper;
  64. };
  65.  
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////
  67.  
  68. // 0 arg(s)
  69. #define DELEGATE_COMMA
  70. #define DELEGATE_TEMPLATE_ARGS
  71. #define DELEGATE_ARG_TYPES
  72. #define DELEGATE_PROC_ARGS
  73. #define DELEGATE_ARGS
  74. #include "delegate_impl.i"
  75.  
  76. // 1 arg(s)
  77. #define DELEGATE_COMMA ,
  78. #define DELEGATE_TEMPLATE_ARGS typename A0
  79. #define DELEGATE_ARG_TYPES A0
  80. #define DELEGATE_PROC_ARGS A0 a0
  81. #define DELEGATE_ARGS      a0
  82. #include "delegate_impl.i"
  83.  
  84. // 2 arg(s)
  85. #define DELEGATE_COMMA ,
  86. #define DELEGATE_TEMPLATE_ARGS typename A0, typename A1
  87. #define DELEGATE_ARG_TYPES A0, A1
  88. #define DELEGATE_PROC_ARGS A0 a0, A1 a1
  89. #define DELEGATE_ARGS      a0, a1
  90. #include "delegate_impl.i"
  91.  
  92. // 3 arg(s)
  93. #define DELEGATE_COMMA ,
  94. #define DELEGATE_TEMPLATE_ARGS typename A0, typename A1, typename A2
  95. #define DELEGATE_ARG_TYPES A0, A1, A2
  96. #define DELEGATE_PROC_ARGS A0 a0, A1 a1, A2 a2
  97. #define DELEGATE_ARGS      a0, a1, a2
  98. #include "delegate_impl.i"
  99.  
  100. // 4 arg(s)
  101. #define DELEGATE_COMMA ,
  102. #define DELEGATE_TEMPLATE_ARGS typename A0, typename A1, typename A2, typename A3
  103. #define DELEGATE_ARG_TYPES A0, A1, A2, A3
  104. #define DELEGATE_PROC_ARGS A0 a0, A1 a1, A2 a2, A3 a3
  105. #define DELEGATE_ARGS      a0, a1, a2, a3
  106. #include "delegate_impl.i"
  107.  
  108. // 5 arg(s)
  109. #define DELEGATE_COMMA ,
  110. #define DELEGATE_TEMPLATE_ARGS typename A0, typename A1, typename A2, typename A3, typename A4
  111. #define DELEGATE_ARG_TYPES A0, A1, A2, A3, A4
  112. #define DELEGATE_PROC_ARGS A0 a0, A1 a1, A2 a2, A3 a3, A4 a4
  113. #define DELEGATE_ARGS      a0, a1, a2, a3, a4
  114. #include "delegate_impl.i"
  115.  
  116. // 6 arg(s)
  117. #define DELEGATE_COMMA ,
  118. #define DELEGATE_TEMPLATE_ARGS typename A0, typename A1, typename A2, typename A3, typename A4, typename A5
  119. #define DELEGATE_ARG_TYPES A0, A1, A2, A3, A4, A5
  120. #define DELEGATE_PROC_ARGS A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5
  121. #define DELEGATE_ARGS      a0, a1, a2, a3, a4, a5
  122. #include "delegate_impl.i"
  123.  
  124.  
  125. #endif // DELEGATE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement