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 / chap04 / connect / object.h < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  188 lines

  1. /*
  2.  * OBJECT.H
  3.  *
  4.  * Definition of the classes CConnObject, CImpIConnPtCont, and
  5.  * CEnumConnectionPoints for the connectable object;
  6.  * CConnectionPoint, CImpIConnectionPoint, CImpIDuckEvents, and
  7.  * CEnumConnections for the connection point.
  8.  *
  9.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Microsoft
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #ifndef _OBJECT_H_
  18. #define _OBJECT_H_
  19.  
  20.  
  21. //Number of connection points in this implementation
  22. #define CCONNPOINTS     1
  23.  
  24.  
  25. /*
  26.  * The connectable object itself implements IUnknown and
  27.  * IConnectionPointContainer.  It is closely associated with
  28.  * the connection point enumerator, CEnumConnectionPoints.
  29.  */
  30.  
  31. class CConnectionPoint;
  32. typedef CConnectionPoint *PCConnectionPoint;
  33.  
  34.  
  35. //ID's for triggering events
  36. enum
  37.     {
  38.     EVENT_QUACK=1, EVENT_FLAP, EVENT_PADDLE
  39.     };
  40.  
  41.  
  42. class CConnObject : public IConnectionPointContainer
  43.     {
  44.     private:
  45.         DWORD             m_cRef;         //Object reference count
  46.  
  47.         //Array holding all the points we have.
  48.         PCConnectionPoint m_rgpConnPt[CCONNPOINTS];
  49.  
  50.     public:
  51.         CConnObject(void);
  52.         ~CConnObject(void);
  53.  
  54.         BOOL Init(void);
  55.  
  56.         //IUnknown members
  57.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  58.         STDMETHODIMP_(DWORD) AddRef(void);
  59.         STDMETHODIMP_(DWORD) Release(void);
  60.  
  61.         //IConnectionPointContainer members
  62.         STDMETHODIMP EnumConnectionPoints(IEnumConnectionPoints **);
  63.         STDMETHODIMP FindConnectionPoint(REFIID, IConnectionPoint **);
  64.  
  65.         //Other members
  66.         BOOL TriggerEvent(UINT);
  67.     };
  68.  
  69.  
  70. typedef CConnObject *PCConnObject;
  71.  
  72.  
  73.  
  74. //Enumerator class for EnumConnectionPoints
  75.  
  76. class CEnumConnectionPoints : public IEnumConnectionPoints
  77.     {
  78.     private:
  79.         ULONG           m_cRef;     //Object reference count
  80.         LPUNKNOWN       m_pUnkRef;  //IUnknown for ref counting
  81.         ULONG           m_iCur;     //Current element
  82.         ULONG           m_cPoints;  //Number of conn points
  83.         IConnectionPoint **m_rgpCP; //Source of conn points
  84.  
  85.     public:
  86.         CEnumConnectionPoints(LPUNKNOWN, ULONG, IConnectionPoint **);
  87.         ~CEnumConnectionPoints(void);
  88.  
  89.         //IUnknown members that delegate to m_pUnkRef.
  90.         STDMETHODIMP         QueryInterface(REFIID, LPVOID *);
  91.         STDMETHODIMP_(ULONG) AddRef(void);
  92.         STDMETHODIMP_(ULONG) Release(void);
  93.  
  94.         //IEnumConnectionPoints members
  95.         STDMETHODIMP Next(ULONG, IConnectionPoint **, ULONG *);
  96.         STDMETHODIMP Skip(ULONG);
  97.         STDMETHODIMP Reset(void);
  98.         STDMETHODIMP Clone(IEnumConnectionPoints **);
  99.     };
  100.  
  101. typedef CEnumConnectionPoints *PCEnumConnectionPoints;
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  * The connection point object iself is contained within the
  108.  * connection point container, which is the connectable object.
  109.  * It therefore manages a back pointer to that connectable object,
  110.  * and implement IConnectionPoint.  This object has a few
  111.  * member functions besides those in IConnectionPoint that are
  112.  * used to fire the outgoing calls.
  113.  */
  114.  
  115. #define CCONNMAX    2
  116.  
  117. class CConnectionPoint : public IConnectionPoint
  118.     {
  119.     private:
  120.         ULONG           m_cRef;     //Object reference count
  121.         PCConnObject    m_pObj;     //Containing object
  122.  
  123.         IID             m_iid;      //Our relevant interface
  124.  
  125.         /*
  126.          * To keep things simple we'll only support two
  127.          * advise connections at most.  Production quality
  128.          * connection points should supprt any number of
  129.          * connections.  For each connection we need to maintain
  130.          * the sink pointer and the cookie assigned to it.
  131.          */
  132.         IUnknown       *m_rgpIUnknown[CCONNMAX];
  133.         DWORD           m_rgdwCookies[CCONNMAX];
  134.  
  135.         UINT            m_cConn;
  136.         DWORD           m_dwCookieNext; //Counter
  137.  
  138.     public:
  139.         CConnectionPoint(PCConnObject, REFIID);
  140.         ~CConnectionPoint(void);
  141.  
  142.         //IUnknown members
  143.         STDMETHODIMP         QueryInterface(REFIID, LPVOID *);
  144.         STDMETHODIMP_(ULONG) AddRef(void);
  145.         STDMETHODIMP_(ULONG) Release(void);
  146.  
  147.         //IConnectionPoint members
  148.         STDMETHODIMP GetConnectionInterface(IID *);
  149.         STDMETHODIMP GetConnectionPointContainer
  150.             (IConnectionPointContainer **);
  151.         STDMETHODIMP Advise(LPUNKNOWN, DWORD *);
  152.         STDMETHODIMP Unadvise(DWORD);
  153.         STDMETHODIMP EnumConnections(IEnumConnections **);
  154.     };
  155.  
  156.  
  157. //Enumeration clas for EnumConnections
  158.  
  159. class CEnumConnections : public IEnumConnections
  160.     {
  161.     private:
  162.         ULONG           m_cRef;     //Object reference count
  163.         LPUNKNOWN       m_pUnkRef;  //IUnknown for ref counting
  164.         ULONG           m_iCur;     //Current element
  165.         ULONG           m_cConn;    //Number of connections
  166.         LPCONNECTDATA   m_rgConnData; //Source of connections
  167.  
  168.     public:
  169.         CEnumConnections(LPUNKNOWN, ULONG, LPCONNECTDATA);
  170.         ~CEnumConnections(void);
  171.  
  172.         //IUnknown members that delegate to m_pUnkRef.
  173.         STDMETHODIMP         QueryInterface(REFIID, LPVOID *);
  174.         STDMETHODIMP_(ULONG) AddRef(void);
  175.         STDMETHODIMP_(ULONG) Release(void);
  176.  
  177.         //IEnumConnections members
  178.         STDMETHODIMP Next(ULONG, LPCONNECTDATA, ULONG *);
  179.         STDMETHODIMP Skip(ULONG);
  180.         STDMETHODIMP Reset(void);
  181.         STDMETHODIMP Clone(IEnumConnections **);
  182.     };
  183.  
  184. typedef CEnumConnections *PCEnumConnections;
  185.  
  186.  
  187. #endif _OBJECT_H_
  188.