Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
6.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasapix.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file describes the IAS C++ API.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 11/10/1997 Original version.
  16. // 01/08/1998 Renamed to iasapix.h (was iascback.h).
  17. // 08/10/1998 Remove obsolete IASRegMap.
  18. //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. #ifndef _IASAPIX_H_
  21. #define _IASAPIX_H_
  22. #ifndef _IASAPI_H_
  23. #error iasapi.h must be included first.
  24. #endif
  25. #ifdef __cplusplus
  26. ///////////////////////////////////////////////////////////////////////////////
  27. //
  28. // MACRO
  29. //
  30. // IAS_DECLARE_REGISTRY
  31. //
  32. // DESCRIPTION
  33. //
  34. // Macro that allows ATL COM components to use the IASRegisterComponent
  35. // API.
  36. //
  37. ///////////////////////////////////////////////////////////////////////////////
  38. #define IAS_DECLARE_REGISTRY(coclass, ver, flags, tlb) \
  39. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) \
  40. { \
  41. return IASRegisterComponent(_Module.GetModuleInstance(), \
  42. __uuidof(coclass), \
  43. IASProgramName, \
  44. L ## #coclass, \
  45. flags, \
  46. __uuidof(tlb), \
  47. ver, \
  48. 0, \
  49. bRegister); \
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. //
  53. // STRUCT
  54. //
  55. // Callback
  56. //
  57. // DESCRIPTION
  58. //
  59. // This servers as the base class for all callback objects. The
  60. // CallbackRoutine member serves as a mini-vtable to provide polymorphism.
  61. // I chose not to use a virtual function for two reasons: C compatibility
  62. // and to make the IAS_CALLBACK struct as lightweight as possible. The
  63. // Callback objects make use of the small block allocator, so all
  64. // derived classes must be smaller than IAS_SMALLBLOCK_SIZE.
  65. //
  66. ///////////////////////////////////////////////////////////////////////////////
  67. struct Callback : IAS_CALLBACK
  68. {
  69. Callback(VOID (WINAPI *pFn)(Callback*))
  70. { CallbackRoutine = (IAS_CALLBACK_ROUTINE)pFn; }
  71. void operator()(){ CallbackRoutine(this); }
  72. };
  73. ///////////////////////////////////////////////////////////////////////////////
  74. //
  75. // STRUCT
  76. //
  77. // cback_fun_t, cback_fun1_t, cback_mem_t, cback_mem1_t
  78. //
  79. // DESCRIPTION
  80. //
  81. // Various templated classes that extend Callback to implement the most
  82. // common scenarios.
  83. //
  84. // These classes should not be instantiated directly. Use the MakeCallback
  85. // and MakeBoundCallback functions instead (see below).
  86. //
  87. ///////////////////////////////////////////////////////////////////////////////
  88. template <class Fn>
  89. struct cback_fun_t : Callback
  90. {
  91. typedef cback_fun_t<Fn> _Myt;
  92. cback_fun_t(Fn pFn)
  93. : Callback(CallbackRoutine), m_pFn(pFn) { }
  94. void operator()() { m_pFn(); }
  95. protected:
  96. Fn m_pFn;
  97. static VOID WINAPI CallbackRoutine(Callback* This)
  98. { (*((_Myt*)This))(); delete This; }
  99. };
  100. template <class Fn, class A>
  101. struct cback_fun1_t : Callback
  102. {
  103. typedef cback_fun1_t<Fn, A> _Myt;
  104. cback_fun1_t(Fn pFn, A vArg)
  105. : Callback(CallbackRoutine), m_pFn(pFn), m_vArg(vArg) { }
  106. void operator()() { m_pFn(m_vArg); }
  107. protected:
  108. Fn m_pFn;
  109. A m_vArg;
  110. static VOID WINAPI CallbackRoutine(Callback* This)
  111. { (*((_Myt*)This))(); delete This; }
  112. };
  113. template <class Ty, class Fn>
  114. struct cback_mem_t : Callback
  115. {
  116. typedef cback_mem_t<Ty, Fn> _Myt;
  117. cback_mem_t(Ty* pObj, Fn pFn)
  118. : Callback(CallbackRoutine), m_pObj(pObj), m_pFn(pFn) { }
  119. void operator()() { (m_pObj->*m_pFn)(); }
  120. protected:
  121. Ty* m_pObj;
  122. Fn m_pFn;
  123. static VOID WINAPI CallbackRoutine(Callback* This)
  124. { (*((_Myt*)This))(); delete This; }
  125. };
  126. template <class Ty, class Fn, class A>
  127. struct cback_mem1_t : Callback
  128. {
  129. typedef cback_mem1_t<Ty, Fn, A> _Myt;
  130. cback_mem1_t(Ty* pObj, Fn pFn, A vArg)
  131. : Callback(CallbackRoutine), m_pObj(pObj), m_pFn(pFn), m_vArg(vArg) { }
  132. void operator()() { (m_pObj->*m_pFn)(m_vArg); }
  133. protected:
  134. Ty* m_pObj;
  135. A m_vArg; // Put m_vArg second since it might require 8 byte alignment.
  136. Fn m_pFn;
  137. static VOID WINAPI CallbackRoutine(Callback* This)
  138. { (*((_Myt*)This))(); delete This; }
  139. };
  140. ///////////////////////////////////////////////////////////////////////////////
  141. //
  142. // FUNCTION
  143. //
  144. // MakeCallback
  145. //
  146. // DESCRIPTION
  147. //
  148. // This overloaded function provides an interface for constructing a
  149. // Callback object that invokes a non-member function. The function can
  150. // use any calling convention, have any return type, and take zero or one
  151. // arguments. The argument must have a copy constructor and be eight bytes
  152. // or less.
  153. //
  154. // The callback will be deleted automatically when it is invoked.
  155. // Otherwise, the caller is responsible for deleting the object.
  156. //
  157. // EXAMPLE
  158. //
  159. // class Foo
  160. // {
  161. // public:
  162. // static void Bar(DWORD dwSomeArg);
  163. // };
  164. //
  165. // DWORD dwArg = 12;
  166. // Callback* cback = MakeCallback(&Foo::Bar, dwArg);
  167. //
  168. ///////////////////////////////////////////////////////////////////////////////
  169. template <class Fn>
  170. inline Callback* MakeCallback(Fn pFn)
  171. {
  172. return new cback_fun_t<Fn>(pFn);
  173. }
  174. template <class Fn, class A>
  175. inline Callback* MakeCallback(Fn pFn, A vArg)
  176. {
  177. return new cback_fun1_t<Fn, A>(pFn, vArg);
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////
  180. //
  181. // FUNCTION
  182. //
  183. // MakeBoundCallback
  184. //
  185. // DESCRIPTION
  186. //
  187. // This overloaded function provides an interface for constructing a
  188. // Callback object that invokes a bound (member) function. The function
  189. // can use any calling convention, have any return type, and take zero or
  190. // one arguments. The argument must have a copy constructor and be eight
  191. // bytes or less.
  192. //
  193. // The callback will be deleted automatically when it is invoked.
  194. // Otherwise, the caller is responsible for deleting the object.
  195. //
  196. // EXAMPLE
  197. //
  198. // class Foo
  199. // {
  200. // public:
  201. // void Bar(DWORD dwSomeArg);
  202. // } foo;
  203. //
  204. // DWORD dwArg = 12;
  205. // Callback* cback = MakeCallback(&foo, &Foo::Bar, dwArg);
  206. //
  207. ///////////////////////////////////////////////////////////////////////////////
  208. template <class Ty, class Fn>
  209. inline Callback* MakeBoundCallback(Ty* pObj, Fn pFn)
  210. {
  211. return new cback_mem_t<Ty, Fn>(pObj, pFn);
  212. }
  213. template <class Ty, class Fn, class A>
  214. inline Callback* MakeBoundCallback(Ty* pObj, Fn pFn, A vArg)
  215. {
  216. return new cback_mem1_t<Ty, Fn, A>(pObj, pFn, vArg);
  217. }
  218. #endif // __cplusplus
  219. #endif // _IASAPIX_H_