afxcom_.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #ifdef __BORLANDC__
  263. _CIP<_Interface, _IID>::_CIP(): _pInterface(NULL)
  264. #else
  265. _CIP<_Interface, _IID>::_CIP<_Interface, _IID>(): _pInterface(NULL)
  266. #endif
  267. {
  268. }
  269. template<class _Interface, const IID* _IID>
  270. #ifdef __BORLANDC__
  271. _CIP<_Interface, _IID>::~_CIP()
  272. #else
  273. _CIP<_Interface, _IID>::~_CIP<_Interface, _IID>()
  274. #endif
  275. {
  276. // If we still have an interface then Release() it. The interface
  277. // may be NULL if Detach() has previosly been called, or if it was
  278. // never set.
  279. _Release();
  280. }
  281. template<class _Interface, const IID* _IID>
  282. class CIP : public _CIP<_Interface, _IID>
  283. {
  284. public:
  285. // Simplified name for base class and provide derived classes
  286. // access to base type
  287. typedef _CIP<_Interface, _IID> BC;
  288. // Provideds derived classes access to the interface type.
  289. typedef _Interface Interface;
  290. // Construct empty in preperation for assignment.
  291. CIP() { }
  292. ~CIP();
  293. // Copy the pointer and AddRef().
  294. CIP(const CIP& cp) : _CIP<_Interface, _IID>(cp) { }
  295. // Saves and AddRef()s the interface.
  296. CIP(Interface* pInterface) : _CIP<_Interface, _IID>(pInterface) { }
  297. // Saves the interface and AddRef()s only if bAddRef is TRUE.
  298. CIP(Interface* pInterface, BOOL bAddRef)
  299. : _CIP<_Interface, _IID>(pInterface, bAddRef) { }
  300. // Queries for this interface.
  301. CIP(IUnknown* pUnknown)
  302. {
  303. if (pUnknown == NULL)
  304. return;
  305. Interface* pInterface;
  306. HRESULT hr = pUnknown->QueryInterface(GetIID(),
  307. reinterpret_cast<void**>(&pInterface));
  308. ASSERT(SUCCEEDED(hr));
  309. Attach(pInterface);
  310. }
  311. // Creates the interface from the CLSID.
  312. CIP(const CLSID& clsid) : _CIP<_Interface, _IID>(clsid) { }
  313. // Creates the interface from the CLSID.
  314. CIP(LPOLESTR str) : _CIP<_Interface, _IID>(str) { }
  315. // Copies and AddRef()'s the interface.
  316. CIP& operator=(const CIP& cp)
  317. { _CIP<_Interface, _IID>::operator=(cp); return *this; }
  318. // Saves and AddRef()s the interface.
  319. CIP& operator=(Interface* pInterface)
  320. { _CIP<_Interface, _IID>::operator=(pInterface); return *this; }
  321. CIP& operator=(IUnknown* pUnknown)
  322. {
  323. HRESULT hr = QueryInterface(pUnknown);
  324. ASSERT(SUCCEEDED(hr));
  325. return *this;
  326. }
  327. // Releases any current interface and loads the class with the
  328. // provided CLSID.
  329. CIP& operator=(const CLSID& clsid)
  330. { _CIP<_Interface, _IID>::operator=(clsid); return *this; }
  331. // Releases any current interface and loads the class with the
  332. // provided CLSID.
  333. CIP& operator=(LPOLESTR str)
  334. { _CIP<_Interface, _IID>::operator=(str); return *this; }
  335. }; // class CIP
  336. template<class _Interface, const IID* _IID>
  337. CIP<_Interface, _IID>::~CIP()
  338. {
  339. }
  340. #if _MSC_VER > 1020
  341. template<>
  342. #endif
  343. class CIP<IUnknown, &IID_IUnknown> : public _CIP<IUnknown, &IID_IUnknown>
  344. {
  345. public:
  346. // Simplified name for base class and provide derived classes
  347. // access to base type
  348. typedef _CIP<IUnknown, &IID_IUnknown> BC;
  349. // Provideds derived classes access to the interface type.
  350. typedef IUnknown Interface;
  351. // Construct empty in preperation for assignment.
  352. CIP() { }
  353. // Copy the pointer and AddRef().
  354. CIP(const CIP& cp) : _CIP<IUnknown, &IID_IUnknown>(cp) { }
  355. // Saves and AddRef()s the interface.
  356. CIP(Interface* pInterface)
  357. : _CIP<IUnknown, &IID_IUnknown>(pInterface) { }
  358. // Saves and then AddRef()s only if bAddRef is TRUE.
  359. CIP(Interface* pInterface, BOOL bAddRef)
  360. : _CIP<IUnknown, &IID_IUnknown>(pInterface, bAddRef) { }
  361. // Creates the interface from the CLSID.
  362. CIP(const CLSID& clsid) : _CIP<IUnknown, &IID_IUnknown>(clsid) { }
  363. // Creates the interface from the CLSID.
  364. CIP(LPOLESTR str) : _CIP<IUnknown, &IID_IUnknown>(str) { }
  365. // Copies and AddRef()'s the interface.
  366. CIP& operator=(const CIP& cp)
  367. { _CIP<IUnknown, &IID_IUnknown>::operator=(cp); return *this; }
  368. // Saves and AddRef()s the interface. The previously saved
  369. // interface is released.
  370. CIP& operator=(Interface* pInterface)
  371. { _CIP<IUnknown, &IID_IUnknown>::operator=(pInterface); return *this; }
  372. // Releases any current interface and loads the class with the
  373. // provided CLSID.
  374. CIP& operator=(const CLSID& clsid)
  375. { _CIP<IUnknown, &IID_IUnknown>::operator=(clsid); return *this; }
  376. // Releases any current interface and loads the class with the
  377. // provided CLSID.
  378. CIP& operator=(LPOLESTR str)
  379. { _CIP<IUnknown, &IID_IUnknown>::operator=(str); return *this; }
  380. // Queries for the unknown and return it
  381. operator IUnknown*()
  382. { return GetInterfacePtr(); }
  383. // Verifies that pUnknown is not null and performs assignment.
  384. HRESULT QueryInterface(IUnknown* pUnknown)
  385. {
  386. _CIP<IUnknown, &IID_IUnknown>::operator=(pUnknown);
  387. return pUnknown != NULL ? S_OK : E_NOINTERFACE;
  388. }
  389. }; // CIP<IUnknown, &IID_IUnknown>
  390. #define IPTR(x) CIP<x, &IID_##x>
  391. #define DEFINE_IPTR(x) typedef IPTR(x) x##Ptr;
  392. /////////////////////////////////////////////////////////////////////////////
  393. #ifdef _AFX_PACKING
  394. #pragma pack(pop)
  395. #endif
  396. #ifdef _AFX_MINREBUILD
  397. #pragma component(minrebuild, on)
  398. #endif
  399. #ifndef _AFX_FULLTYPEINFO
  400. #pragma component(mintypeinfo, off)
  401. #endif
  402. #endif // __AFXCOM_H__
  403. /////////////////////////////////////////////////////////////////////////////