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.

173 lines
5.5 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1995-1999 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. // VCUE_Collection.h
  11. //
  12. // This header contains code that simplifies or enhances use of ATL's collection
  13. // and enumerator classes, ICollectionOnSTLImpl, IEnumOnSTLImpl, CComEnumOnSTL, CComEnumImpl, and CComEnum
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #if !defined(_COLLECTION_H___36A49828_B15B_11D2_BA63_00C04F8EC847___INCLUDED_)
  17. #define _COLLECTION_H___36A49828_B15B_11D2_BA63_00C04F8EC847___INCLUDED_
  18. #if _MSC_VER >= 1000
  19. #pragma once
  20. #endif // _MSC_VER >= 1000
  21. #include <AtlCom.h>
  22. namespace VCUE
  23. {
  24. // CGenericDataHolder is a class that stores data
  25. // The lifetime of objects of this class is managed via the IUnknown interface
  26. // which makes objects of this class suitable as a source of shared data
  27. // Clients that need access to the data can keep a CGenericDataHolder object alive
  28. // just by holding a COM reference on that object
  29. // This class is used (by default) by ICollectionOnSTLCopyImpl::get__NewEnum to provide data
  30. // to be shared between an enumerator and its clones.
  31. template < class DataType, class ThreadModel = CComObjectThreadModel >
  32. class ATL_NO_VTABLE CGenericDataHolder :
  33. public IUnknown,
  34. public CComObjectRootEx< ThreadModel >
  35. {
  36. public:
  37. typedef CGenericDataHolder< DataType, ThreadModel > thisClass;
  38. BEGIN_COM_MAP(thisClass)
  39. COM_INTERFACE_ENTRY(IUnknown)
  40. END_COM_MAP()
  41. template < class SourceType >
  42. HRESULT Copy(const SourceType& c)
  43. {
  44. m_Data = c;
  45. return S_OK;
  46. } // HRESULT Copy(const SourceType& c)
  47. DataType m_Data;
  48. }; // class ATL_NO_VTABLE CGenericDataHolder
  49. // CreateSTLEnumerator wraps the necessary creation, initialization
  50. // and error handling code for the the creation of a CComEnumOnSTL-style enumerator
  51. // *** EXAMPLE : Using CreateSTLEnumerator to implement get__NewEnum ***
  52. // typedef CComEnumOnSTL<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT,
  53. // _Copy<VARIANT>, std::vector<CComVariant> > VarVarEnum;
  54. // std::vector<CComVariant> m_vec;
  55. // STDMETHOD(get__NewEnum)(IUnknown** ppUnk)
  56. // {
  57. // return CreateSTLEnumerator<VarVarEnum>(ppUnk, this, m_vec);
  58. // }
  59. template <class EnumType, class CollType>
  60. HRESULT CreateSTLEnumerator(IUnknown** ppUnk, IUnknown* pUnkForRelease, CollType& collection)
  61. {
  62. if (ppUnk == NULL)
  63. return E_POINTER;
  64. *ppUnk = NULL;
  65. CComObject<EnumType>* pEnum = NULL;
  66. HRESULT hr = CComObject<EnumType>::CreateInstance(&pEnum);
  67. if (FAILED(hr))
  68. return hr;
  69. hr = pEnum->Init(pUnkForRelease, collection);
  70. if (SUCCEEDED(hr))
  71. hr = pEnum->QueryInterface(ppUnk);
  72. if (FAILED(hr))
  73. delete pEnum;
  74. return hr;
  75. } // HRESULT CreateSTLEnumerator(IUnknown** ppUnk, IUnknown* pUnkForRelease, CollType& collection)
  76. // CreateEnumerator wraps the necessary creation, initialization
  77. // and error handling code for the the creation of a CComEnum-style enumerator
  78. template <class EnumType, class ElementType>
  79. HRESULT CreateEnumerator(IUnknown** ppUnk,
  80. ElementType* begin, ElementType* end,
  81. IUnknown* pUnk,
  82. CComEnumFlags flags)
  83. {
  84. if (ppUnk == NULL)
  85. return E_POINTER;
  86. *ppUnk = NULL;
  87. CComObject<EnumType>* pEnum = NULL;
  88. HRESULT hr = CComObject<EnumType>::CreateInstance(&pEnum);
  89. if (FAILED(hr))
  90. return hr;
  91. hr = pEnum->Init(begin, end, pUnk, flags);
  92. if (SUCCEEDED(hr))
  93. hr = pEnum->QueryInterface(ppUnk);
  94. if (FAILED(hr))
  95. delete pEnum;
  96. return hr;
  97. } // CreateEnumerator
  98. // ICollectionOnSTLCopyImpl derives from ICollectionOnSTLImpl and overrides get__NewEnum
  99. // The new implementation provides each enumerator with its own copy of the collection data.
  100. // (Note that this only applies to enumerators returned directly by get__NewEnum.
  101. // Cloned enumerators use their parent's data as before.
  102. // This is OK because the enumerator never changes the data)
  103. // Use this class when:
  104. // The collection can change while there are outstanding enumerators
  105. // And You don't want to invalidate those enumerators when that happens
  106. // And You are sure that the performance hit is worth it
  107. // And You are sure that the way items are copied between containers works correctly
  108. // (You can adjust this by passing a different class as the Holder parameter)
  109. // Mostly you can use this class in exactly the same
  110. // way that you would use ICollectionOnSTLImpl.
  111. template <class T, class CollType, class ItemType, class CopyItem, class EnumType, class Holder = CGenericDataHolder< CollType > >
  112. class ICollectionOnSTLCopyImpl :
  113. public ICollectionOnSTLImpl<T, CollType, ItemType, CopyItem, EnumType>
  114. {
  115. public :
  116. STDMETHOD(get__NewEnum)(IUnknown** ppUnk)
  117. {
  118. typedef CComObject< Holder > HolderObject;
  119. HolderObject* p = NULL;
  120. HRESULT hr = HolderObject::CreateInstance(&p);
  121. if (FAILED(hr))
  122. return hr;
  123. hr = p->Copy(m_coll);
  124. if (FAILED(hr))
  125. return hr;
  126. return CreateSTLEnumerator<EnumType>(ppUnk, p, p->m_Data);
  127. } // STDMETHOD(get__NewEnum)(IUnknown** ppUnk)
  128. }; // class ICollectionOnSTLCopyImpl
  129. }; // namespace VCUE
  130. #endif // !defined(_COLLECTION_H___36A49828_B15B_11D2_BA63_00C04F8EC847___INCLUDED_)