CComEnumOnSTL

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:

See ATL Collections and Enumerators for an example.

To use this class independently of ICollectionOnSTLImpl:

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 :
    // ... code omitted
{
    // ... code omitted
    std::vector<CComVariant> m_vec;

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

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

#include <atlcom.h>

Class Members