Callback.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2006-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: HN, AH
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Definition of callback objects.
  10. Helper functions used for managing callbacks. Callback objects are used
  11. to encapsulate C-style functions or C++ member functions as
  12. callbacks.
  13. Based on the ideas of Paul Jakubik.
  14. */
  15. #ifndef __PYLON_CALLBACK_H__
  16. #define __PYLON_CALLBACK_H__
  17. #if _MSC_VER > 1000
  18. #pragma once
  19. #endif
  20. #include <cassert>
  21. #include <cstddef>
  22. #include <pylon/Platform.h>
  23. #ifdef _MSC_VER
  24. # pragma pack(push, PYLON_PACKING)
  25. #endif /* _MSC_VER */
  26. namespace Pylon
  27. {
  28. /*
  29. \brief Abstract callback container.
  30. This helper class is used to manage device removal callbacks.
  31. \see RegisterRemovalCallback()
  32. \see IPylonDevice::RegisterRemovalCallback()
  33. */
  34. template <class P1>
  35. class Base_Callback1Body
  36. {
  37. public:
  38. //! destructor
  39. virtual ~Base_Callback1Body( void )
  40. {
  41. };
  42. //! do operation with P1 as argument
  43. virtual void operator()( P1 ) const = 0;
  44. //! deep copy
  45. virtual Base_Callback1Body<P1>* clone( void ) const = 0;
  46. };
  47. /*
  48. \brief Callback object with one parameter P1
  49. This helper class is used to manage device removal callbacks.
  50. \see RegisterRemovalCallback()
  51. \see IPylonDevice::RegisterRemovalCallback()
  52. */
  53. template<class P1>
  54. class Callback1
  55. {
  56. public:
  57. //! constructor, taking lifetime control of body
  58. Callback1( Base_Callback1Body<P1>* pBody ) :
  59. m_pBody( pBody )
  60. {
  61. }
  62. //! copy constructor doing deep copy
  63. Callback1( const Callback1<P1>& callback ) :
  64. m_pBody( callback.m_pBody ? callback.m_pBody->clone() : NULL )
  65. {
  66. }
  67. //! destructor, destroying body
  68. ~Callback1( void )
  69. {
  70. delete m_pBody;
  71. m_pBody = NULL;
  72. }
  73. //! assignment operator
  74. Callback1<P1>& operator=( const Callback1<P1>& callback )
  75. {
  76. if (this != &callback)
  77. {
  78. delete m_pBody;
  79. m_pBody = callback.m_pBody ? callback.m_pBody->clone() : NULL;
  80. }
  81. return *this;
  82. }
  83. //! do operation defined in body
  84. void operator()( P1 p1 )
  85. {
  86. if (m_pBody)
  87. (*m_pBody)(p1);
  88. else
  89. assert( false && "Callback1:Empty body" );
  90. }
  91. private:
  92. Base_Callback1Body<P1>* m_pBody;
  93. };
  94. /***************************************************************************/
  95. // C Functions as callbacks
  96. /***************************************************************************/
  97. /*
  98. \brief Container for a function pointer
  99. This helper class is used to manage device removal callbacks.
  100. \see RegisterRemovalCallback()
  101. \see IPylonDevice::RegisterRemovalCallback()
  102. */
  103. template <class Function, class P>
  104. class Function_CallbackBody : public Base_Callback1Body<P>
  105. {
  106. public:
  107. //! Constructor
  108. Function_CallbackBody( const Function& function ) :
  109. m_pFunction( function )
  110. {
  111. }
  112. //! execute operation: call the function
  113. void operator()( P p ) const
  114. {
  115. if (m_pFunction)
  116. m_pFunction( p );
  117. }
  118. //! virtual copy constructor
  119. Function_CallbackBody<Function, P>* clone( void ) const
  120. {
  121. return new Function_CallbackBody<Function, P>( *this );
  122. }
  123. private:
  124. //! the callback function
  125. const Function m_pFunction;
  126. //! no assignment operator
  127. Function_CallbackBody& operator=( Function_CallbackBody& );
  128. };
  129. /*
  130. \brief Make a new callback object for C functions
  131. This helper function is used to manage device removal callbacks.
  132. Use one of the the Pylon::RegisterRemovalCallback() functions to
  133. register a callback that is fired in case of a device removal.
  134. \see IPylonDevice::RegisterRemovalCallback()
  135. \see RegisterRemovalCallback()
  136. */
  137. template <class Function, class Callback, class P>
  138. Callback make_FunctionCallback( Function function )
  139. {
  140. return new Function_CallbackBody<Function, P>( function );
  141. }
  142. /*-----------------------------------------------------------------------------*/
  143. /***************************************************************************/
  144. // C++ Member functions as callbacks
  145. /***************************************************************************/
  146. /*
  147. \brief Container for a member function pointer
  148. This helper class is used to manage device removal callbacks.
  149. \see RegisterRemovalCallback()
  150. \see IPylonDevice::RegisterRemovalCallback()
  151. */
  152. template <class Client, class Member, class P>
  153. class Member_CallbackBody : public Base_Callback1Body<P>
  154. {
  155. public:
  156. //! Member function type
  157. typedef void (Client::* PMEMBERFUNC)(P);
  158. //! Constructor
  159. Member_CallbackBody( Client& client, Member member ) :
  160. m_Client( client ),
  161. m_pMemberFunc( member )
  162. {
  163. }
  164. //! execute operation
  165. void operator()( P pP ) const
  166. {
  167. if (m_pMemberFunc)
  168. (m_Client.*m_pMemberFunc)(pP);
  169. }
  170. //! virtual copy constructor
  171. Member_CallbackBody<Client, Member, P>* clone() const
  172. {
  173. return new Member_CallbackBody<Client, Member, P>( m_Client, m_pMemberFunc );
  174. }
  175. private:
  176. //! The object the method function belongs to
  177. Client& m_Client;
  178. //! The method to call
  179. PMEMBERFUNC m_pMemberFunc;
  180. //! no assignment operator
  181. Member_CallbackBody& operator=( Member_CallbackBody& );
  182. //! no copy constructor
  183. Member_CallbackBody( const Member_CallbackBody& );
  184. };
  185. /*-----------------------------------------------------------------------------*/
  186. /*
  187. \brief Make a new callback object for member functions
  188. \brief Make a new callback object for C functions
  189. This helper function is used to manage device removal callbacks.
  190. Use one of the the Pylon::RegisterRemovalCallback() functions to
  191. register a callback that is fired in case of a device removal.
  192. \see RegisterRemovalCallback()
  193. \see IPylonDevice::RegisterRemovalCallback()
  194. */
  195. template <class Client, class Member, class Callback, class P>
  196. Callback make_MemberFunctionCallback( Client& client, Member member )
  197. {
  198. return Callback( static_cast<Base_Callback1Body<P>*>(new Member_CallbackBody<Client, Member, P>( client, member )) );
  199. }
  200. }
  201. #ifdef _MSC_VER
  202. # pragma pack(pop)
  203. #endif /* _MSC_VER */
  204. #endif