Compartilhar via


CComEnumOnSTL Class

This class defines a COM enumerator object based on an STL collection.

template < 
   class Base, 
   const IID* piid, 
   class T, 
   class Copy, 
   class CollType, 
   class ThreadModel = CComObjectThreadModel 
> 
class ATL_NO_VTABLE CComEnumOnSTL : 
   public IEnumOnSTLImpl<Base, piid, T, Copy, CollType>, 
   public CComObjectRootEx< ThreadModel >

Parameters

  • Base
    A COM enumerator (IEnumXXXX) interface.

  • piid
    A pointer to the interface ID of the enumerator interface.

  • T
    The type of item exposed by the enumerator interface.

  • Copy
    A copy policy class.

  • CollType
    An STL container class.

Remarks

CComEnumOnSTL defines a COM enumerator object based on an STL collection. This class can be used on its own or in conjunction with ICollectionOnSTLImpl. Typical steps for using this class are outlined below. For more information, see ATL Collections and Enumerators.

To use this class with ICollectionOnSTLImpl:

  • typedef a specialization of this class.

  • Use the typedef as the final template argument in a specialization of ICollectionOnSTLImpl.

See ATL Collections and Enumerators for an example.

To use this class independently of ICollectionOnSTLImpl:

  • typedef a specialization of this class.

  • Use the typedef as the template argument in a specialization of CComObject.

  • Create an instance of the CComObject specialization.

  • Initialize the enumerator object by calling IEnumOnSTLImpl::Init.

  • Return the enumerator interface to the client.

Inheritance Hierarchy

CComObjectRootBase

Base

CComObjectRootEx

IEnumOnSTLImpl

CComEnumOnSTL

Requirements

Header: atlcom.h

Example

The code shown below provides a generic function to handle the creation and initialization of an enumerator object:

template <class EnumType, class CollType>
HRESULT CreateSTLEnumerator(IUnknown** ppUnk, IUnknown* pUnkForRelease, 
   CollType& collection)
{
   if (ppUnk == NULL)
      return E_POINTER;
   *ppUnk = NULL;

   CComObject<EnumType>* pEnum = NULL;
   HRESULT hr = CComObject<EnumType>::CreateInstance(&pEnum);

   if (FAILED(hr))
      return hr;

   hr = pEnum->Init(pUnkForRelease, collection);

   if (SUCCEEDED(hr))
      hr = pEnum->QueryInterface(ppUnk);

   if (FAILED(hr))
      delete pEnum;

   return hr;
} // CreateSTLEnumerator

This template function can be used to implement the _NewEnum property of a collection interface as shown below:

typedef CComEnumOnSTL<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _Copy<VARIANT>,
   std::vector<CComVariant> > VarVarEnum;

class ATL_NO_VTABLE CVariantCollection :
   public CComObjectRootEx<CComSingleThreadModel>,
   public CComCoClass<CVariantCollection, &CLSID_VariantCollection>,
   public IDispatchImpl<IVariantCollection, &IID_IVariantCollection, &LIBID_NVC_ATL_COMLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
   std::vector<CComVariant> m_vec;

   STDMETHOD(get__NewEnum)(IUnknown** ppUnk)
   {
      return CreateSTLEnumerator<VarVarEnum>(ppUnk, this, m_vec);
   }

   // Remainder of class declaration omitted.

This code creates a typedef for CComEnumOnSTL that exposes a vector of CComVariants by means of the IEnumVariant interface. The CVariantCollection class simply specializes CreateSTLEnumerator to work with enumerator objects of this type.

See Also

Reference

IEnumOnSTLImpl

CComObjectRootEx Class

CComObjectThreadModel

IEnumOnSTLImpl Class

Concepts

ATLCollections Sample: Demonstrates ICollectionOnSTLImpl, CComEnumOnSTL, and Custom Copy Policy Classes

Other Resources

ATL Class Overview