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.

679 lines
18 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // TemplateFuncs.h
  7. //
  8. // Description:
  9. // Template function implementations.
  10. //
  11. // Author:
  12. // Galen Barbee (galenb) 09-Feb-1999
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #ifndef _TEMPLATEFUNCS_H_
  20. #define _TEMPLATEFUNCS_H_
  21. /////////////////////////////////////////////////////////////////////////////
  22. //++
  23. //
  24. // HrNewIDispatchEnum
  25. //
  26. // Description:
  27. // Create a new Enumerator of IDispatch objects.
  28. //
  29. // Template Arguments:
  30. // TCollection - Type of the STL container argument.
  31. // TObject - Type of the objects in the container.
  32. //
  33. // Arguments:
  34. // ppunk [OUT] - catches the enumerator.
  35. // rCollection [IN] - Implementatoin collection to make the
  36. // enumerator from.
  37. //
  38. // Return Value:
  39. // S_OK if successful, E_POINTER, or other HRESULT error.
  40. //
  41. //--
  42. /////////////////////////////////////////////////////////////////////////////
  43. template< class TCollection, class TObject >
  44. HRESULT HrNewIDispatchEnum(
  45. OUT IUnknown ** ppunk,
  46. IN const TCollection & rCollection
  47. )
  48. {
  49. ASSERT( ppunk != NULL );
  50. HRESULT _hr = E_POINTER;
  51. if ( ppunk != NULL )
  52. {
  53. TObject * _pObject= NULL;
  54. size_t _cObjects = rCollection.size();
  55. size_t _iIndex;
  56. LPDISPATCH _lpDisp;
  57. TCollection::const_iterator _itFirst = rCollection.begin();
  58. TCollection::const_iterator _itLast = rCollection.end();
  59. CComVariant * _pvarVect = NULL;
  60. *ppunk = NULL;
  61. _pvarVect = new CComVariant[ _cObjects ];
  62. if ( _pvarVect != NULL )
  63. {
  64. for ( _iIndex = 0; _itFirst != _itLast; _iIndex++, _itFirst++ )
  65. {
  66. _lpDisp = NULL;
  67. _pObject = NULL;
  68. _pObject = *_itFirst;
  69. _hr = _pObject->QueryInterface( IID_IDispatch, (void **) &_lpDisp );
  70. if ( SUCCEEDED( _hr ) )
  71. {
  72. //
  73. // create a variant and add it to the collection
  74. //
  75. CComVariant & var = _pvarVect[ _iIndex ];
  76. var.vt = VT_DISPATCH;
  77. var.pdispVal = _lpDisp;
  78. }
  79. } // for: each node in the list
  80. CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > > * _pEnum;
  81. _pEnum = new CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > >;
  82. if ( _pEnum != NULL )
  83. {
  84. _hr = _pEnum->Init( &_pvarVect[ 0 ], &_pvarVect[ _cObjects ], NULL, AtlFlagCopy );
  85. if ( SUCCEEDED( _hr ) )
  86. {
  87. _hr = _pEnum->QueryInterface( IID_IEnumVARIANT, (void **) ppunk );
  88. }
  89. else
  90. {
  91. delete _pEnum;
  92. }
  93. }
  94. else
  95. {
  96. _hr = E_OUTOFMEMORY;
  97. }
  98. ClearIDispatchEnum( &_pvarVect );
  99. }
  100. else
  101. {
  102. _hr = E_OUTOFMEMORY;
  103. }
  104. }
  105. return _hr;
  106. } //*** HrNewIDispatchEnum()
  107. /////////////////////////////////////////////////////////////////////////////
  108. //++
  109. //
  110. // ReleaseAndEmptyCollection
  111. //
  112. // Description:
  113. // Clean out the passed in STL container by releasing it's references
  114. // on the contained objects.
  115. //
  116. // Template Arguments:
  117. // TCollection - Type of the STL container argument.
  118. // TObject - Type of the objects in the container.
  119. //
  120. // Arguments:
  121. // rCollection [IN OUT] - STL container instance to clear.
  122. //
  123. // Return Value:
  124. // None.
  125. //
  126. //--
  127. /////////////////////////////////////////////////////////////////////////////
  128. template< class TCollection, class TObject >
  129. void ReleaseAndEmptyCollection(
  130. IN OUT TCollection & rCollection
  131. )
  132. {
  133. if ( !rCollection.empty() )
  134. {
  135. TObject * _pObject = NULL;
  136. TCollection::iterator _itFirst = rCollection.begin();
  137. TCollection::iterator _itLast = rCollection.end();
  138. for ( ; _itFirst != _itLast; _itFirst++ )
  139. {
  140. _pObject = *_itFirst;
  141. if ( _pObject != NULL )
  142. {
  143. _pObject->Release();
  144. } // if: we have an object
  145. } // for: each object in the collection
  146. rCollection.erase( rCollection.begin(), _itLast );
  147. } // if: the collection is not empty
  148. } //*** ReleaseAndEmptyCollection()
  149. /////////////////////////////////////////////////////////////////////////////
  150. //++
  151. //
  152. // HrNewVariantEnum
  153. //
  154. // Description:
  155. // Create a new Enumerator of VARIANT objects.
  156. //
  157. // Template Arguments:
  158. // TCollection - Type of the STL container argument.
  159. //
  160. // Arguments:
  161. // ppunk [OUT] - catches the enumerator.
  162. // rCollection [IN] - Implementatoin collection to make the
  163. // enumerator from.
  164. //
  165. // Return Value:
  166. // S_OK if successful, E_POINTER, or other HRESULT error.
  167. //
  168. //--
  169. /////////////////////////////////////////////////////////////////////////////
  170. template< class TCollection >
  171. STDMETHODIMP HrNewVariantEnum(
  172. OUT IUnknown ** ppunk,
  173. IN const TCollection & rCollection
  174. )
  175. {
  176. ASSERT( ppunk != NULL );
  177. HRESULT _hr = E_POINTER;
  178. if ( ppunk != NULL )
  179. {
  180. TCollection::const_iterator _itFirst = rCollection.begin();
  181. TCollection::const_iterator _itLast = rCollection.end();
  182. size_t _iIndex;
  183. size_t _cVariants = rCollection.size();
  184. CComVariant * _pvarVect = NULL;
  185. *ppunk = NULL;
  186. _pvarVect = new CComVariant[ _cVariants ];
  187. if ( _pvarVect != NULL )
  188. {
  189. for ( _iIndex = 0; _itFirst != _itLast; _iIndex++, _itFirst++ )
  190. {
  191. _pvarVect[ _iIndex ] = *_itFirst;
  192. }
  193. CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > > * _pEnum;
  194. _pEnum = new CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > >;
  195. if ( _pEnum != NULL )
  196. {
  197. _hr = _pEnum->Init( &_pvarVect[ 0 ], &_pvarVect[ _cVariants ], NULL, AtlFlagCopy );
  198. if ( SUCCEEDED( _hr ) )
  199. {
  200. _hr = _pEnum->QueryInterface( IID_IEnumVARIANT, (void **) ppunk );
  201. }
  202. else
  203. {
  204. delete _pEnum;
  205. }
  206. }
  207. else
  208. {
  209. _hr = E_OUTOFMEMORY;
  210. }
  211. ClearVariantEnum( &_pvarVect );
  212. }
  213. else
  214. {
  215. _hr = E_OUTOFMEMORY;
  216. }
  217. }
  218. return _hr;
  219. } //*** HrNewVariantEnum()
  220. /////////////////////////////////////////////////////////////////////////////
  221. //++
  222. //
  223. // HrNewCComBSTREnum
  224. //
  225. // Description:
  226. // Create a new Enumerator of CComBSTR objects.
  227. //
  228. // Template Arguments:
  229. // TCollection - Type of the STL container argument.
  230. //
  231. // Arguments:
  232. // ppunk [OUT] - catches the enumerator.
  233. // rCollection [IN] - Implementatoin collection to make the
  234. // enumerator from.
  235. //
  236. // Return Value:
  237. // S_OK if successful, E_POINTER, or other HRESULT error.
  238. //
  239. //--
  240. /////////////////////////////////////////////////////////////////////////////
  241. template< class TCollection >
  242. STDMETHODIMP HrNewCComBSTREnum(
  243. OUT IUnknown ** ppunk,
  244. IN const TCollection & rCollection
  245. )
  246. {
  247. ASSERT( ppunk != NULL );
  248. HRESULT _hr = E_POINTER;
  249. if ( ppunk != NULL )
  250. {
  251. TCollection::const_iterator _itFirst = rCollection.begin();
  252. TCollection::const_iterator _itLast = rCollection.end();
  253. size_t _iIndex;
  254. size_t _cVariants = rCollection.size();
  255. CComVariant * _pvarVect = NULL;
  256. *ppunk = NULL;
  257. _pvarVect = new CComVariant[ _cVariants ];
  258. if ( _pvarVect != NULL )
  259. {
  260. for ( _iIndex = 0; _itFirst != _itLast; _iIndex++, _itFirst++ )
  261. {
  262. _pvarVect[ _iIndex ].bstrVal = (*_itFirst)->Copy();;
  263. _pvarVect[ _iIndex ].vt = VT_BSTR;
  264. } // for:
  265. CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > > * _pEnum;
  266. _pEnum = new CComObject< CComEnum< IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy< VARIANT > > >;
  267. if ( _pEnum != NULL )
  268. {
  269. _hr = _pEnum->Init( &_pvarVect[ 0 ], &_pvarVect[ _cVariants ], NULL, AtlFlagCopy );
  270. if ( SUCCEEDED( _hr ) )
  271. {
  272. _hr = _pEnum->QueryInterface( IID_IEnumVARIANT, (void **) ppunk );
  273. }
  274. else
  275. {
  276. delete _pEnum;
  277. }
  278. }
  279. else
  280. {
  281. _hr = E_OUTOFMEMORY;
  282. }
  283. ClearVariantEnum( &_pvarVect );
  284. }
  285. else
  286. {
  287. _hr = E_OUTOFMEMORY;
  288. }
  289. }
  290. return _hr;
  291. } //*** HrNewCComBSTREnum()
  292. /////////////////////////////////////////////////////////////////////////////
  293. //++
  294. //
  295. // HrCreateResourceCollection
  296. //
  297. // Description:
  298. // Create a resource collection.
  299. //
  300. // Template Arguments:
  301. // TCollection - Type of the collection implementation argument.
  302. // TInterface - Type of the collection Interface argument.
  303. // THandle - Type of the tHandle argument.
  304. //
  305. // Arguments:
  306. // ppCollection [OUT] - Catches the new collection implementation.
  307. // tHandle [IN] - Passed to the objects Create method.
  308. // ppInterface [OUT] - Catches the new collection interface.
  309. // iid [IN] - IID of the interface to QI for.
  310. // pClusRefObject [IN] - Wraps the cluster handle.
  311. //
  312. // Return Value:
  313. // S_OK if successful, E_POINTER, or other HRESULT error.
  314. //
  315. //--
  316. /////////////////////////////////////////////////////////////////////////////
  317. template< class TCollection, class TInterface, class THandle >
  318. HRESULT HrCreateResourceCollection(
  319. OUT CComObject< TCollection > ** ppCollection,
  320. IN THandle tHandle,
  321. OUT TInterface ** ppInterface,
  322. IN IID iid,
  323. IN ISClusRefObject * pClusRefObject
  324. )
  325. {
  326. ASSERT( ppCollection != NULL );
  327. ASSERT( tHandle != NULL );
  328. // ASSERT( ppInterface != NULL );
  329. ASSERT( pClusRefObject != NULL );
  330. HRESULT _hr = E_POINTER;
  331. if ( ( ppCollection != NULL ) && ( ppInterface != NULL ) && ( pClusRefObject != NULL ) && ( tHandle != NULL ) )
  332. {
  333. *ppInterface = NULL;
  334. _hr = S_OK;
  335. if ( *ppCollection == NULL )
  336. {
  337. CComObject< TCollection > * pCollection = NULL;
  338. _hr = CComObject< TCollection >::CreateInstance( &pCollection );
  339. if ( SUCCEEDED( _hr ) )
  340. {
  341. CSmartPtr< ISClusRefObject > ptrRefObject( pClusRefObject );
  342. CSmartPtr< CComObject< TCollection > > ptrCollection( pCollection );
  343. _hr = ptrCollection->Create( ptrRefObject, tHandle );
  344. if ( SUCCEEDED( _hr ) )
  345. {
  346. _hr = ptrCollection->Refresh();
  347. if ( SUCCEEDED( _hr ) )
  348. {
  349. *ppCollection = ptrCollection;
  350. ptrCollection->AddRef();
  351. } // if: Refresh OK
  352. } // if: Create OK
  353. } // if: CreateInstance OK
  354. } // if: do we need to create a new collection?
  355. if ( SUCCEEDED( _hr ) )
  356. {
  357. _hr = (*ppCollection)->QueryInterface( iid, (void **) ppInterface );
  358. } // if: we have, or successfully made a collection
  359. } // if: all args OK
  360. return _hr;
  361. } //*** HrCreateResourceCollection()
  362. /////////////////////////////////////////////////////////////////////////////
  363. //++
  364. //
  365. // HrCreateResourceCollection
  366. //
  367. // Description:
  368. // Create a resource collection.
  369. //
  370. // Template Arguments:
  371. // TCollection - Type of the collection implementation argument.
  372. // TInterface - Type of the collection Interface argument.
  373. // THandle - Type of the tHandle argument.
  374. //
  375. // Arguments:
  376. // ppInterface [OUT] - Catches the new collection interface.
  377. // tHandle [IN] - Passed to the objects Create method.
  378. // iid [IN] - IID of the interface to QI for.
  379. // pClusRefObject [IN] - Wraps the cluster handle.
  380. //
  381. // Return Value:
  382. // S_OK if successful, E_POINTER, or other HRESULT error.
  383. //
  384. //--
  385. /////////////////////////////////////////////////////////////////////////////
  386. template< class TCollection, class TInterface, class THandle >
  387. HRESULT HrCreateResourceCollection(
  388. OUT TInterface ** ppInterface,
  389. IN THandle tHandle,
  390. IN IID iid,
  391. IN ISClusRefObject * pClusRefObject
  392. )
  393. {
  394. ASSERT( ppInterface != NULL );
  395. ASSERT( tHandle != NULL );
  396. ASSERT( pClusRefObject != NULL );
  397. HRESULT _hr = E_POINTER;
  398. if ( ( ppInterface != NULL ) && ( pClusRefObject != NULL ) && ( tHandle != NULL ) )
  399. {
  400. *ppInterface = NULL;
  401. _hr = S_OK;
  402. CComObject< TCollection > * pCollection = NULL;
  403. _hr = CComObject< TCollection >::CreateInstance( &pCollection );
  404. if ( SUCCEEDED( _hr ) )
  405. {
  406. CSmartPtr< ISClusRefObject > ptrRefObject( pClusRefObject );
  407. CSmartPtr< CComObject< TCollection > > ptrCollection( pCollection );
  408. _hr = ptrCollection->Create( ptrRefObject, tHandle );
  409. if ( SUCCEEDED( _hr ) )
  410. {
  411. _hr = ptrCollection->Refresh();
  412. if ( SUCCEEDED( _hr ) )
  413. {
  414. _hr = pCollection->QueryInterface( iid, (void **) ppInterface );
  415. } // if: Refresh OK
  416. } // if: Create OK
  417. } // if: CreateInstance OK
  418. } // if: all args OK
  419. return _hr;
  420. } //*** HrCreateResourceCollection()
  421. /////////////////////////////////////////////////////////////////////////////
  422. //++
  423. //
  424. // HrCreateResourceCollection
  425. //
  426. // Description:
  427. // Create a resource collection.
  428. //
  429. // Template Arguments:
  430. // TCollection - Type of the collection implementation to make.
  431. // TInterface - Type of the collection Interface argument.
  432. // THandle - Type of the tHandle argument.
  433. //
  434. // Arguments:
  435. // ppInterface [OUT] - Catches the new collection interface.
  436. // iid [IN] - IID of the interface to QI for.
  437. // pClusRefObject [IN] - Wraps the cluster handle.
  438. //
  439. // Return Value:
  440. // S_OK if successful, E_POINTER, or other HRESULT error.
  441. //
  442. //--
  443. /////////////////////////////////////////////////////////////////////////////
  444. template< class TCollection, class TInterface, class THandle >
  445. HRESULT HrCreateResourceCollection(
  446. OUT TInterface ** ppInterface,
  447. IN IID iid,
  448. IN ISClusRefObject * pClusRefObject
  449. )
  450. {
  451. //ASSERT( ppInterface != NULL );
  452. ASSERT( pClusRefObject != NULL );
  453. HRESULT _hr = E_POINTER;
  454. if ( ( ppInterface != NULL ) && ( pClusRefObject != NULL ) )
  455. {
  456. *ppInterface = NULL;
  457. _hr = S_OK;
  458. CComObject< TCollection > * pCollection = NULL;
  459. _hr = CComObject< TCollection >::CreateInstance( &pCollection );
  460. if ( SUCCEEDED( _hr ) )
  461. {
  462. CSmartPtr< ISClusRefObject > ptrRefObject( pClusRefObject );
  463. CSmartPtr< CComObject< TCollection > > ptrCollection( pCollection );
  464. _hr = ptrCollection->Create( ptrRefObject );
  465. if ( SUCCEEDED( _hr ) )
  466. {
  467. _hr = ptrCollection->Refresh();
  468. if ( SUCCEEDED( _hr ) )
  469. {
  470. _hr = pCollection->QueryInterface( iid, (void **) ppInterface );
  471. } // if: Refresh OK
  472. } // if: Create OK
  473. } // if: CreateInstance OK
  474. } // if: all args OK
  475. return _hr;
  476. } //*** HrCreateResourceCollection()
  477. /////////////////////////////////////////////////////////////////////////////
  478. //++
  479. //
  480. // HrCreateResourceCollection
  481. //
  482. // Description:
  483. // Create a resource collection.
  484. //
  485. // Template Arguments:
  486. // TCollection - Type of the collection implementation argument.
  487. // TInterface - Type of the collection Interface argument.
  488. // THandle - Not used. Simply here because the Alpha compiler is broken.
  489. //
  490. // Arguments:
  491. // ppCollection [OUT] - Catches the new collection implementation.
  492. // ppInterface [OUT] - Catches the new collection interface.
  493. // iid [IN] - IID of the interface to QI for.
  494. // pClusRefObject [IN] - Wraps the cluster handle.
  495. //
  496. // Return Value:
  497. // S_OK if successful, E_POINTER, or other HRESULT error.
  498. //
  499. //--
  500. /////////////////////////////////////////////////////////////////////////////
  501. template< class TCollection, class TInterface, class THandle >
  502. HRESULT HrCreateResourceCollection(
  503. OUT CComObject< TCollection > ** ppCollection,
  504. OUT TInterface ** ppInterface,
  505. IN IID iid,
  506. IN ISClusRefObject * pClusRefObject
  507. )
  508. {
  509. ASSERT( ppCollection != NULL );
  510. //ASSERT( ppInterface != NULL );
  511. ASSERT( pClusRefObject != NULL );
  512. HRESULT _hr = E_POINTER;
  513. if ( ( ppCollection != NULL ) && ( ppInterface != NULL ) && ( pClusRefObject != NULL ) )
  514. {
  515. *ppInterface = NULL;
  516. _hr = S_OK;
  517. if ( *ppCollection == NULL )
  518. {
  519. CComObject< TCollection > * pCollection = NULL;
  520. _hr = CComObject< TCollection >::CreateInstance( &pCollection );
  521. if ( SUCCEEDED( _hr ) )
  522. {
  523. CSmartPtr< ISClusRefObject > ptrRefObject( pClusRefObject );
  524. CSmartPtr< CComObject< TCollection > > ptrCollection( pCollection );
  525. _hr = ptrCollection->Create( ptrRefObject );
  526. if ( SUCCEEDED( _hr ) )
  527. {
  528. _hr = ptrCollection->Refresh();
  529. if ( SUCCEEDED( _hr ) )
  530. {
  531. *ppCollection = ptrCollection;
  532. ptrCollection->AddRef();
  533. } // if: Refresh OK
  534. } // if: Create OK
  535. } // if: CreateInstance OK
  536. } // if: do we need to create a new collection?
  537. if ( SUCCEEDED( _hr ) )
  538. {
  539. _hr = (*ppCollection)->QueryInterface( iid, (void **) ppInterface );
  540. } // if: we have, or successfully made a collection
  541. } // if: all args OK
  542. return _hr;
  543. } //*** HrCreateResourceCollection()
  544. /////////////////////////////////////////////////////////////////////////////
  545. //++
  546. //
  547. // HrCreateResourceCollection
  548. //
  549. // Description:
  550. // Create a resource collection.
  551. //
  552. // Template Arguments:
  553. // TCollection - Type of the collection implementation to make.
  554. // TInterface - Type of the collection Interface argument.
  555. // THandle - Type of the tHandle argument.
  556. //
  557. // Arguments:
  558. // tHandle [IN] - Passed to the collection' create method.
  559. // ppInterface [OUT] - Catches the new collection interface.
  560. // iid [IN] - IID of the interface to QI for.
  561. //
  562. // Return Value:
  563. // S_OK if successful, E_POINTER, or other HRESULT error.
  564. //
  565. //--
  566. /////////////////////////////////////////////////////////////////////////////
  567. template< class TCollection, class TInterface, class THandle >
  568. HRESULT HrCreateResourceCollection(
  569. IN THandle tHandle,
  570. OUT TInterface ** ppInterface,
  571. IN IID iid
  572. )
  573. {
  574. //ASSERT( ppInterface != NULL );
  575. HRESULT _hr = E_POINTER;
  576. if ( ppInterface != NULL )
  577. {
  578. *ppInterface = NULL;
  579. _hr = S_OK;
  580. CComObject< TCollection > * pCollection = NULL;
  581. _hr = CComObject< TCollection >::CreateInstance( &pCollection );
  582. if ( SUCCEEDED( _hr ) )
  583. {
  584. CSmartPtr< CComObject< TCollection > > ptrCollection( pCollection );
  585. _hr = ptrCollection->Create( tHandle );
  586. if ( SUCCEEDED( _hr ) )
  587. {
  588. _hr = ptrCollection->Refresh();
  589. if ( SUCCEEDED( _hr ) )
  590. {
  591. _hr = pCollection->QueryInterface( iid, (void **) ppInterface );
  592. } // if: Refresh OK
  593. } // if: Create OK
  594. } // if: CreateInstance OK
  595. } // if: all args OK
  596. return _hr;
  597. } //*** HrCreateResourceCollection()
  598. #endif // _TEMPLATEFUNCS_H_