Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1999 - 1999
  5. *
  6. * File: serial.h
  7. *
  8. * Contents: Object serialization class definitions
  9. *
  10. * History: 11-Feb-99 vivekj Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #pragma once
  14. #ifndef SERIAL_H
  15. #define SERIAL_H
  16. /*+-------------------------------------------------------------------------*
  17. * class CSerialObject
  18. *
  19. *
  20. * PURPOSE: Base class for objects that can be serialized.
  21. *
  22. *+-------------------------------------------------------------------------*/
  23. class CSerialObject
  24. {
  25. public:
  26. HRESULT Read (IStream &stm);
  27. protected: // implemented by the derived class
  28. // virtual CStr GetName() =0;
  29. virtual UINT GetVersion() =0;
  30. // return values for ReadSerialObject: S_OK: succeeded, S_FALSE: unknown version
  31. // E_UNEXPECTED: catastrophic error.
  32. virtual HRESULT ReadSerialObject (IStream &stm, UINT nVersion /*,LARGE_INTEGER nBytes*/) = 0;
  33. };
  34. /*+-------------------------------------------------------------------------*
  35. * class CSerialObjectRW
  36. *
  37. *
  38. * PURPOSE: Provided to separate from CSerialObject the "Write" functionality
  39. * which is much less frequently used
  40. *
  41. *+-------------------------------------------------------------------------*/
  42. class CSerialObjectRW : public CSerialObject
  43. {
  44. public:
  45. HRESULT Write(IStream &stm);
  46. protected: // implemented by the derived class
  47. virtual HRESULT WriteSerialObject(IStream &stm) = 0;
  48. };
  49. //############################################################################
  50. //############################################################################
  51. //
  52. // template functions - std::list class
  53. //
  54. //############################################################################
  55. //############################################################################
  56. template<class T, class Al>
  57. HRESULT Read(IStream& stm, std::list<T, Al>& l)
  58. {
  59. HRESULT hr = S_OK;
  60. try
  61. {
  62. int cSize;
  63. stm >> cSize;
  64. for(int i=0 ; i<cSize; i++)
  65. {
  66. T t;
  67. hr = t.Read(stm); // read the underlying object
  68. BREAK_ON_FAIL (hr);
  69. l.push_back(t); // add it to the list
  70. }
  71. }
  72. catch (_com_error& err)
  73. {
  74. hr = err.Error();
  75. ASSERT (false && "Caught _com_error");
  76. }
  77. return (hr);
  78. }
  79. template<class T, class Al>
  80. HRESULT Write(IStream& stm, std::list<T, Al>& l)
  81. {
  82. HRESULT hr = S_OK;
  83. try
  84. {
  85. int cSize = l.size();
  86. // write out the length
  87. stm << cSize;
  88. // write out the members
  89. for(std::list<T, Al>::iterator it = l.begin(); it != l.end(); ++it)
  90. {
  91. hr = it->Write (stm);
  92. BREAK_ON_FAIL (hr);
  93. }
  94. }
  95. catch (_com_error& err)
  96. {
  97. hr = err.Error();
  98. ASSERT (false && "Caught _com_error");
  99. }
  100. return (hr);
  101. }
  102. #endif // SERIAL_H