Guest User

Untitled

a guest
Jul 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4. #include <memory>
  5.  
  6.  
  7. /*!
  8. class ICallback
  9.  
  10. \brief The class represents an interface of a callback
  11. */
  12. template<typename TResult, typename... TArgs>
  13. class ICallback
  14. {
  15. public:
  16. ICallback() = default;
  17. virtual ~ICallback() = default;
  18.  
  19. /*!
  20. \brief The method causes call of a specified callback
  21. \param [in] args The arguments that will be send to the callback
  22.  
  23. \return Some value of TResult type
  24. */
  25. virtual TResult Invoke(TArgs... args) const = 0;
  26.  
  27. /*!
  28. \brief The method implements comparator's functionality and allows to
  29. compare two callbacks by their content
  30.  
  31. \return The method returns true if both callbacks' objects have the same type
  32. and point to the same functions
  33. */
  34. virtual bool EqualsTo(const ICallback<TResult, TArgs...>* pCallback) const = 0;
  35. };
  36.  
  37.  
  38. /*!
  39. class CStaticCallback
  40.  
  41. \brief The class implements ICallback interface
  42. */
  43. template<typename TResult, typename... TArgs>
  44. class CStaticCallback : public ICallback<TResult, TArgs...>
  45. {
  46. protected:
  47. typedef TResult(*TCallbackPtr)(TArgs...);
  48. public:
  49. CStaticCallback(TCallbackPtr callee)
  50. {
  51. mpCalleeFunc = callee;
  52.  
  53. assert(mpCalleeFunc != nullptr);
  54. }
  55.  
  56. virtual ~CStaticCallback()
  57. {
  58. mpCalleeFunc = nullptr;
  59. }
  60.  
  61. /*!
  62. \brief The method causes call of a specified callback
  63. \param [in] args The arguments that will be send to the callback
  64.  
  65. \return Some value of TResult type
  66. */
  67. TResult Invoke(TArgs... args) const override
  68. {
  69. if (mpCalleeFunc != nullptr)
  70. {
  71. (*mpCalleeFunc)(args...);
  72. }
  73. }
  74.  
  75. /*!
  76. \brief The method implements comparator's functionality and allows to
  77. compare two callbacks by their content
  78.  
  79. \return The method returns true if both callbacks' objects have the same type
  80. and point to the same functions
  81. */
  82. bool EqualsTo(const ICallback<TResult, TArgs...>* pCallback) const override
  83. {
  84. const CStaticCallback<TResult, TArgs...>* pStaticCallback = dynamic_cast<const CStaticCallback<TResult, TArgs...>*>(pCallback);
  85.  
  86. if (!pStaticCallback)
  87. {
  88. return false;
  89. }
  90.  
  91. return pStaticCallback->mpCalleeFunc == mpCalleeFunc;
  92. }
  93.  
  94. static std::unique_ptr<ICallback<TResult, TArgs...>> CreateCallback(TCallbackPtr callee)
  95. {
  96. return std::make_unique<CStaticCallback>(callee);
  97. }
  98. protected:
  99. TCallbackPtr mpCalleeFunc;
  100. };
  101.  
  102.  
  103. /*!
  104. class CMethodCallback
  105.  
  106. \brief The class implements ICallback
  107. */
  108. template<typename TClass, typename TResult, typename... TArgs>
  109. class CMethodCallback : public ICallback<TResult, TArgs...>
  110. {
  111. protected:
  112. typedef TResult(TClass::*TCallbackPtr)(TArgs...);
  113. public:
  114. CMethodCallback(TClass* pObject, TCallbackPtr callee)
  115. {
  116. mpCalleeFunc = callee;
  117.  
  118. mpCallerObject = pObject;
  119.  
  120. assert(mpCalleeFunc != nullptr);
  121. assert(mpCallerObject != nullptr);
  122. }
  123.  
  124. virtual ~CMethodCallback()
  125. {
  126. mpCalleeFunc = nullptr;
  127. }
  128.  
  129. /*!
  130. \brief The method causes call of a specified callback
  131. \param [in] args The arguments that will be send to the callback
  132.  
  133. \return Some value of TResult type
  134. */
  135. TResult Invoke(TArgs... args) const override
  136. {
  137. if (mpCalleeFunc != nullptr &&
  138. mpCallerObject != nullptr)
  139. {
  140. (mpCallerObject->*mpCalleeFunc)(args...);
  141. }
  142. }
  143.  
  144. /*!
  145. \brief The method implements comparator's functionality and allows to
  146. compare two callbacks by their content
  147.  
  148. \return The method returns true if both callbacks' objects have the same type
  149. and point to the same functions
  150. */
  151. bool EqualsTo(const ICallback<TResult, TArgs...>* pCallback) const override
  152. {
  153. const CMethodCallback<TClass, TResult, TArgs...>* pMethodCallback = dynamic_cast<const CMethodCallback<TClass, TResult, TArgs...>*>(pCallback);
  154.  
  155. if (!pMethodCallback)
  156. {
  157. return false;
  158. }
  159.  
  160. return pMethodCallback->mpCalleeFunc == mpCalleeFunc &&
  161. pMethodCallback->mpCallerObject == mpCallerObject;
  162. }
  163.  
  164. static std::unique_ptr<ICallback<TResult, TArgs...>> CreateCallback(TClass* pCallerObject, TCallbackPtr callee)
  165. {
  166. return std::make_unique<CMethodCallback>(pCallerObject, callee);
  167. }
  168. protected:
  169. TCallbackPtr mpCalleeFunc;
  170. TClass* mpCallerObject;
  171. };
Add Comment
Please, Sign In to add comment