pstream.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. {
  43. private:
  44. // Internal state:
  45. protected:
  46. DWORD mPS_dwFileVersion; // version number of file (being read)
  47. BOOL mPS_fDirty;
  48. public:
  49. // IPersistStream methods
  50. STDMETHODIMP IsDirty() {
  51. return (mPS_fDirty ? S_OK : S_FALSE); // note FALSE means clean
  52. }
  53. STDMETHODIMP Load(LPSTREAM pStm);
  54. STDMETHODIMP Save(LPSTREAM pStm, BOOL fClearDirty);
  55. STDMETHODIMP GetSizeMax(__out ULARGE_INTEGER * pcbSize)
  56. // Allow 24 bytes for version.
  57. {
  58. pcbSize->QuadPart = 12*sizeof(WCHAR)+SizeMax();
  59. return NOERROR;
  60. }
  61. // implementation
  62. CPersistStream(IUnknown *punk, __inout HRESULT *phr);
  63. ~CPersistStream();
  64. HRESULT SetDirty(BOOL fDirty) {
  65. mPS_fDirty = fDirty;
  66. return NOERROR;
  67. }
  68. // override to reveal IPersist & IPersistStream
  69. // STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv);
  70. // --- IPersist ---
  71. // You must override this to provide your own class id
  72. STDMETHODIMP GetClassID(__out CLSID *pClsid) PURE;
  73. // overrideable if you want
  74. // file version number. Override it if you ever change format
  75. virtual DWORD GetSoftwareVersion(void) {
  76. return 0;
  77. }
  78. //=========================================================================
  79. // OVERRIDE THESE to read and write your data
  80. // OVERRIDE THESE to read and write your data
  81. // OVERRIDE THESE to read and write your data
  82. virtual int SizeMax() {
  83. return 0;
  84. }
  85. virtual HRESULT WriteToStream(IStream *pStream);
  86. virtual HRESULT ReadFromStream(IStream *pStream);
  87. //=========================================================================
  88. private:
  89. };
  90. // --- Useful helpers ---
  91. // Writes an int to an IStream as UNICODE.
  92. STDAPI WriteInt(IStream *pIStream, int n);
  93. // inverse of WriteInt
  94. STDAPI_(int) ReadInt(IStream *pIStream, __out HRESULT &hr);
  95. #endif // __PSTREAM__