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 / chap18 / cosmo / iclassf.cpp < prev    next >
C/C++ Source or Header  |  1996-05-23  |  4KB  |  190 lines

  1. /*
  2.  * ICLASSF.CPP
  3.  * Cosmo Chapter 18
  4.  *
  5.  * Implementation of Cosmo's class factory.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "cosmo.h"
  16.  
  17.  
  18. /*
  19.  * CFigureClassFactory::CFigureClassFactory
  20.  * CFigureClassFactory::~CFigureClassFactory
  21.  *
  22.  * Constructor Parameters:
  23.  *  pFR             PCCosmoFrame that can create documents.
  24.  */
  25.  
  26. CFigureClassFactory::CFigureClassFactory(PCCosmoFrame pFR)
  27.     {
  28.     m_cRef=0L;
  29.     m_pFR=pFR;
  30.     m_fCreated=FALSE;
  31.     return;
  32.     }
  33.  
  34.  
  35. CFigureClassFactory::~CFigureClassFactory(void)
  36.     {
  37.     return;
  38.     }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CFigureClassFactory::QueryInterface
  47.  * CFigureClassFactory::AddRef
  48.  * CFigureClassFactory::Release
  49.  */
  50.  
  51. STDMETHODIMP CFigureClassFactory::QueryInterface(REFIID riid
  52.     , PPVOID ppv)
  53.     {
  54.     *ppv=NULL;
  55.  
  56.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  57.         *ppv=this;
  58.  
  59.     if (NULL!=*ppv)
  60.         {
  61.         ((LPUNKNOWN)*ppv)->AddRef();
  62.         return NOERROR;
  63.         }
  64.  
  65.     return ResultFromScode(E_NOINTERFACE);
  66.     }
  67.  
  68.  
  69. STDMETHODIMP_(ULONG) CFigureClassFactory::AddRef(void)
  70.     {
  71.     return ++m_cRef;
  72.     }
  73.  
  74.  
  75. STDMETHODIMP_(ULONG) CFigureClassFactory::Release(void)
  76.     {
  77.     if (0!=--m_cRef)
  78.         return m_cRef;
  79.  
  80.     delete this;
  81.     return 0;
  82.     }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /*
  91.  * CFigureClassFactory::CreateInstance
  92.  *
  93.  * Purpose:
  94.  *  Instantiates a Figure object that supports embedding.
  95.  *
  96.  * Parameters:
  97.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
  98.  *                  being used in an aggregation.
  99.  *  riid            REFIID identifying the interface the caller
  100.  *                  desires to have for the new object.
  101.  *  ppvObj          PPVOID in which to store the desired
  102.  *                  interface pointer for the new object.
  103.  *
  104.  * Return Value:
  105.  *  HRESULT         NOERROR if successful, otherwise contains
  106.  *                  E_NOINTERFACE if we cannot support the requested
  107.  *                  interface.
  108.  */
  109.  
  110. STDMETHODIMP CFigureClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
  111.     , REFIID riid, PPVOID ppvObj)
  112.     {
  113.     PCCosmoDoc          pDoc;
  114.     HRESULT             hr;
  115.  
  116.     *ppvObj=NULL;
  117.  
  118.     //Great idea to protect yourself from multiple creates here.
  119.     if (m_fCreated)
  120.         return ResultFromScode(E_UNEXPECTED);
  121.  
  122.     m_fCreated=TRUE;
  123.     hr=ResultFromScode(E_OUTOFMEMORY);
  124.  
  125.     //We don't support aggregation
  126.     if (NULL!=pUnkOuter)
  127.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  128.  
  129.     //Try creating a new document, which creates the object.
  130.     pDoc=(PCCosmoDoc)m_pFR->m_pCL->NewDocument(TRUE);
  131.  
  132.     if (NULL==pDoc)
  133.         {
  134.         //This will cause shutdown; object count will go to zero.
  135.         g_cObj++;
  136.         ObjectDestroyed();
  137.         return hr;
  138.         }
  139.  
  140.     //Insure the document is untitled; get the requested interface.
  141.     pDoc->Load(TRUE, NULL);
  142.     pDoc->m_pFigure->FrameSet(m_pFR);
  143.     hr=pDoc->m_pFigure->QueryInterface(riid, ppvObj);
  144.  
  145.     //Closing the document will destroy the objec, cause shutdown.
  146.     if (FAILED(hr))
  147.         {
  148.         m_pFR->m_pCL->CloseDocument(pDoc);
  149.         return hr;
  150.         }
  151.  
  152.     return NOERROR;
  153.     }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /*
  161.  * CFigureClassFactory::LockServer
  162.  *
  163.  * Purpose:
  164.  *  Increments or decrements the lock count of the serving
  165.  *  IClassFactory object.  When the number of locks goes to
  166.  *  zero and the number of objects is zero, we shut down the
  167.  *  application.
  168.  *
  169.  * Parameters:
  170.  *  fLock           BOOL specifying whether to increment or
  171.  *                  decrement the lock count.
  172.  *
  173.  * Return Value:
  174.  *  HRESULT         NOERROR always.
  175.  */
  176.  
  177. STDMETHODIMP CFigureClassFactory::LockServer(BOOL fLock)
  178.     {
  179.     if (fLock)
  180.         g_cLock++;
  181.     else
  182.         {
  183.         g_cLock--;
  184.         g_cObj++;
  185.         ObjectDestroyed();
  186.         }
  187.  
  188.     return NOERROR;
  189.     }
  190.