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 / chap10 / polyline / iperstmi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  5.6 KB  |  259 lines

  1. /*
  2.  * IPERSTMI.CPP
  3.  * Polyline Component Chapter 10
  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.     //CHAPTER10MOD
  156.     //DataSet now internal on Polyline
  157.     m_pObj->DataSet(&pl, TRUE, TRUE);
  158.     //End CHAPTER10MOD
  159.     return NOERROR;
  160.     }
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.  * CImpIPersistStreamInit::Save
  168.  *
  169.  * Purpose:
  170.  *  Saves the data for this object to an IStreamInit.  Be sure not
  171.  *  to change the position of the seek pointer on entry to this
  172.  *  function: the caller will assume that you write from the
  173.  *  current offset.  Leave the stream's seek pointer at the end
  174.  *  of the data written on exit.
  175.  *
  176.  * Parameters:
  177.  *  pIStream        LPSTREAM in which to save our data.
  178.  *  fClearDirty     BOOL indicating if this call should clear
  179.  *                  the object's dirty flag (TRUE) or leave it
  180.  *                  unchanged (FALSE).
  181.  */
  182.  
  183. STDMETHODIMP CImpIPersistStreamInit::Save(LPSTREAM pIStream
  184.     , BOOL fClearDirty)
  185.     {
  186.     POLYLINEDATA    pl;
  187.     ULONG           cb;
  188.     HRESULT         hr;
  189.  
  190.     if (NULL==pIStream)
  191.         return ResultFromScode(E_POINTER);
  192.  
  193.     //CHAPTER10MOD
  194.     //DataGet now internal on Polyline
  195.     m_pObj->DataGet(&pl);
  196.     //End CHAPTER10MOD
  197.  
  198.     hr=pIStream->Write(&pl, CBPOLYLINEDATA, &cb);
  199.  
  200.     if (FAILED(hr) || CBPOLYLINEDATA!=cb)
  201.         return ResultFromScode(STG_E_WRITEFAULT);
  202.  
  203.     if (fClearDirty)
  204.         m_pObj->m_fDirty;
  205.  
  206.     return NOERROR;
  207.     }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /*
  217.  * CImpIPersistStreamInit::GetSizeMax
  218.  *
  219.  * Purpose:
  220.  *  Returns the size of the data we would write if Save was
  221.  *  called right now.
  222.  *
  223.  * Parameters:
  224.  *  pcbSize         ULARGE_INTEGER * in which to save the size
  225.  *                  of the stream an immediate call to Save would
  226.  *                  write.
  227.  */
  228.  
  229. STDMETHODIMP CImpIPersistStreamInit::GetSizeMax(ULARGE_INTEGER
  230.     *pcbSize)
  231.     {
  232.     if (NULL==pcbSize)
  233.         return ResultFromScode(E_POINTER);
  234.  
  235.     ULISet32(*pcbSize, CBPOLYLINEDATA);
  236.     return NOERROR;
  237.     }
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  * CImpIPersistStreamInit::InitNew
  244.  *
  245.  * Purpose:
  246.  *  Informs the object that it is being created new instead of
  247.  *  loaded from a persistent state.  This will be called in lieu
  248.  *  of IPersistStreamInit::Load.
  249.  *
  250.  * Parameters:
  251.  *  None
  252.  */
  253.  
  254. STDMETHODIMP CImpIPersistStreamInit::InitNew(void)
  255.     {
  256.     //Nothing for us to do
  257.     return NOERROR;
  258.     }
  259.