pstream.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //------------------------------------------------------------------------------
  2. // File: PStream.h
  3. //
  4. // Desc: DirectShow base classes - defines a class for persistent properties
  5. // of filters.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #ifndef __PSTREAM__
  10. #define __PSTREAM__
  11. // Base class for persistent properties of filters
  12. // (i.e. filter properties in saved graphs)
  13. // The simplest way to use this is:
  14. // 1. Arrange for your filter to inherit this class
  15. // 2. Implement in your class WriteToStream and ReadFromStream
  16. // These will override the "do nothing" functions here.
  17. // 3. Change your NonDelegatingQueryInterface to handle IPersistStream
  18. // 4. Implement SizeMax to return the number of bytes of data you save.
  19. // If you save UNICODE data, don't forget a char is 2 bytes.
  20. // 5. Whenever your data changes, call SetDirty()
  21. //
  22. // At some point you may decide to alter, or extend the format of your data.
  23. // At that point you will wish that you had a version number in all the old
  24. // saved graphs, so that you can tell, when you read them, whether they
  25. // represent the old or new form. To assist you in this, this class
  26. // writes and reads a version number.
  27. // When it writes, it calls GetSoftwareVersion() to enquire what version
  28. // of the software we have at the moment. (In effect this is a version number
  29. // of the data layout in the file). It writes this as the first thing in the data.
  30. // If you want to change the version, implement (override) GetSoftwareVersion().
  31. // It reads this from the file into mPS_dwFileVersion before calling ReadFromStream,
  32. // so in ReadFromStream you can check mPS_dwFileVersion to see if you are reading
  33. // an old version file.
  34. // Normally you should accept files whose version is no newer than the software
  35. // version that's reading them.
  36. // CPersistStream
  37. //
  38. // Implements IPersistStream.
  39. // See 'OLE Programmers Reference (Vol 1):Structured Storage Overview' for
  40. // more implementation information.
  41. class CPersistStream : public IPersistStream {
  42. private:
  43. // Internal state:
  44. protected:
  45. DWORD mPS_dwFileVersion; // version number of file (being read)
  46. BOOL mPS_fDirty;
  47. public:
  48. // IPersistStream methods
  49. STDMETHODIMP IsDirty()
  50. {return (mPS_fDirty ? S_OK : S_FALSE);} // note FALSE means clean
  51. STDMETHODIMP Load(LPSTREAM pStm);
  52. STDMETHODIMP Save(LPSTREAM pStm, BOOL fClearDirty);
  53. STDMETHODIMP GetSizeMax(__out ULARGE_INTEGER * pcbSize)
  54. // Allow 24 bytes for version.
  55. { pcbSize->QuadPart = 12*sizeof(WCHAR)+SizeMax(); return NOERROR; }
  56. // implementation
  57. CPersistStream(IUnknown *punk, __inout HRESULT *phr);
  58. ~CPersistStream();
  59. HRESULT SetDirty(BOOL fDirty)
  60. { mPS_fDirty = fDirty; return NOERROR;}
  61. // override to reveal IPersist & IPersistStream
  62. // STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv);
  63. // --- IPersist ---
  64. // You must override this to provide your own class id
  65. STDMETHODIMP GetClassID(__out CLSID *pClsid) PURE;
  66. // overrideable if you want
  67. // file version number. Override it if you ever change format
  68. virtual DWORD GetSoftwareVersion(void) { return 0; }
  69. //=========================================================================
  70. // OVERRIDE THESE to read and write your data
  71. // OVERRIDE THESE to read and write your data
  72. // OVERRIDE THESE to read and write your data
  73. virtual int SizeMax() {return 0;}
  74. virtual HRESULT WriteToStream(IStream *pStream);
  75. virtual HRESULT ReadFromStream(IStream *pStream);
  76. //=========================================================================
  77. private:
  78. };
  79. // --- Useful helpers ---
  80. // Writes an int to an IStream as UNICODE.
  81. STDAPI WriteInt(IStream *pIStream, int n);
  82. // inverse of WriteInt
  83. STDAPI_(int) ReadInt(IStream *pIStream, __out HRESULT &hr);
  84. #endif // __PSTREAM__