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 / ienumcp.cpp < prev    next >
C/C++ Source or Header  |  1996-05-21  |  5KB  |  257 lines

  1. /*
  2.  * IENUMCP.CPP
  3.  *
  4.  * Standard implementation of an IConnectionPoint enumerator with the
  5.  * IEnumConnectionPoints 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 "ienumcp.h"
  17.  
  18.  
  19. /*
  20.  * CEnumConnectionPoints::CEnumConnectionPoints
  21.  * CEnumConnectionPoints::~CEnumConnectionPoints
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pUnkRef         LPUNKNOWN to use for reference counting.
  25.  *  cPoints         ULONG number of connection points in rgpCP
  26.  *  rgpCP           LPCONNECTIONPOINT * to the array to enumerate.
  27.  */
  28.  
  29. CEnumConnectionPoints::CEnumConnectionPoints(LPUNKNOWN pUnkRef
  30.     , ULONG cPoints, LPCONNECTIONPOINT *rgpCP)
  31.     {
  32.     UINT        i;
  33.  
  34.     m_cRef=0;
  35.     m_pUnkRef=pUnkRef;
  36.  
  37.     m_iCur=0;
  38.     m_cPoints=cPoints;
  39.     m_rgpCP=new LPCONNECTIONPOINT[(UINT)cPoints];
  40.  
  41.     if (NULL!=m_rgpCP)
  42.         {
  43.         for (i=0; i < cPoints; i++)
  44.             m_rgpCP[i]=rgpCP[i];
  45.         }
  46.  
  47.     return;
  48.     }
  49.  
  50.  
  51. CEnumConnectionPoints::~CEnumConnectionPoints(void)
  52.     {
  53.     if (NULL!=m_rgpCP)
  54.         delete [] m_rgpCP;
  55.  
  56.     return;
  57.     }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CEnumConnectionPoints::QueryInterface
  66.  * CEnumConnectionPoints::AddRef
  67.  * CEnumConnectionPoints::Release
  68.  *
  69.  * Purpose:
  70.  *  IUnknown members for CEnumConnectionPoints object.
  71.  */
  72.  
  73. STDMETHODIMP CEnumConnectionPoints::QueryInterface(REFIID riid
  74.     , LPVOID *ppv)
  75.     {
  76.     *ppv=NULL;
  77.  
  78.     /*
  79.      * Enumerators are separate objects, not the data object, so
  80.      * we only need to support out IUnknown and IEnumConnectionPoints
  81.      * interfaces here with no concern for aggregation.
  82.      */
  83.     if (IID_IUnknown==riid || IID_IEnumConnectionPoints==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) CEnumConnectionPoints::AddRef(void)
  97.     {
  98.     ++m_cRef;
  99.     m_pUnkRef->AddRef();
  100.     return m_cRef;
  101.     }
  102.  
  103. STDMETHODIMP_(ULONG) CEnumConnectionPoints::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.  * CEnumConnectionPoints::Next
  120.  *
  121.  * Purpose:
  122.  *  Returns the next element in the enumeration.
  123.  *
  124.  * Parameters:
  125.  *  cPoints         ULONG number of connection points to return.
  126.  *  pCP             LPCONNECTIONPOINT in which to store the returned
  127.  *                  pointers.
  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 CEnumConnectionPoints::Next(ULONG cPoints
  136.     , LPCONNECTIONPOINT *pCP, ULONG *pulEnum)
  137.     {
  138.     ULONG               cReturn=0L;
  139.  
  140.     if (NULL==m_rgpCP)
  141.         return ResultFromScode(S_FALSE);
  142.  
  143.     if (NULL==pulEnum)
  144.         {
  145.         if (1L!=cPoints)
  146.             return ResultFromScode(E_POINTER);
  147.         }
  148.     else
  149.         *pulEnum=0L;
  150.  
  151.     if (NULL==pCP || m_iCur >= m_cPoints)
  152.         return ResultFromScode(S_FALSE);
  153.  
  154.     while (m_iCur < m_cPoints && cPoints > 0)
  155.         {
  156.         *pCP=m_rgpCP[m_iCur++];
  157.  
  158.         if (NULL!=*pCP)
  159.             (*pCP)->AddRef();
  160.  
  161.         pCP++;
  162.         cReturn++;
  163.         cPoints--;
  164.         }
  165.  
  166.     if (NULL!=pulEnum)
  167.         *pulEnum=cReturn;
  168.  
  169.     return NOERROR;
  170.     }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /*
  179.  * CEnumConnectionPoints::Skip
  180.  *
  181.  * Purpose:
  182.  *  Skips the next n elements in the enumeration.
  183.  *
  184.  * Parameters:
  185.  *  cSkip           ULONG number of elements to skip.
  186.  *
  187.  * Return Value:
  188.  *  HRESULT         NOERROR if successful, S_FALSE if we could not
  189.  *                  skip the requested number.
  190.  */
  191.  
  192. STDMETHODIMP CEnumConnectionPoints::Skip(ULONG cSkip)
  193.     {
  194.     if (((m_iCur+cSkip) >= m_cPoints) || NULL==m_rgpCP)
  195.         return ResultFromScode(S_FALSE);
  196.  
  197.     m_iCur+=cSkip;
  198.     return NOERROR;
  199.     }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. /*
  207.  * CEnumConnectionPoints::Reset
  208.  *
  209.  * Purpose:
  210.  *  Resets the current element index in the enumeration to zero.
  211.  *
  212.  * Parameters:
  213.  *  None
  214.  */
  215.  
  216. STDMETHODIMP CEnumConnectionPoints::Reset(void)
  217.     {
  218.     m_iCur=0;
  219.     return NOERROR;
  220.     }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. /*
  228.  * CEnumConnectionPoints::Clone
  229.  *
  230.  * Purpose:
  231.  *  Returns another IEnumConnectionPoints with the same state as ourselves.
  232.  *
  233.  * Parameters:
  234.  *  ppEnum          LPENUMCONNECTIONPOINTS * in which to return the
  235.  *                  new object.
  236.  */
  237.  
  238. STDMETHODIMP CEnumConnectionPoints::Clone
  239.     (LPENUMCONNECTIONPOINTS *ppEnum)
  240.     {
  241.     PCEnumConnectionPoints   pNew;
  242.  
  243.     *ppEnum=NULL;
  244.  
  245.     //Create the clone
  246.     pNew=new CEnumConnectionPoints(m_pUnkRef, m_cPoints, m_rgpCP);
  247.  
  248.     if (NULL==pNew)
  249.         return ResultFromScode(E_OUTOFMEMORY);
  250.  
  251.     pNew->AddRef();
  252.     pNew->m_iCur=m_iCur;
  253.  
  254.     *ppEnum=pNew;
  255.     return NOERROR;
  256.     }
  257.