enumtmpl.hxx
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #ifndef __IFACEPTR_ENUMERATOR_TEMPLATE_H
  2. #define __IFACEPTR_ENUMERATOR_TEMPLATE_H
  3. //
  4. // Instantiate with T as the pointer type.
  5. // i.e. EnumIFacePtrs<PSETTINGS_FOLDER_CATEGORY>
  6. //
  7. template <class T>
  8. class EnumIFacePtrs
  9. {
  10.     private:
  11.         UINT        m_iCurrent;
  12.         PointerList m_List;
  13.     public:
  14.         EnumIFacePtrs(T *ppIFace, UINT cItems
  15.             ) : m_iCurrent(0)
  16.         {
  17.             DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::EnumIFacePtrs"));
  18.             for (UINT i = 0; i < cItems; i++)
  19.             {
  20.                 Assert(NULL != *(ppIFace + i));
  21.                 m_List.Append((LPVOID)(*(ppIFace + i))); // Can throw OutOfMemory.
  22.                 DebugMsg(DM_NOW, TEXT("EnumIFacePtrs added 0x%08X at item %d"), *(ppIFace+i), i);
  23.             }
  24.             DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::EnumIFacePtrs [LEAVE]"));
  25.         }
  26.         EnumIFacePtrs(EnumIFacePtrs& rhs);
  27.         ~EnumIFacePtrs(VOID);
  28.         //
  29.         // IEnumXXXXX methods.
  30.         //
  31.         HRESULT
  32.         Next(
  33.             DWORD cRequested, 
  34.             T *ppIFace, 
  35.             LPDWORD pcCreated);
  36.             
  37.         HRESULT
  38.         Skip(
  39.             DWORD cSkip);
  40.         HRESULT
  41.         Reset(
  42.             VOID);
  43.         HRESULT
  44.         Clone(
  45.             EnumIFacePtrs **);
  46. };
  47. template <class T>
  48. EnumIFacePtrs<T>::EnumIFacePtrs(
  49.     EnumIFacePtrs& rhs
  50.     ) : m_iCurrent(0)
  51. {
  52.     DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::EnumIFacePtrs"));
  53.     INT cItems = rhs.m_List.Count();
  54.     for (INT i = 0; i < cItems; i++)
  55.     {
  56.         T pIFace = NULL;
  57.         rhs.m_List.Retrieve((LPVOID *)&pIFace, i);
  58.         Assert(NULL != pIFace);
  59.         m_List.Append((LPVOID)pIFace); // Can throw OutOfMemory.
  60.         pIFace->AddRef();
  61.         DebugMsg(DM_NOW, TEXT("EnumIFacePtrs added 0x%08X at item %d"), pIFace, i);
  62.     }
  63.     DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::EnumIFacePtrs [LEAVE]"));
  64. }
  65. template<class T>
  66. EnumIFacePtrs<T>::~EnumIFacePtrs(
  67.     VOID
  68.     )
  69. {
  70.     T pIFace = NULL;
  71.     DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::~EnumIFacePtrs"));
  72.     //
  73.     // Release all objects held in member list.
  74.     //
  75.     while(m_List.Remove((LPVOID *)&pIFace, 0))
  76.     {
  77.         Assert(NULL != pIFace);
  78.         DebugMsg(DM_NOW, TEXT("Releasing ptr 0x%08X from PointerList"), pIFace);
  79.         pIFace->Release();
  80.     }
  81.     DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::~EnumIFacePtrs [LEAVE]"));
  82. }
  83. template<class T>
  84. HRESULT
  85. EnumIFacePtrs<T>::Next(
  86.     DWORD cRequested, 
  87.     T *ppIFace, 
  88.     LPDWORD pcCreated
  89.     )
  90. {
  91.     HRESULT hResult = S_OK;
  92.     DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::Next %d"), cRequested);
  93.     if (NULL != ppIFace)
  94.     {
  95.         DWORD cCreated = 0;
  96.         UINT cItems = m_List.Count();
  97.         //
  98.         // Transfer data to caller's array.
  99.         // Stop when at the end of the enumeration or we've
  100.         // returned all that the caller asked for.
  101.         //
  102.         while(m_iCurrent < cItems && cRequested > 0)
  103.         {
  104.             DebugMsg(DM_NOW, TEXT("Enumerating, iCurrent = %d,  cItems = %d, cRequested = %d"),
  105.                                   m_iCurrent, cItems, cRequested);
  106.             T pIFace = NULL;
  107.             m_List.Retrieve((LPVOID *)&pIFace, m_iCurrent++);
  108.             Assert(NULL != pIFace);
  109.             pIFace->AddRef();
  110.             *(ppIFace + cCreated) = pIFace;
  111.             DebugMsg(DM_NOW, TEXT("Enumerated 0x%08X"), pIFace);
  112.             cCreated++;
  113.             cRequested--;
  114.         }
  115.         //
  116.         // If requested, return the count of categories enumerated.
  117.         //
  118.         if (NULL != pcCreated)
  119.             *pcCreated = cCreated;
  120.         if (cRequested > 0)
  121.         {
  122.             //
  123.             // Less than requested number of categories were retrieved.
  124.             // 
  125.             hResult = S_FALSE;
  126.         }
  127.         DebugMsg(DM_NOW, TEXT("EnumIFacePtrs::Next [LEAVE] %d enumerated"), cCreated);
  128.     }
  129.     else
  130.         hResult = E_POINTER;
  131.     return hResult;
  132. }
  133.     
  134. template<class T>
  135. HRESULT
  136. EnumIFacePtrs<T>::Skip(
  137.     DWORD cSkip)
  138. {
  139.     UINT cItems = m_List.Count();
  140.     while(m_iCurrent < cItems && cSkip > 0)
  141.     {
  142.         m_iCurrent++;
  143.         cSkip--;
  144.     }
  145.     return cSkip == 0 ? S_OK : S_FALSE;
  146. }
  147. template<class T>
  148. HRESULT
  149. EnumIFacePtrs<T>::Reset(
  150.     VOID)
  151. {
  152.     m_iCurrent = 0;
  153.     return S_OK;
  154. }
  155. template<class T>
  156. HRESULT
  157. EnumIFacePtrs<T>::Clone(
  158.     EnumIFacePtrs<T> **ppEnum
  159.     )
  160. {
  161.     HRESULT hResult         = NO_ERROR;
  162.     EnumIFacePtrs<T> *pEnum = NULL;
  163.     if (NULL != ppEnum)
  164.     {
  165.         *ppEnum = NULL;
  166.         try
  167.         {        
  168.             pEnum = new EnumIFacePtrs<T>((const EnumIFacePtrs<T>&)*this);
  169.             *ppEnum = pEnum;
  170.         }
  171.         catch(OutOfMemory)
  172.         {
  173.             hResult = E_OUTOFMEMORY;
  174.         }
  175.         catch(...)
  176.         {
  177.             hResult = E_UNEXPECTED;
  178.         }
  179.         if (FAILED(hResult) && NULL != pEnum)
  180.         {
  181.             delete pEnum;
  182.             *ppEnum = NULL;
  183.         }
  184.     }
  185.     else
  186.         hResult = E_INVALIDARG;
  187.     return hResult;
  188. }
  189. #endif // __IFACEPTR_ENUMERATOR_TEMPLATE_H