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 / chap08 / polyline / iperstmi.cpp < prev    next >
C/C++ Source or Header  |  1996-05-21  |  6KB  |  253 lines

  1. /*
  2.  * IPERSTMI.CPP
  3.  * Polyline Component Chapter 8
  4.  *
  5.  * Implementation of the IPersistStreamInit interface exposed on the
  6.  * Polyline object.
  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 "polyline.h"
  17.  
  18.  
  19. /*
  20.  * CImpIPersistStreamInit:CImpIPersistStreamInit
  21.  * CImpIPersistStreamInit::~CImpIPersistStreamInit
  22.  *
  23.  * Constructor Parameters:
  24.  *  pObj            PCPolyline pointing to the object we live in.
  25.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  26.  */
  27.  
  28. CImpIPersistStreamInit::CImpIPersistStreamInit(PCPolyline pObj
  29.     , LPUNKNOWN pUnkOuter)
  30.     {
  31.     m_cRef=0;
  32.     m_pObj=pObj;
  33.     m_pUnkOuter=pUnkOuter;
  34.     return;
  35.     }
  36.  
  37. CImpIPersistStreamInit::~CImpIPersistStreamInit(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44. /*
  45.  * CImpIPersistStreamInit::QueryInterface
  46.  * CImpIPersistStreamInit::AddRef
  47.  * CImpIPersistStreamInit::Release
  48.  *
  49.  * Purpose:
  50.  *  Delegating IUnknown members for CImpIPersistStreamInit.
  51.  */
  52.  
  53. STDMETHODIMP CImpIPersistStreamInit::QueryInterface(REFIID riid
  54.     , LPVOID *ppv)
  55.     {
  56.     return m_pUnkOuter->QueryInterface(riid, ppv);
  57.     }
  58.  
  59. STDMETHODIMP_(ULONG) CImpIPersistStreamInit::AddRef(void)
  60.     {
  61.     ++m_cRef;
  62.     return m_pUnkOuter->AddRef();
  63.     }
  64.  
  65. STDMETHODIMP_(ULONG) CImpIPersistStreamInit::Release(void)
  66.     {
  67.     --m_cRef;
  68.     return m_pUnkOuter->Release();
  69.     }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /*
  76.  * CImpIPersistStreamInit::GetClassID
  77.  *
  78.  * Purpose:
  79.  *  Returns the CLSID of the object represented by this interface.
  80.  *
  81.  * Parameters:
  82.  *  pClsID          LPCLSID in which to store our CLSID.
  83.  */
  84.  
  85. STDMETHODIMP CImpIPersistStreamInit::GetClassID(LPCLSID pClsID)
  86.     {
  87.     *pClsID=m_pObj->m_clsID;
  88.     return NOERROR;
  89.     }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. /*
  96.  * CImpIPersistStreamInit::IsDirty
  97.  *
  98.  * Purpose:
  99.  *  Tells the caller if we have made changes to this object since
  100.  *  it was loaded or initialized new.
  101.  *
  102.  * Parameters:
  103.  *  None
  104.  *
  105.  * Return Value:
  106.  *  HRESULT         Contains S_OK if we ARE dirty, S_FALSE if
  107.  *                  NOT dirty.
  108.  */
  109.  
  110. STDMETHODIMP CImpIPersistStreamInit::IsDirty(void)
  111.     {
  112.     return ResultFromScode(m_pObj->m_fDirty ? S_OK : S_FALSE);
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /*
  122.  * CImpIPersistStreamInit::Load
  123.  *
  124.  * Purpose:
  125.  *  Instructs the object to load itself from a previously saved
  126.  *  IStreamInit that was handled by Save in another object lifetime.
  127.  *  The seek pointer in this stream will be exactly the same as
  128.  *  it was when Save was called, and this function must leave
  129.  *  the seek pointer the same as it was on exit from Save, regardless
  130.  *  of success or failure.  This function should not hold on to
  131.  *  pIStream.
  132.  *
  133.  *  This function is called in lieu of IPersistStreamInit::InitNew
  134.  *  when the object already has a persistent state.
  135.  *
  136.  * Parameters:
  137.  *  pIStream        LPSTREAM from which to load.
  138.  */
  139.  
  140. STDMETHODIMP CImpIPersistStreamInit::Load(LPSTREAM pIStream)
  141.     {
  142.     POLYLINEDATA    pl;
  143.     ULONG           cb;
  144.     HRESULT         hr;
  145.  
  146.     if (NULL==pIStream)
  147.         return ResultFromScode(E_POINTER);
  148.  
  149.     //Read all the data into the POLYLINEDATA structure.
  150.     hr=pIStream->Read(&pl, CBPOLYLINEDATA, &cb);
  151.  
  152.     if (FAILED(hr) || CBPOLYLINEDATA!=cb)
  153.         return hr;
  154.  
  155.     m_pObj->m_pImpIPolyline->DataSet(&pl, TRUE, TRUE);
  156.     return NOERROR;
  157.     }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /*
  164.  * CImpIPersistStreamInit::Save
  165.  *
  166.  * Purpose:
  167.  *  Saves the data for this object to an IStreamInit.  Be sure not
  168.  *  to change the position of the seek pointer on entry to this
  169.  *  function: the caller will assume that you write from the
  170.  *  current offset.  Leave the stream's seek pointer at the end
  171.  *  of the data written on exit.
  172.  *
  173.  * Parameters:
  174.  *  pIStream        LPSTREAM in which to save our data.
  175.  *  fClearDirty     BOOL indicating if this call should clear
  176.  *                  the object's dirty flag (TRUE) or leave it
  177.  *                  unchanged (FALSE).
  178.  */
  179.  
  180. STDMETHODIMP CImpIPersistStreamInit::Save(LPSTREAM pIStream
  181.     , BOOL fClearDirty)
  182.     {
  183.     POLYLINEDATA    pl;
  184.     ULONG           cb;
  185.     HRESULT         hr;
  186.  
  187.     if (NULL==pIStream)
  188.         return ResultFromScode(E_POINTER);
  189.  
  190.     m_pObj->m_pImpIPolyline->DataGet(&pl);
  191.  
  192.     hr=pIStream->Write(&pl, CBPOLYLINEDATA, &cb);
  193.  
  194.     if (FAILED(hr) || CBPOLYLINEDATA!=cb)
  195.         return ResultFromScode(STG_E_WRITEFAULT);
  196.  
  197.     if (fClearDirty)
  198.         m_pObj->m_fDirty=FALSE;
  199.  
  200.     return NOERROR;
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /*
  211.  * CImpIPersistStreamInit::GetSizeMax
  212.  *
  213.  * Purpose:
  214.  *  Returns the size of the data we would write if Save was
  215.  *  called right now.
  216.  *
  217.  * Parameters:
  218.  *  pcbSize         ULARGE_INTEGER * in which to save the size
  219.  *                  of the stream an immediate call to Save would
  220.  *                  write.
  221.  */
  222.  
  223. STDMETHODIMP CImpIPersistStreamInit::GetSizeMax(ULARGE_INTEGER
  224.     *pcbSize)
  225.     {
  226.     if (NULL==pcbSize)
  227.         return ResultFromScode(E_POINTER);
  228.  
  229.     ULISet32(*pcbSize, CBPOLYLINEDATA);
  230.     return NOERROR;
  231.     }
  232.  
  233.  
  234.  
  235.  
  236. /*
  237.  * CImpIPersistStreamInit::InitNew
  238.  *
  239.  * Purpose:
  240.  *  Informs the object that it is being created new instead of
  241.  *  loaded from a persistent state.  This will be called in lieu
  242.  *  of IPersistStreamInit::Load.
  243.  *
  244.  * Parameters:
  245.  *  None
  246.  */
  247.  
  248. STDMETHODIMP CImpIPersistStreamInit::InitNew(void)
  249.     {
  250.     //Nothing for us to do
  251.     return NOERROR;
  252.     }
  253.