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.

290 lines
5.7 KiB

  1. //---[ tunk.h ]----------------------------------------------------------------
  2. //
  3. // Template for IUnknown<T>
  4. //
  5. //-----------------------------------------------------------------------------
  6. #pragma once
  7. #ifndef _TUNK_H_
  8. #define _TUNK_H_
  9. template<class T>
  10. class TUnknown : public T
  11. {
  12. public:
  13. TUnknown (REFIID iid) : m_nRef(1), m_iid(iid) {}
  14. virtual ~TUnknown() {}
  15. virtual ULONG __stdcall AddRef()
  16. { return ++m_nRef; }
  17. virtual ULONG __stdcall Release()
  18. {
  19. int nRef = --m_nRef;
  20. if (!nRef)
  21. delete this;
  22. return nRef;
  23. }
  24. virtual HRESULT __stdcall QueryInterface(REFIID iid, void ** lppv)
  25. {
  26. if ((iid == IID_IUnknown) ||
  27. (iid == m_iid))
  28. {
  29. *lppv = this;
  30. AddRef();
  31. return S_OK;
  32. }
  33. else
  34. {
  35. *lppv = NULL;
  36. return E_NOINTERFACE;
  37. }
  38. }
  39. protected:
  40. int m_nRef;
  41. REFIID m_iid;
  42. };
  43. template<class T1, class T2>
  44. class TUnknown2 : public T1, public T2
  45. {
  46. public:
  47. TUnknown2 (REFIID iid, REFIID iid2) : m_nRef(1), m_iid(iid), m_iid2(iid2) {}
  48. virtual ~TUnknown2() {}
  49. virtual ULONG __stdcall AddRef()
  50. { return ++m_nRef; }
  51. virtual ULONG __stdcall Release()
  52. {
  53. int nRef = --m_nRef;
  54. if (!nRef)
  55. delete this;
  56. return nRef;
  57. }
  58. virtual HRESULT __stdcall QueryInterface(REFIID iid, void ** lppv)
  59. {
  60. if ((iid == IID_IUnknown) ||
  61. (iid == m_iid))
  62. {
  63. *lppv = (LPUNKNOWN)(T1*)this;
  64. AddRef();
  65. return S_OK;
  66. }
  67. else if (iid == m_iid2)
  68. {
  69. *lppv = (LPUNKNOWN)(T2*)this;
  70. AddRef();
  71. return S_OK;
  72. }
  73. else
  74. {
  75. *lppv = NULL;
  76. return S_FALSE;
  77. }
  78. }
  79. protected:
  80. int m_nRef;
  81. REFIID m_iid;
  82. REFIID m_iid2;
  83. };
  84. template<class T1, class T2, class T3>
  85. class TUnknown3 : public T1, public T2, public T3
  86. {
  87. public:
  88. TUnknown3 (REFIID iid, REFIID iid2, REFIID iid3) : m_nRef(1), m_iid(iid), m_iid2(iid2), m_iid3(iid3) {}
  89. virtual ~TUnknown3() {}
  90. virtual ULONG __stdcall AddRef()
  91. { return ++m_nRef; }
  92. virtual ULONG __stdcall Release()
  93. {
  94. int nRef = --m_nRef;
  95. if (!nRef)
  96. delete this;
  97. return nRef;
  98. }
  99. virtual HRESULT __stdcall QueryInterface(REFIID iid, void ** lppv)
  100. {
  101. if ((iid == IID_IUnknown) ||
  102. (iid == m_iid))
  103. {
  104. *lppv = (LPUNKNOWN)(T1*)this;
  105. AddRef();
  106. return S_OK;
  107. }
  108. else if (iid == m_iid2)
  109. {
  110. *lppv = (LPUNKNOWN)(T2*)this;
  111. AddRef();
  112. return S_OK;
  113. }
  114. else if (iid == m_iid3)
  115. {
  116. *lppv = (LPUNKNOWN)(T3*)this;
  117. AddRef();
  118. return S_OK;
  119. }
  120. else
  121. {
  122. *lppv = NULL;
  123. return S_FALSE;
  124. }
  125. }
  126. protected:
  127. int m_nRef;
  128. REFIID m_iid;
  129. REFIID m_iid2;
  130. REFIID m_iid3;
  131. };
  132. template<class T1, class T2, class T3, class T4>
  133. class TUnknown4 : public T1, public T2, public T3, public T4
  134. {
  135. public:
  136. TUnknown4 (REFIID iid, REFIID iid2, REFIID iid3, REFIID iid4) :
  137. m_nRef(1), m_iid(iid), m_iid2(iid2), m_iid3(iid3), m_iid4(iid4) {}
  138. virtual ~TUnknown4() {}
  139. virtual ULONG __stdcall AddRef()
  140. { return ++m_nRef; }
  141. virtual ULONG __stdcall Release()
  142. {
  143. int nRef = --m_nRef;
  144. if (!nRef)
  145. delete this;
  146. return nRef;
  147. }
  148. virtual HRESULT __stdcall QueryInterface(REFIID iid, void ** lppv)
  149. {
  150. if ((iid == IID_IUnknown) ||
  151. (iid == m_iid))
  152. {
  153. *lppv = (LPUNKNOWN)(T1*)this;
  154. AddRef();
  155. return S_OK;
  156. }
  157. else if (iid == m_iid2)
  158. {
  159. *lppv = (LPUNKNOWN)(T2*)this;
  160. AddRef();
  161. return S_OK;
  162. }
  163. else if (iid == m_iid3)
  164. {
  165. *lppv = (LPUNKNOWN)(T3*)this;
  166. AddRef();
  167. return S_OK;
  168. }
  169. else if (iid == m_iid4)
  170. {
  171. *lppv = (LPUNKNOWN)(T4*)this;
  172. AddRef();
  173. return S_OK;
  174. }
  175. else
  176. {
  177. *lppv = NULL;
  178. return S_FALSE;
  179. }
  180. }
  181. protected:
  182. int m_nRef;
  183. REFIID m_iid;
  184. REFIID m_iid2;
  185. REFIID m_iid3;
  186. REFIID m_iid4;
  187. };
  188. template<class T1, class T2, class T3, class T4, class T5>
  189. class TUnknown5 : public T1, public T2, public T3, public T4, public T5
  190. {
  191. public:
  192. TUnknown5 (REFIID iid, REFIID iid2, REFIID iid3, REFIID iid4, REFIID iid5) :
  193. m_nRef(1), m_iid(iid), m_iid2(iid2), m_iid3(iid3), m_iid4(iid4), m_iid5(iid5) {}
  194. virtual ~TUnknown5() {}
  195. virtual ULONG __stdcall AddRef()
  196. { return ++m_nRef; }
  197. virtual ULONG __stdcall Release()
  198. {
  199. int nRef = --m_nRef;
  200. if (!nRef)
  201. delete this;
  202. return nRef;
  203. }
  204. virtual HRESULT __stdcall QueryInterface(REFIID iid, void ** lppv)
  205. {
  206. if ((iid == IID_IUnknown) ||
  207. (iid == m_iid))
  208. {
  209. *lppv = (LPUNKNOWN)(T1*)this;
  210. AddRef();
  211. return S_OK;
  212. }
  213. else if (iid == m_iid2)
  214. {
  215. *lppv = (LPUNKNOWN)(T2*)this;
  216. AddRef();
  217. return S_OK;
  218. }
  219. else if (iid == m_iid3)
  220. {
  221. *lppv = (LPUNKNOWN)(T3*)this;
  222. AddRef();
  223. return S_OK;
  224. }
  225. else if (iid == m_iid4)
  226. {
  227. *lppv = (LPUNKNOWN)(T4*)this;
  228. AddRef();
  229. return S_OK;
  230. }
  231. else if (iid == m_iid5)
  232. {
  233. *lppv = (LPUNKNOWN)(T5*)this;
  234. AddRef();
  235. return S_OK;
  236. }
  237. else
  238. {
  239. *lppv = NULL;
  240. return S_FALSE;
  241. }
  242. }
  243. protected:
  244. int m_nRef;
  245. REFIID m_iid;
  246. REFIID m_iid2;
  247. REFIID m_iid3;
  248. REFIID m_iid4;
  249. REFIID m_iid5;
  250. };
  251. #endif // _TUNK_H_