Leaked source code of windows server 2003
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.

311 lines
6.9 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. CLSFAC.H
  5. Abstract:
  6. Class Factory Helpers
  7. History:
  8. --*/
  9. #ifndef __WBEM_CLASS_FACTORY__H_
  10. #define __WBEM_CLASS_FACTORY__H_
  11. #include <unk.h>
  12. #include <sync.h>
  13. #include <comutl.h>
  14. /***************************************************************************
  15. CBaseClassFactory
  16. ****************************************************************************/
  17. class CBaseClassFactory : public CUnkInternal
  18. {
  19. public:
  20. CBaseClassFactory( CLifeControl* pControl )
  21. : CUnkInternal( pControl ), m_XClassFactory( this ) {}
  22. class XClassFactory : public CImpl<IClassFactory,CBaseClassFactory>
  23. {
  24. public:
  25. STDMETHOD(CreateInstance)(IUnknown* pOuter, REFIID riid, void** ppv)
  26. {
  27. return m_pObject->CreateInstance( pOuter, riid, ppv );
  28. }
  29. STDMETHOD(LockServer)(BOOL fLock)
  30. {
  31. return m_pObject->LockServer( fLock );
  32. }
  33. XClassFactory( CBaseClassFactory* pOwner )
  34. : CImpl<IClassFactory,CBaseClassFactory>( pOwner ) {}
  35. } m_XClassFactory;
  36. void* GetInterface( REFIID riid )
  37. {
  38. if ( riid == IID_IClassFactory )
  39. {
  40. return &m_XClassFactory;
  41. }
  42. return NULL;
  43. }
  44. virtual HRESULT CreateInstance( IUnknown* pOuter,
  45. REFIID riid,
  46. void** ppv ) = 0;
  47. virtual HRESULT LockServer( BOOL fLock ) = 0;
  48. };
  49. /****************************************************************************
  50. CSimpleClassFactory - does not support aggregation
  51. *****************************************************************************/
  52. template<class TObject>
  53. class CSimpleClassFactory : public CBaseClassFactory
  54. {
  55. public:
  56. CSimpleClassFactory( CLifeControl* pControl = NULL )
  57. : CBaseClassFactory( pControl ) {}
  58. HRESULT CreateInstance( IUnknown* pOuter, REFIID riid, void** ppv )
  59. {
  60. HRESULT hr;
  61. *ppv = NULL;
  62. if(pOuter)
  63. return CLASS_E_NOAGGREGATION;
  64. // Lock
  65. if(m_pControl && !m_pControl->ObjectCreated(NULL))
  66. {
  67. // Shutting down
  68. // =============
  69. return CO_E_SERVER_STOPPING;
  70. }
  71. // Create
  72. try
  73. {
  74. CWbemPtr<TObject> pObject = new TObject(m_pControl);
  75. if ( pObject != NULL )
  76. {
  77. hr = pObject->QueryInterface(riid, ppv);
  78. }
  79. else
  80. {
  81. hr = E_FAIL;
  82. }
  83. }
  84. catch(...)
  85. {
  86. hr = E_FAIL;
  87. }
  88. //
  89. // Unlock
  90. //
  91. if( m_pControl != NULL )
  92. {
  93. m_pControl->ObjectDestroyed(NULL);
  94. }
  95. return hr;
  96. }
  97. HRESULT LockServer( BOOL fLock )
  98. {
  99. if(fLock)
  100. m_pControl->ObjectCreated(NULL);
  101. else
  102. m_pControl->ObjectDestroyed(NULL);
  103. return S_OK;
  104. }
  105. };
  106. /****************************************************************************
  107. CClassFactory - supports aggregation
  108. *****************************************************************************/
  109. template<class TObject>
  110. class CClassFactory : public CSimpleClassFactory<TObject>
  111. {
  112. public:
  113. CClassFactory(CLifeControl* pControl = NULL) :
  114. CSimpleClassFactory<TObject>(pControl){}
  115. HRESULT CreateInstance( IUnknown* pOuter, REFIID riid, void** ppv )
  116. {
  117. HRESULT hr;
  118. *ppv = NULL;
  119. //
  120. // Lock
  121. //
  122. if(m_pControl && !m_pControl->ObjectCreated(NULL))
  123. {
  124. // Shutting down
  125. // =============
  126. return CO_E_SERVER_STOPPING;
  127. }
  128. //
  129. // Create
  130. //
  131. TObject * pNewObject = 0;
  132. try
  133. {
  134. pNewObject = new TObject(m_pControl, pOuter);
  135. }
  136. catch(...) // do not let exception go beyond COM
  137. {
  138. return E_OUTOFMEMORY;
  139. }
  140. CWbemPtr<TObject> pObject(pNewObject);
  141. //
  142. // Initialize
  143. //
  144. if ( pObject != NULL && pObject->Initialize() )
  145. {
  146. if ( pOuter == NULL )
  147. {
  148. hr = pObject->QueryInterface(riid, ppv);
  149. }
  150. else
  151. {
  152. if ( riid == IID_IUnknown )
  153. {
  154. *ppv = pObject->GetInnerUnknown();
  155. pObject->AddRef();
  156. hr = S_OK;
  157. }
  158. else
  159. {
  160. hr = CLASS_E_NOAGGREGATION;
  161. }
  162. }
  163. }
  164. else
  165. {
  166. hr = E_FAIL;
  167. }
  168. // Unlock
  169. if( m_pControl != NULL )
  170. {
  171. m_pControl->ObjectDestroyed(NULL);
  172. }
  173. return hr;
  174. }
  175. };
  176. /****************************************************************************
  177. CSingletonClassFactory
  178. *****************************************************************************/
  179. template<class T>
  180. class CSingletonClassFactory : public CBaseClassFactory
  181. {
  182. CCritSec m_cs;
  183. T* m_pObj;
  184. public:
  185. CSingletonClassFactory( CLifeControl* pControl )
  186. : CBaseClassFactory( pControl ), m_pObj(NULL) {}
  187. ~CSingletonClassFactory()
  188. {
  189. if ( m_pObj != NULL )
  190. {
  191. m_pObj->InternalRelease();
  192. }
  193. }
  194. HRESULT CreateInstance( IUnknown* pOuter, REFIID riid, void** ppv )
  195. {
  196. HRESULT hr;
  197. *ppv = NULL;
  198. if( pOuter != NULL )
  199. {
  200. return CLASS_E_NOAGGREGATION;
  201. }
  202. //
  203. // lock the server.
  204. //
  205. if ( !m_pControl->ObjectCreated( this ) )
  206. {
  207. return CO_E_SERVER_STOPPING;
  208. }
  209. {
  210. CInCritSec ics( &m_cs );
  211. if ( m_pObj != NULL )
  212. {
  213. hr = m_pObj->QueryInterface( riid, ppv );
  214. }
  215. else
  216. {
  217. m_pObj = new T( m_pControl );
  218. if ( m_pObj != NULL )
  219. {
  220. m_pObj->InternalAddRef();
  221. hr = m_pObj->QueryInterface( riid, ppv );
  222. }
  223. else
  224. {
  225. hr = E_FAIL;
  226. }
  227. }
  228. }
  229. //
  230. // Unlock the server and return;
  231. //
  232. m_pControl->ObjectDestroyed( this );
  233. return hr;
  234. }
  235. HRESULT LockServer( BOOL fLock )
  236. {
  237. if(fLock)
  238. m_pControl->ObjectCreated(NULL);
  239. else
  240. m_pControl->ObjectDestroyed(NULL);
  241. return S_OK;
  242. }
  243. };
  244. #endif