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.

334 lines
8.8 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: comutils.h
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: LazarI
  10. *
  11. * DATE: 23-Dec-2000
  12. *
  13. * DESCRIPTION: COM templates & utilities
  14. *
  15. *****************************************************************************/
  16. #ifndef _COMUTILS_H
  17. #define _COMUTILS_H
  18. // the generic smart pointers & handles
  19. #include "gensph.h"
  20. ////////////////////////////////////////////////
  21. // template class CUnknownMT<pQITable>
  22. //
  23. // milti-thread impl. of IUnknown
  24. // with interlocking for the ref-counting.
  25. //
  26. template <const QITAB* pQITable>
  27. class CUnknownMT
  28. {
  29. public:
  30. CUnknownMT(): m_cRefs(1) {}
  31. virtual ~CUnknownMT() {}
  32. //////////////////
  33. // IUnknown
  34. //
  35. STDMETHODIMP Handle_QueryInterface(REFIID riid, void **ppv)
  36. {
  37. return QISearch(this, pQITable, riid, ppv);
  38. }
  39. STDMETHODIMP_(ULONG) Handle_AddRef()
  40. {
  41. return InterlockedIncrement(&m_cRefs);
  42. }
  43. STDMETHODIMP_(ULONG) Handle_Release()
  44. {
  45. ULONG cRefs = InterlockedDecrement(&m_cRefs);
  46. if( 0 == cRefs )
  47. {
  48. delete this;
  49. }
  50. return cRefs;
  51. }
  52. private:
  53. LONG m_cRefs;
  54. };
  55. ////////////////////////////////////////////////
  56. // template class CUnknownST<pQITable>
  57. //
  58. // single-thread impl. of IUnknown
  59. // no interlocking for the ref-counting.
  60. //
  61. template <const QITAB* pQITable>
  62. class CUnknownST
  63. {
  64. public:
  65. CUnknownST(): m_cRefs(1) {}
  66. virtual ~CUnknownST() {}
  67. //////////////////
  68. // IUnknown
  69. //
  70. STDMETHODIMP Handle_QueryInterface(REFIID riid, void **ppv)
  71. {
  72. return QISearch(this, pQITable, riid, ppv);
  73. }
  74. STDMETHODIMP_(ULONG) Handle_AddRef()
  75. {
  76. return m_cRefs++;
  77. }
  78. STDMETHODIMP_(ULONG) Handle_Release()
  79. {
  80. if( 0 == --m_cRefs )
  81. {
  82. delete this;
  83. return 0;
  84. }
  85. return m_cRefs;
  86. }
  87. private:
  88. LONG m_cRefs;
  89. };
  90. #define QITABLE_DECLARE(className) \
  91. class className##_QITable \
  92. { \
  93. public: \
  94. static const QITAB qit[]; \
  95. }; \
  96. #define QITABLE_GET(className) \
  97. className##_QITable::qit \
  98. #define QITABLE_BEGIN(className) \
  99. const QITAB className##_QITable::qit[] = \
  100. { \
  101. #define QITABLE_END() \
  102. { 0 }, \
  103. }; \
  104. #define IMPLEMENT_IUNKNOWN() \
  105. STDMETHODIMP QueryInterface(REFIID riid, void **ppv) \
  106. { return Handle_QueryInterface(riid, ppv); } \
  107. STDMETHODIMP_(ULONG) AddRef() \
  108. { return Handle_AddRef(); } \
  109. STDMETHODIMP_(ULONG) Release() \
  110. { return Handle_Release(); } \
  111. #if FALSE
  112. ////////////////////////////////////////////////
  113. // template class CEnumFormatEtc
  114. //
  115. // implementation for an IDataObject which
  116. // supports SetData to different formats.
  117. // - not implemented yet.
  118. //
  119. class CEnumFormatEtc: public IEnumFORMATETC
  120. {
  121. public:
  122. CEnumFormatEtc(IUnknown *pUnkOuter, const FORMATETC *pfetc, UINT uSize);
  123. ~CEnumFormatEtc();
  124. //////////////////
  125. // IUnknown
  126. //
  127. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  128. STDMETHODIMP_(ULONG) AddRef();
  129. STDMETHODIMP_(ULONG) Release();
  130. //////////////////
  131. // IEnumFORMATETC
  132. //
  133. /* [local] */
  134. virtual HRESULT STDMETHODCALLTYPE Next(
  135. /* [in] */ ULONG celt,
  136. /* [length_is][size_is][out] */ FORMATETC *rgelt,
  137. /* [out] */ ULONG *pceltFetched) = 0;
  138. virtual HRESULT STDMETHODCALLTYPE Skip(
  139. /* [in] */ ULONG celt) = 0;
  140. virtual HRESULT STDMETHODCALLTYPE Reset( void) = 0;
  141. virtual HRESULT STDMETHODCALLTYPE Clone(
  142. /* [out] */ IEnumFORMATETC **ppenum) = 0;
  143. private:
  144. ULONG m_cRefs;
  145. IUnknown *m_pUnkOuter;
  146. LPFORMATETC m_prgfe;
  147. ULONG m_iCur;
  148. ULONG m_cItems;
  149. };
  150. #endif // #if FALSE
  151. ////////////////////////////////////////////////
  152. // template class CDataObj<MAX_FORMATS>
  153. //
  154. // implementation for an IDataObject which
  155. // supports SetData to different formats.
  156. //
  157. template <int MAX_FORMATS = 32>
  158. class CDataObj: public IDataObject
  159. {
  160. public:
  161. // construction/destruction
  162. CDataObj();
  163. ~CDataObj();
  164. //////////////////
  165. // IUnknown
  166. //
  167. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  168. STDMETHODIMP_(ULONG) AddRef();
  169. STDMETHODIMP_(ULONG) Release();
  170. //////////////////
  171. // IDataObject
  172. //
  173. /* [local] */
  174. virtual HRESULT STDMETHODCALLTYPE GetData(
  175. /* [unique][in] */ FORMATETC *pformatetcIn,
  176. /* [out] */ STGMEDIUM *pmedium);
  177. /* [local] */
  178. virtual HRESULT STDMETHODCALLTYPE GetDataHere(
  179. /* [unique][in] */ FORMATETC *pformatetc,
  180. /* [out][in] */ STGMEDIUM *pmedium);
  181. virtual HRESULT STDMETHODCALLTYPE QueryGetData(
  182. /* [unique][in] */ FORMATETC *pformatetc);
  183. virtual HRESULT STDMETHODCALLTYPE GetCanonicalFormatEtc(
  184. /* [unique][in] */ FORMATETC *pformatectIn,
  185. /* [out] */ FORMATETC *pformatetcOut);
  186. /* [local] */
  187. virtual HRESULT STDMETHODCALLTYPE SetData(
  188. /* [unique][in] */ FORMATETC *pformatetc,
  189. /* [unique][in] */ STGMEDIUM *pmedium,
  190. /* [in] */ BOOL fRelease);
  191. virtual HRESULT STDMETHODCALLTYPE EnumFormatEtc(
  192. /* [in] */ DWORD dwDirection,
  193. /* [out] */ IEnumFORMATETC **ppenumFormatEtc);
  194. virtual HRESULT STDMETHODCALLTYPE DAdvise(
  195. /* [in] */ FORMATETC *pformatetc,
  196. /* [in] */ DWORD advf,
  197. /* [unique][in] */ IAdviseSink *pAdvSink,
  198. /* [out] */ DWORD *pdwConnection);
  199. virtual HRESULT STDMETHODCALLTYPE DUnadvise(
  200. /* [in] */ DWORD dwConnection);
  201. virtual HRESULT STDMETHODCALLTYPE EnumDAdvise(
  202. /* [out] */ IEnumSTATDATA **ppenumAdvise);
  203. private:
  204. LONG m_cRefs;
  205. FORMATETC m_fmte[MAX_FORMATS];
  206. STGMEDIUM m_medium[MAX_FORMATS];
  207. };
  208. ////////////////////////////////////////////////
  209. // template class CSimpleDataObjImpl<T>
  210. //
  211. // simple implementation for an IDataObject
  212. // and IDropSource which lives in memory.
  213. //
  214. template <class T>
  215. class CSimpleDataObjImpl: public IDataObject,
  216. public IDropSource
  217. {
  218. public:
  219. // construction/destruction
  220. CSimpleDataObjImpl(const T &data, CLIPFORMAT cfDataType, IDataObject *pDataObj = NULL);
  221. ~CSimpleDataObjImpl();
  222. //////////////////
  223. // IUnknown
  224. //
  225. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  226. STDMETHODIMP_(ULONG) AddRef();
  227. STDMETHODIMP_(ULONG) Release();
  228. //////////////////
  229. // IDataObject
  230. //
  231. /* [local] */
  232. virtual HRESULT STDMETHODCALLTYPE GetData(
  233. /* [unique][in] */ FORMATETC *pformatetcIn,
  234. /* [out] */ STGMEDIUM *pmedium);
  235. /* [local] */
  236. virtual HRESULT STDMETHODCALLTYPE GetDataHere(
  237. /* [unique][in] */ FORMATETC *pformatetc,
  238. /* [out][in] */ STGMEDIUM *pmedium);
  239. virtual HRESULT STDMETHODCALLTYPE QueryGetData(
  240. /* [unique][in] */ FORMATETC *pformatetc);
  241. virtual HRESULT STDMETHODCALLTYPE GetCanonicalFormatEtc(
  242. /* [unique][in] */ FORMATETC *pformatectIn,
  243. /* [out] */ FORMATETC *pformatetcOut);
  244. /* [local] */
  245. virtual HRESULT STDMETHODCALLTYPE SetData(
  246. /* [unique][in] */ FORMATETC *pformatetc,
  247. /* [unique][in] */ STGMEDIUM *pmedium,
  248. /* [in] */ BOOL fRelease);
  249. virtual HRESULT STDMETHODCALLTYPE EnumFormatEtc(
  250. /* [in] */ DWORD dwDirection,
  251. /* [out] */ IEnumFORMATETC **ppenumFormatEtc);
  252. virtual HRESULT STDMETHODCALLTYPE DAdvise(
  253. /* [in] */ FORMATETC *pformatetc,
  254. /* [in] */ DWORD advf,
  255. /* [unique][in] */ IAdviseSink *pAdvSink,
  256. /* [out] */ DWORD *pdwConnection);
  257. virtual HRESULT STDMETHODCALLTYPE DUnadvise(
  258. /* [in] */ DWORD dwConnection);
  259. virtual HRESULT STDMETHODCALLTYPE EnumDAdvise(
  260. /* [out] */ IEnumSTATDATA **ppenumAdvise);
  261. //////////////////
  262. // IDropSource
  263. //
  264. virtual HRESULT STDMETHODCALLTYPE QueryContinueDrag(
  265. /* [in] */ BOOL fEscapePressed,
  266. /* [in] */ DWORD grfKeyState);
  267. virtual HRESULT STDMETHODCALLTYPE GiveFeedback(
  268. /* [in] */ DWORD dwEffect);
  269. private:
  270. LONG m_cRefs;
  271. T m_data;
  272. CLIPFORMAT m_cfDataType;
  273. CRefPtrCOM<IDataObject> m_spDataObj;
  274. };
  275. // this namespace is a placeholder to put COM related helpers here
  276. namespace comhelpers
  277. {
  278. BOOL AreObjectsIdentical(IUnknown *punk1, IUnknown *punk2);
  279. }
  280. // include the implementation of the template classes here
  281. #include "comutils.inl"
  282. #endif // endif _COMUTILS_H