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.

480 lines
12 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. /////////////////////////////////////////////////////////////////////////////
  11. // AFXCOM_.H
  12. //
  13. // THIS FILE IS FOR MFC IMPLEMENTATION ONLY.
  14. #ifndef __AFXCOM_H__
  15. #define __AFXCOM_H__
  16. #ifndef _OBJBASE_H_
  17. #include <objbase.h>
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. #ifdef _AFX_MINREBUILD
  21. #pragma component(minrebuild, off)
  22. #endif
  23. #ifndef _AFX_FULLTYPEINFO
  24. #pragma component(mintypeinfo, on)
  25. #endif
  26. /////////////////////////////////////////////////////////////////////////////
  27. #ifndef _AFX_NOFORCE_LIBS
  28. #pragma comment(lib, "uuid.lib")
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. #ifdef _AFX_PACKING
  32. #pragma pack(push, _AFX_PACKING)
  33. #endif
  34. #ifndef ASSERT
  35. #ifndef _INC_CRTDBG
  36. #include <crtdbg.h>
  37. #endif // _INC_CRTDBG
  38. #define ASSERT(x) _ASSERT(x)
  39. #endif // ASSERT
  40. /////////////////////////////////////////////////////////////////////////////
  41. template<class _Interface, const IID* _IID>
  42. class _CIP
  43. {
  44. public:
  45. // Declare interface type so that the type may be available outside
  46. // the scope of this template.
  47. typedef _Interface Interface;
  48. // When the compiler supports references in template params,
  49. // _CLSID will be changed to a reference. To avoid conversion
  50. // difficulties this function should be used to obtain the
  51. // CLSID.
  52. static const IID& GetIID()
  53. { ASSERT(_IID != NULL); return *_IID; }
  54. // Construct empty in preperation for assignment.
  55. _CIP();
  56. // Copy the pointer and AddRef().
  57. _CIP(const _CIP& cp) : _pInterface(cp._pInterface)
  58. { _AddRef(); }
  59. // Saves and AddRef()'s the interface
  60. _CIP(Interface* pInterface) : _pInterface(pInterface)
  61. { _AddRef(); }
  62. // Copies the pointer. If bAddRef is TRUE, the interface will
  63. // be AddRef()ed.
  64. _CIP(Interface* pInterface, BOOL bAddRef)
  65. : _pInterface(pInterface)
  66. {
  67. if (bAddRef)
  68. {
  69. ASSERT(pInterface != NULL);
  70. _AddRef();
  71. }
  72. }
  73. // Calls CoCreateClass with the provided CLSID.
  74. _CIP(const CLSID& clsid, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
  75. : _pInterface(NULL)
  76. {
  77. CreateObject(clsid, dwClsContext);
  78. }
  79. // Calls CoCreateClass with the provided CLSID retrieved from
  80. // the string.
  81. _CIP(LPOLESTR str, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
  82. : _pInterface(NULL)
  83. {
  84. CreateObject(str, dwClsContext);
  85. }
  86. // Saves and AddRef()s the interface.
  87. _CIP& operator=(Interface* pInterface)
  88. {
  89. if (_pInterface != pInterface)
  90. {
  91. Interface* pOldInterface = _pInterface;
  92. _pInterface = pInterface;
  93. _AddRef();
  94. if (pOldInterface != NULL)
  95. pOldInterface->Release();
  96. }
  97. return *this;
  98. }
  99. // Copies and AddRef()'s the interface.
  100. _CIP& operator=(const _CIP& cp)
  101. { return operator=(cp._pInterface); }
  102. // Releases any current interface and loads the class with the
  103. // provided CLSID.
  104. _CIP& operator=(const CLSID& clsid)
  105. {
  106. CreateObject(clsid);
  107. return *this;
  108. }
  109. // Calls CoCreateClass with the provided CLSID retrieved from
  110. // the string.
  111. _CIP& operator=(LPOLESTR str)
  112. {
  113. CreateObject(str);
  114. return *this;
  115. }
  116. ~_CIP();
  117. // Saves/sets the interface without AddRef()ing. This call
  118. // will release any previously aquired interface.
  119. void Attach(Interface* pInterface)
  120. {
  121. _Release();
  122. _pInterface = pInterface;
  123. }
  124. // Saves/sets the interface only AddRef()ing if bAddRef is TRUE.
  125. // This call will release any previously aquired interface.
  126. void Attach(Interface* pInterface, BOOL bAddRef)
  127. {
  128. _Release();
  129. _pInterface = pInterface;
  130. if (bAddRef)
  131. {
  132. ASSERT(pInterface != NULL);
  133. pInterface->AddRef();
  134. }
  135. }
  136. // Simply NULL the interface pointer so that it isn't Released()'ed.
  137. void Detach()
  138. {
  139. ASSERT(_pInterface);
  140. _pInterface = NULL;
  141. }
  142. // Return the interface. This value may be NULL
  143. operator Interface*() const
  144. { return _pInterface; }
  145. // Queries for the unknown and return it
  146. operator IUnknown*()
  147. { return _pInterface; }
  148. // Provides minimal level assertion before use.
  149. operator Interface&() const
  150. { ASSERT(_pInterface); return *_pInterface; }
  151. // Allows an instance of this class to act as though it were the
  152. // actual interface. Also provides minimal assertion verification.
  153. Interface& operator*() const
  154. { ASSERT(_pInterface); return *_pInterface; }
  155. // Returns the address of the interface pointer contained in this
  156. // class. This is useful when using the COM/OLE interfaces to create
  157. // this interface.
  158. Interface** operator&()
  159. {
  160. _Release();
  161. _pInterface = NULL;
  162. return &_pInterface;
  163. }
  164. // Allows this class to be used as the interface itself.
  165. // Also provides simple assertion verification.
  166. Interface* operator->() const
  167. { ASSERT(_pInterface != NULL); return _pInterface; }
  168. // This operator is provided so that simple boolean expressions will
  169. // work. For example: "if (p) ...".
  170. // Returns TRUE if the pointer is not NULL.
  171. operator BOOL() const
  172. { return _pInterface != NULL; }
  173. // Returns TRUE if the interface is NULL.
  174. // This operator will be removed when support for type bool
  175. // is added to the compiler.
  176. BOOL operator!()
  177. { return _pInterface == NULL; }
  178. // Provides assertion verified, Release()ing of this interface.
  179. void Release()
  180. {
  181. ASSERT(_pInterface != NULL);
  182. _pInterface->Release();
  183. _pInterface = NULL;
  184. }
  185. // Provides assertion verified AddRef()ing of this interface.
  186. void AddRef()
  187. { ASSERT(_pInterface != NULL); _pInterface->AddRef(); }
  188. // Another way to get the interface pointer without casting.
  189. Interface* GetInterfacePtr() const
  190. { return _pInterface; }
  191. // Loads an interface for the provided CLSID.
  192. // Returns an HRESULT. Any previous interface is released.
  193. HRESULT CreateObject(
  194. const CLSID& clsid, DWORD dwClsContext=CLSCTX_INPROC_SERVER)
  195. {
  196. _Release();
  197. HRESULT hr = CoCreateInstance(clsid, NULL, dwClsContext,
  198. GetIID(), reinterpret_cast<void**>(&_pInterface));
  199. ASSERT(SUCCEEDED(hr));
  200. return hr;
  201. }
  202. // Creates the class specified by clsidString. clsidString may
  203. // contain a class id, or a prog id string.
  204. HRESULT CreateObject(
  205. LPOLESTR clsidString, DWORD dwClsContext=CLSCTX_INPROC_SERVER)
  206. {
  207. ASSERT(clsidString != NULL);
  208. CLSID clsid;
  209. HRESULT hr;
  210. if (clsidString[0] == '{')
  211. hr = CLSIDFromString(clsidString, &clsid);
  212. else
  213. hr = CLSIDFromProgID(clsidString, &clsid);
  214. ASSERT(SUCCEEDED(hr));
  215. if (FAILED(hr))
  216. return hr;
  217. return CreateObject(clsid, dwClsContext);
  218. }
  219. // Performs a QI on pUnknown for the interface type returned
  220. // for this class. The interface is stored. If pUnknown is
  221. // NULL, or the QI fails, E_NOINTERFACE is returned and
  222. // _pInterface is set to NULL.
  223. HRESULT QueryInterface(IUnknown* pUnknown)
  224. {
  225. if (pUnknown == NULL) // Can't QI NULL
  226. {
  227. operator=(static_cast<Interface*>(NULL));
  228. return E_NOINTERFACE;
  229. }
  230. // Query for this interface
  231. Interface* pInterface;
  232. HRESULT hr = pUnknown->QueryInterface(GetIID(),
  233. reinterpret_cast<void**>(&pInterface));
  234. if (FAILED(hr))
  235. {
  236. // If failed intialize interface to NULL and return HRESULT.
  237. Attach(NULL);
  238. return hr;
  239. }
  240. // Save the interface without AddRef()ing.
  241. Attach(pInterface);
  242. return hr;
  243. }
  244. private:
  245. // Releases only if the interface is not null.
  246. // The interface is not set to NULL.
  247. void _Release()
  248. {
  249. if (_pInterface != NULL)
  250. _pInterface->Release();
  251. }
  252. // AddRefs only if the interface is not NULL
  253. void _AddRef()
  254. {
  255. if (_pInterface != NULL)
  256. _pInterface->AddRef();
  257. }
  258. // The Interface.
  259. Interface* _pInterface;
  260. }; // class _CIP
  261. template<class _Interface, const IID* _IID>
  262. _CIP<_Interface, _IID>::_CIP<_Interface, _IID>()
  263. : _pInterface(NULL)
  264. {
  265. }
  266. template<class _Interface, const IID* _IID>
  267. _CIP<_Interface, _IID>::~_CIP<_Interface, _IID>()
  268. {
  269. // If we still have an interface then Release() it. The interface
  270. // may be NULL if Detach() has previosly been called, or if it was
  271. // never set.
  272. _Release();
  273. }
  274. template<class _Interface, const IID* _IID>
  275. class CIP : public _CIP<_Interface, _IID>
  276. {
  277. public:
  278. // Simplified name for base class and provide derived classes
  279. // access to base type
  280. typedef _CIP<_Interface, _IID> BC;
  281. // Provideds derived classes access to the interface type.
  282. typedef _Interface Interface;
  283. // Construct empty in preperation for assignment.
  284. CIP() { }
  285. ~CIP();
  286. // Copy the pointer and AddRef().
  287. CIP(const CIP& cp) : _CIP<_Interface, _IID>(cp) { }
  288. // Saves and AddRef()s the interface.
  289. CIP(Interface* pInterface) : _CIP<_Interface, _IID>(pInterface) { }
  290. // Saves the interface and AddRef()s only if bAddRef is TRUE.
  291. CIP(Interface* pInterface, BOOL bAddRef)
  292. : _CIP<_Interface, _IID>(pInterface, bAddRef) { }
  293. // Queries for this interface.
  294. CIP(IUnknown* pUnknown)
  295. {
  296. if (pUnknown == NULL)
  297. return;
  298. Interface* pInterface;
  299. HRESULT hr = pUnknown->QueryInterface(GetIID(),
  300. reinterpret_cast<void**>(&pInterface));
  301. ASSERT(SUCCEEDED(hr));
  302. Attach(pInterface);
  303. }
  304. // Creates the interface from the CLSID.
  305. CIP(const CLSID& clsid) : _CIP<_Interface, _IID>(clsid) { }
  306. // Creates the interface from the CLSID.
  307. CIP(LPOLESTR str) : _CIP<_Interface, _IID>(str) { }
  308. // Copies and AddRef()'s the interface.
  309. CIP& operator=(const CIP& cp)
  310. { _CIP<_Interface, _IID>::operator=(cp); return *this; }
  311. // Saves and AddRef()s the interface.
  312. CIP& operator=(Interface* pInterface)
  313. { _CIP<_Interface, _IID>::operator=(pInterface); return *this; }
  314. CIP& operator=(IUnknown* pUnknown)
  315. {
  316. HRESULT hr = QueryInterface(pUnknown);
  317. ASSERT(SUCCEEDED(hr));
  318. return *this;
  319. }
  320. // Releases any current interface and loads the class with the
  321. // provided CLSID.
  322. CIP& operator=(const CLSID& clsid)
  323. { _CIP<_Interface, _IID>::operator=(clsid); return *this; }
  324. // Releases any current interface and loads the class with the
  325. // provided CLSID.
  326. CIP& operator=(LPOLESTR str)
  327. { _CIP<_Interface, _IID>::operator=(str); return *this; }
  328. }; // class CIP
  329. template<class _Interface, const IID* _IID>
  330. CIP<_Interface, _IID>::~CIP()
  331. {
  332. }
  333. #if _MSC_VER>1020
  334. template<>
  335. #endif
  336. class CIP<IUnknown, &IID_IUnknown> : public _CIP<IUnknown, &IID_IUnknown>
  337. {
  338. public:
  339. // Simplified name for base class and provide derived classes
  340. // access to base type
  341. typedef _CIP<IUnknown, &IID_IUnknown> BC;
  342. // Provideds derived classes access to the interface type.
  343. typedef IUnknown Interface;
  344. // Construct empty in preperation for assignment.
  345. CIP() { }
  346. // Copy the pointer and AddRef().
  347. CIP(const CIP& cp) : _CIP<IUnknown, &IID_IUnknown>(cp) { }
  348. // Saves and AddRef()s the interface.
  349. CIP(Interface* pInterface)
  350. : _CIP<IUnknown, &IID_IUnknown>(pInterface) { }
  351. // Saves and then AddRef()s only if bAddRef is TRUE.
  352. CIP(Interface* pInterface, BOOL bAddRef)
  353. : _CIP<IUnknown, &IID_IUnknown>(pInterface, bAddRef) { }
  354. // Creates the interface from the CLSID.
  355. CIP(const CLSID& clsid) : _CIP<IUnknown, &IID_IUnknown>(clsid) { }
  356. // Creates the interface from the CLSID.
  357. CIP(LPOLESTR str) : _CIP<IUnknown, &IID_IUnknown>(str) { }
  358. // Copies and AddRef()'s the interface.
  359. CIP& operator=(const CIP& cp)
  360. { _CIP<IUnknown, &IID_IUnknown>::operator=(cp); return *this; }
  361. // Saves and AddRef()s the interface. The previously saved
  362. // interface is released.
  363. CIP& operator=(Interface* pInterface)
  364. { _CIP<IUnknown, &IID_IUnknown>::operator=(pInterface); return *this; }
  365. // Releases any current interface and loads the class with the
  366. // provided CLSID.
  367. CIP& operator=(const CLSID& clsid)
  368. { _CIP<IUnknown, &IID_IUnknown>::operator=(clsid); return *this; }
  369. // Releases any current interface and loads the class with the
  370. // provided CLSID.
  371. CIP& operator=(LPOLESTR str)
  372. { _CIP<IUnknown, &IID_IUnknown>::operator=(str); return *this; }
  373. // Queries for the unknown and return it
  374. operator IUnknown*()
  375. { return GetInterfacePtr(); }
  376. // Verifies that pUnknown is not null and performs assignment.
  377. HRESULT QueryInterface(IUnknown* pUnknown)
  378. {
  379. _CIP<IUnknown, &IID_IUnknown>::operator=(pUnknown);
  380. return pUnknown != NULL ? S_OK : E_NOINTERFACE;
  381. }
  382. }; // CIP<IUnknown, &IID_IUnknown>
  383. #define IPTR(x) CIP<x, &IID_##x>
  384. #define DEFINE_IPTR(x) typedef IPTR(x) x##Ptr;
  385. /////////////////////////////////////////////////////////////////////////////
  386. #ifdef _AFX_PACKING
  387. #pragma pack(pop)
  388. #endif
  389. #ifdef _AFX_MINREBUILD
  390. #pragma component(minrebuild, on)
  391. #endif
  392. #ifndef _AFX_FULLTYPEINFO
  393. #pragma component(mintypeinfo, off)
  394. #endif
  395. #endif // __AFXCOM_H__
  396. /////////////////////////////////////////////////////////////////////////////