home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / interfac / ienumcon.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  5KB  |  252 lines

  1. /*
  2.  * IENUMCON.CPP
  3.  *
  4.  * Standard implementation of an enumerator with the
  5.  * IEnumConnections interface that will generally not need
  6.  * modification.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "ienumcon.h"
  17.  
  18.  
  19. /*
  20.  * CEnumConnections::CEnumConnections
  21.  * CEnumConnections::~CEnumConnections
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pUnkRef         LPUNKNOWN to use for reference counting.
  25.  *  cConn           ULONG number of connections in prgpConn
  26.  *  prgConnData     LPCONNECTDATA to the array to enumerate.
  27.  */
  28.  
  29. CEnumConnections::CEnumConnections(LPUNKNOWN pUnkRef, ULONG cConn
  30.     , LPCONNECTDATA prgConnData)
  31.     {
  32.     UINT        i;
  33.  
  34.     m_cRef=0;
  35.     m_pUnkRef=pUnkRef;
  36.  
  37.     m_iCur=0;
  38.     m_cConn=cConn;
  39.     m_rgConnData=new CONNECTDATA[(UINT)cConn];
  40.  
  41.     if (NULL!=m_rgConnData)
  42.         {
  43.         for (i=0; i < cConn; i++)
  44.             m_rgConnData[i]=prgConnData[i];
  45.         }
  46.  
  47.     return;
  48.     }
  49.  
  50.  
  51. CEnumConnections::~CEnumConnections(void)
  52.     {
  53.     if (NULL!=m_rgConnData)
  54.         delete [] m_rgConnData;
  55.  
  56.     return;
  57.     }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CEnumConnections::QueryInterface
  66.  * CEnumConnections::AddRef
  67.  * CEnumConnections::Release
  68.  *
  69.  * Purpose:
  70.  *  IUnknown members for CEnumConnections object.
  71.  */
  72.  
  73. STDMETHODIMP CEnumConnections::QueryInterface(REFIID riid
  74.     , LPVOID *ppv)
  75.     {
  76.     *ppv=NULL;
  77.  
  78.     /*
  79.      * Enumerators are separate objects, so we only need to support
  80.      * ou IUnknown and IEnumConnections interfaces here with no
  81.      * concern for aggregation.
  82.      */
  83.     if (IID_IUnknown==riid || IID_IEnumConnections==riid)
  84.         *ppv=(LPVOID)this;
  85.  
  86.     if (NULL!=*ppv)
  87.         {
  88.         ((LPUNKNOWN)*ppv)->AddRef();
  89.         return NOERROR;
  90.         }
  91.  
  92.     return ResultFromScode(E_NOINTERFACE);
  93.     }
  94.  
  95.  
  96. STDMETHODIMP_(ULONG) CEnumConnections::AddRef(void)
  97.     {
  98.     ++m_cRef;
  99.     m_pUnkRef->AddRef();
  100.     return m_cRef;
  101.     }
  102.  
  103. STDMETHODIMP_(ULONG) CEnumConnections::Release(void)
  104.     {
  105.     m_pUnkRef->Release();
  106.  
  107.     if (0L!=--m_cRef)
  108.         return m_cRef;
  109.  
  110.     delete this;
  111.     return 0;
  112.     }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /*
  119.  * CEnumConnections::Next
  120.  *
  121.  * Purpose:
  122.  *  Returns the next element in the enumeration.
  123.  *
  124.  * Parameters:
  125.  *  cConn           ULONG number of connections to return.
  126.  *  pConnData       LPCONNECTDATA in which to store the returned
  127.  *                  structures.
  128.  *  pulEnum         ULONG * in which to return how many we
  129.  *                  enumerated.
  130.  *
  131.  * Return Value:
  132.  *  HRESULT         NOERROR if successful, S_FALSE otherwise,
  133.  */
  134.  
  135. STDMETHODIMP CEnumConnections::Next(ULONG cConn
  136.     , LPCONNECTDATA pConnData, ULONG *pulEnum)
  137.     {
  138.     ULONG               cReturn=0L;
  139.  
  140.     if (NULL==m_rgConnData)
  141.         return ResultFromScode(S_FALSE);
  142.  
  143.     if (NULL==pulEnum)
  144.         {
  145.         if (1L!=cConn)
  146.             return ResultFromScode(E_POINTER);
  147.         }
  148.     else
  149.         *pulEnum=0L;
  150.  
  151.     if (NULL==pConnData || m_iCur >= m_cConn)
  152.         return ResultFromScode(S_FALSE);
  153.  
  154.     while (m_iCur < m_cConn && cConn > 0)
  155.         {
  156.         *pConnData++=m_rgConnData[m_iCur];
  157.         m_rgConnData[m_iCur++].pUnk->AddRef;
  158.         cReturn++;
  159.         cConn--;
  160.         }
  161.  
  162.     if (NULL!=pulEnum)
  163.         *pulEnum=cReturn;
  164.  
  165.     return NOERROR;
  166.     }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /*
  175.  * CEnumConnections::Skip
  176.  *
  177.  * Purpose:
  178.  *  Skips the next n elements in the enumeration.
  179.  *
  180.  * Parameters:
  181.  *  cSkip           ULONG number of elements to skip.
  182.  *
  183.  * Return Value:
  184.  *  HRESULT         NOERROR if successful, S_FALSE if we could not
  185.  *                  skip the requested number.
  186.  */
  187.  
  188. STDMETHODIMP CEnumConnections::Skip(ULONG cSkip)
  189.     {
  190.     if (((m_iCur+cSkip) >= m_cConn) || NULL==m_rgConnData)
  191.         return ResultFromScode(S_FALSE);
  192.  
  193.     m_iCur+=cSkip;
  194.     return NOERROR;
  195.     }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /*
  203.  * CEnumConnections::Reset
  204.  *
  205.  * Purpose:
  206.  *  Resets the current element index in the enumeration to zero.
  207.  *
  208.  * Parameters:
  209.  *  None
  210.  */
  211.  
  212. STDMETHODIMP CEnumConnections::Reset(void)
  213.     {
  214.     m_iCur=0;
  215.     return NOERROR;
  216.     }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. /*
  224.  * CEnumConnections::Clone
  225.  *
  226.  * Purpose:
  227.  *  Returns another IEnumConnections with the same state as ourselves.
  228.  *
  229.  * Parameters:
  230.  *  ppEnum          LPENUMCONNECTIONPOINTS * in which to return the
  231.  *                  new object.
  232.  */
  233.  
  234. STDMETHODIMP CEnumConnections::Clone(LPENUMCONNECTIONS *ppEnum)
  235.     {
  236.     PCEnumConnections   pNew;
  237.  
  238.     *ppEnum=NULL;
  239.  
  240.     //Create the clone
  241.     pNew=new CEnumConnections(m_pUnkRef, m_cConn, m_rgConnData);
  242.  
  243.     if (NULL==pNew)
  244.         return ResultFromScode(E_OUTOFMEMORY);
  245.  
  246.     pNew->AddRef();
  247.     pNew->m_iCur=m_iCur;
  248.  
  249.     *ppEnum=pNew;
  250.     return NOERROR;
  251.     }
  252.