Leaked source code of windows server 2003
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.

183 lines
6.0 KiB

  1. #if !defined(_BCL_W32UNICODEINILINESTRINGBUFFER_H_INCLUDED_)
  2. #define _BCL_W32UNICODEINILINESTRINGBUFFER_H_INCLUDED_
  3. #pragma once
  4. #include <windows.h>
  5. #include <bcl_inlinestring.h>
  6. #include <bcl_unicodechartraits.h>
  7. #include <bcl_w32common.h>
  8. #include <bcl_w32baseunicodestringbuffer.h>
  9. namespace BCL
  10. {
  11. template <SIZE_T nInlineChars> class CWin32BaseUnicodeInlineStringBuffer;
  12. template <SIZE_T nInlineChars>
  13. class CWin32BaseUnicodeInlineStringBufferTraits : public CWin32BaseUnicodeStringBufferTraits<CWin32BaseUnicodeInlineStringBuffer<nInlineChars>, CWin32CallDisposition, BOOL>
  14. {
  15. typedef CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars> TThis;
  16. typedef CWin32BaseUnicodeInlineStringBuffer<nInlineChars> TBuffer;
  17. typedef void TAccessor;
  18. friend BCL::CPureString<TThis>;
  19. friend BCL::CInlineString<TThis>;
  20. friend BCL::CUnicodeCharTraits<TBuffer, TCallDisposition>;
  21. friend CWin32BaseUnicodeStringBufferTraits<CWin32BaseUnicodeInlineStringBuffer<nInlineChars>, CWin32CallDisposition, BOOL >;
  22. typedef BCL::CInlineString<TThis> TInlineString;
  23. typedef CWin32BaseUnicodeInlineStringBuffer<nInlineChars> TBuffer;
  24. static inline PWSTR __fastcall GetInlineBufferPtr(const BCL::CBaseString *p);
  25. static inline SIZE_T __fastcall GetInlineBufferCch(const BCL::CBaseString *p);
  26. static inline TConstantPair __fastcall InlineBufferPair(const BCL::CBaseString *p);
  27. static inline TMutablePair __fastcall InlineMutableBufferPair(const BCL::CBaseString *p);
  28. static inline CWin32CallDisposition ReallocateBuffer(BCL::CBaseString *p, SIZE_T cch);
  29. };
  30. template <SIZE_T nInlineChars>
  31. inline
  32. PWSTR
  33. __fastcall
  34. CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::GetInlineBufferPtr(
  35. const BCL::CBaseString *p
  36. )
  37. {
  38. return static_cast<const TBuffer *>(p)->GetInlineBufferPtr();
  39. }
  40. template <SIZE_T nInlineChars>
  41. inline
  42. SIZE_T
  43. __fastcall
  44. CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::GetInlineBufferCch(
  45. const BCL::CBaseString *p
  46. )
  47. {
  48. return nInlineChars;
  49. }
  50. template <SIZE_T nInlineChars>
  51. inline
  52. typename CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::TConstantPair
  53. __fastcall
  54. CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::InlineBufferPair(
  55. const BCL::CBaseString *p
  56. )
  57. {
  58. return TConstantPair(TThis::GetInlineBufferPtr(p), TThis::GetInlineBufferCch(p));
  59. }
  60. template <SIZE_T nInlineChars>
  61. inline
  62. typename CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::TMutablePair
  63. __fastcall
  64. CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::InlineMutableBufferPair(
  65. const BCL::CBaseString *p
  66. )
  67. {
  68. return TMutablePair(TThis::GetInlineBufferPtr(p), TThis::GetInlineBufferCch(p));
  69. }
  70. template <SIZE_T nInlineChars>
  71. inline
  72. CWin32CallDisposition
  73. CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>::ReallocateBuffer(
  74. BCL::CBaseString *p,
  75. SIZE_T cch
  76. )
  77. {
  78. TBuffer * const pBuffer = static_cast<TBuffer *>(p);
  79. const PWSTR pBufferPtr = pBuffer->GetMutableBufferPtr();
  80. if (cch < TThis::GetInlineBufferCch(p))
  81. {
  82. if (pBufferPtr != TBuffer::TTraits::GetInlineBufferPtr(p))
  83. {
  84. if (pBufferPtr != NULL)
  85. TThis::DeallocateBuffer(pBufferPtr);
  86. TThis::SetBufferPointerAndCount(p, TThis::GetInlineBufferPtr(p), TThis::GetInlineBufferCch(p));
  87. }
  88. }
  89. else
  90. {
  91. const TSizeT cb = cch * sizeof(WCHAR);
  92. PWSTR psz = NULL;
  93. if (cch != (cb / sizeof(WCHAR)))
  94. return CWin32CallDisposition::FromWin32Error(ERROR_ARITHMETIC_OVERFLOW);
  95. if ((pBuffer != NULL) && (pBufferPtr != TThis::GetInlineBufferPtr(p)))
  96. {
  97. ::SetLastError(ERROR_SUCCESS);
  98. psz =
  99. reinterpret_cast<PWSTR>(
  100. ::HeapReAlloc(
  101. ::GetProcessHeap(),
  102. 0,
  103. const_cast<PWSTR>(pBufferPtr),
  104. cb));
  105. if (psz == NULL)
  106. {
  107. const DWORD dwLastError = ::GetLastError();
  108. if (dwLastError == ERROR_SUCCESS)
  109. return CWin32CallDisposition::FromWin32Error(ERROR_OUTOFMEMORY);
  110. return CWin32CallDisposition::FromWin32Error(dwLastError);
  111. }
  112. }
  113. else
  114. {
  115. psz =
  116. reinterpret_cast<PWSTR>(
  117. ::HeapAlloc(
  118. ::GetProcessHeap(),
  119. 0,
  120. cb));
  121. }
  122. if (psz == NULL)
  123. return CWin32CallDisposition::FromWin32Error(ERROR_OUTOFMEMORY);
  124. pBuffer->SetBufferPointerAndCount(psz, cch);
  125. }
  126. return CWin32CallDisposition::FromWin32Error(ERROR_SUCCESS);
  127. }
  128. template <SIZE_T nInlineChars>
  129. class CWin32BaseUnicodeInlineStringBuffer : protected BCL::CInlineString<CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars> >, private BCL::CWin32BaseUnicodeStringBufferAddIn
  130. {
  131. public:
  132. typedef CWin32BaseUnicodeInlineStringBuffer<nInlineChars> TThis;
  133. typedef CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars> TTraits;
  134. public:
  135. inline CWin32BaseUnicodeInlineStringBuffer() : CWin32BaseUnicodeStringBufferAddIn(m_rgchInlineBuffer, nInlineChars) { m_rgchInlineBuffer[0] = L'\0'; }
  136. inline ~CWin32BaseUnicodeInlineStringBuffer() { TTraits::DeallocateDynamicBuffer(this); }
  137. operator PCWSTR() const { return this->GetStringPtr(); }
  138. #include <bcl_stringapi.h>
  139. private:
  140. inline PWSTR GetInlineBufferPtr() const { return const_cast<PWSTR>(m_rgchInlineBuffer); }
  141. WCHAR m_rgchInlineBuffer[nInlineChars];
  142. friend CWin32BaseUnicodeStringBufferTraits<TThis, CWin32CallDisposition, BOOL>;
  143. friend CWin32BaseUnicodeInlineStringBufferTraits<nInlineChars>;
  144. friend BCL::CUnicodeCharTraits<TThis, TCallDisposition>;
  145. }; // class CWin32BaseUnicodeInlineStringBuffer<>
  146. }; // namespace BCL
  147. #endif // !defined(_BCL_W32UNICODEINILINESTRINGBUFFER_H_INCLUDED_)