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.

209 lines
4.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: comstrm.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef COMSTRM_H
  11. #define COMSTRM_H
  12. #ifndef COMPTRS_H
  13. #include <comptrs.h>
  14. #endif
  15. #ifndef COMBSTR_H
  16. #include <combstr.h>
  17. #endif
  18. namespace microsoft {
  19. namespace com {
  20. class stream_ptr
  21. {
  22. // Construction
  23. public: stream_ptr() throw()
  24. // Sets the stream to NULL
  25. : m_pStream()
  26. {
  27. }
  28. public: stream_ptr(const stream_ptr& pStream) explicit throw()
  29. : m_pStream()
  30. {
  31. Initialize(pStream.m_pStream);
  32. }
  33. public: stream_ptr(IStream* pStream) explicit throw()
  34. // Saves the stream
  35. : m_pStream()
  36. {
  37. Initialize(pStream);
  38. }
  39. //REVIEW: add template constructors
  40. public: stream_ptr(HGLOBAL global) explicit throw()
  41. // Creates a stream on top of the global
  42. : m_pStream()
  43. {
  44. Initialize(global);
  45. }
  46. public: stream_ptr(LPCOLESTR filename) explicit throw()
  47. // Creates a stream on top of the specified file
  48. : m_pStream()
  49. {
  50. Initialize(filename);
  51. }
  52. public: stream_ptr(STGMEDIUM& stgMedium) explicit throw()
  53. // Saves the provided stream.
  54. : m_pStream()
  55. {
  56. Initialize(stgMedium);
  57. }
  58. public: stream_ptr(STGMEDIUM* pStgMedium) explicit throw()
  59. // Saves the provided stream.
  60. : m_pStream()
  61. {
  62. if (pStgMedium)
  63. Initialize(*pStgMedium);
  64. }
  65. //REVIEW: Add Create and Open functions
  66. //REVIEW: Add all of the assignment operators, cast operators, attach, detach, ->, *, etc.
  67. public: operator IStream*() const throw()
  68. {
  69. //REVIEW: trace on null would be helpful
  70. return m_pStream;
  71. }
  72. public: IStream* operator->() const throw()
  73. {
  74. //REVIEW: trace on null would be helpful
  75. return m_pStream;
  76. }
  77. public: IStream& operator*() const throw()
  78. {
  79. //REVIEW: trace on null would be helpful
  80. return *m_pStream;
  81. }
  82. // Write interfaces
  83. public: HRESULT Write(
  84. const void* pBuffer, unsigned long writeCount, unsigned long& written) throw()
  85. // Write the data contained in the buffer
  86. {
  87. if (m_pStream == NULL)
  88. return E_FAIL; //REVIEW: correct failure code?
  89. return m_pStream->Write(pBuffer, writeCount, &written);
  90. }
  91. public: HRESULT Write(const void* pBuffer, unsigned long writeCount) throw()
  92. {
  93. unsigned long written;
  94. return Write(pBuffer, writeCount, written);
  95. }
  96. public: HRESULT Write(const wchar_t* string) throw()
  97. {
  98. unsigned long len=wcslen(string)+1;
  99. return Write(string, len*sizeof(wchar_t), len);
  100. }
  101. public: HRESULT Write(const char* string) throw()
  102. {
  103. unsigned long len=strlen(string)+1;
  104. return Write(string, len, len);
  105. }
  106. public: HRESULT Write(const bstr& bstr) throw()
  107. {
  108. return Write(static_cast<const wchar_t*>(bstr));
  109. }
  110. //REVIEW: Read interfaces
  111. //REVIEW: Seek
  112. //REVIEW: Stat - broken out
  113. // Initialization. May be used by derived classes to setup the stream for
  114. // different types of storage mediums. These functions are all re-entrant,
  115. // and may be called at any time. They perform all of the appropriate
  116. // clean up and releasing of any resources in previous use.
  117. protected: void Initialize(HGLOBAL hg) throw()
  118. {
  119. //REVIEW: make re-entrant and bullet proof
  120. HRESULT const hr = CreateStreamOnHGlobal(hg, FALSE, &m_pStream);
  121. ASSERT(SUCCEEDED(hr));
  122. }
  123. protected: void Initialize(IStream* pStream) throw()
  124. {
  125. //REVIEW: make re-entrant and bullet proof
  126. m_pStream = pStream;
  127. }
  128. protected: void Initialize(LPCOLESTR filename) throw()
  129. {
  130. //REVIEW: make re-entrant and bullet proof
  131. #if 0 //REVIEW: need to create FileStream before this can be enabled
  132. if (!filename || !*filename)
  133. return false;
  134. cip<FileStream> fs = new CComObject<FileStream>;
  135. if (!fs)
  136. return false;
  137. HRESULT hr = fs->Open(filename);
  138. if (FAILED(hr))
  139. return false;
  140. m_pStream = fs;
  141. return true;
  142. #endif // 0
  143. }
  144. protected: void Initialize(STGMEDIUM& storage) throw()
  145. // Initializes the read/write functions based on the type of storage
  146. // medium. If there is a problem, the reader/writer is not set.
  147. {
  148. //REVIEW: make re-entrant and bullet proof
  149. switch (storage.tymed)
  150. {
  151. case TYMED_HGLOBAL:
  152. Initialize(storage.hGlobal);
  153. return;
  154. case TYMED_FILE:
  155. Initialize(storage.lpszFileName);
  156. return;
  157. case TYMED_ISTREAM:
  158. Initialize(storage.pstm);
  159. return;
  160. }
  161. }
  162. // Implementation
  163. private: IStreamCIP m_pStream;
  164. // This stream is created and used when the TYMED type is HGLOBAL.
  165. }; // class streamptr
  166. } // namespace com
  167. } // namespace microsoft
  168. #ifndef MICROSOFT_NAMESPACE_ON
  169. using namespace microsoft;
  170. #ifndef COM_NAMESPACE_ON
  171. using namespace com;
  172. #endif // COM_NAMESPACE_ON
  173. #endif // MICROSOFT_NAMESPACE_ON
  174. #endif // COMSTRM_H