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.

246 lines
8.3 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 1999
  5. *
  6. * File: tstring.h
  7. *
  8. * Contents: Interface file for tstring
  9. *
  10. * History: 28-Oct-98 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #ifndef TSTRING_H
  14. #define TSTRING_H
  15. #pragma once
  16. #include <string> // for std::wstring, std::string
  17. #include <objidl.h> // for IStream
  18. #include <commctrl.h>
  19. #include "mmc.h"
  20. #include "ndmgr.h" // for MMC_STRING_ID
  21. #include "ndmgrpriv.h"
  22. #include "stddbg.h" // for ASSERT
  23. #include "mmcptrs.h" // for IStringTablePrivatePtr
  24. /*+-------------------------------------------------------------------------*
  25. * tstring
  26. *
  27. * A tstring is a native-format (ANSI/Unicode) Standard C++ string that:
  28. *
  29. * 1. always persists itself in Unicode format, and
  30. * 2. supports LoadString, like MFC CStrings
  31. *
  32. * For ANSI, we provide IStream insertion and extraction operators that
  33. * will automatically convert to Unicode on stream insertion and from
  34. * Unicode on stream extraction.
  35. *
  36. * All base class member functions that return a base class instance (and
  37. * overloads), like substr:
  38. *
  39. * std::string std::string::substr (size_type pos, size_type n) const;
  40. *
  41. * must have forwarder functions here, so tstring supports such constructs
  42. * as:
  43. *
  44. * tstring strStuff;
  45. * *pStream << strStuff.substr(4, 6);
  46. *
  47. * If we didn't have forwarder functions in tstring, then an instance of
  48. * the base class type would be inserted in the stream instead of a tstring.
  49. * For Unicode, that wouldn't be a problem; but for ANSI, we'd end up
  50. * inserting a std::string into the stream in non-Unicode format. That
  51. * would defeat the purpose of having this class.
  52. *--------------------------------------------------------------------------*/
  53. #ifdef UNICODE
  54. typedef std::wstring tstring_BaseClass;
  55. #else
  56. typedef std::string tstring_BaseClass;
  57. #endif
  58. class tstring : public tstring_BaseClass
  59. {
  60. typedef tstring_BaseClass BaseClass;
  61. public:
  62. explicit tstring (const allocator_type& al = allocator_type());
  63. tstring (const tstring& other);
  64. tstring (const BaseClass& other);
  65. tstring (const tstring& other, size_type pos, size_type n);
  66. tstring (const BaseClass& other, size_type pos, size_type n);
  67. tstring (const TCHAR* psz);
  68. tstring (const TCHAR* psz, size_type n);
  69. tstring (size_type n, TCHAR ch);
  70. tstring (const_iterator first, const_iterator last);
  71. tstring& operator= (const tstring& other);
  72. tstring& operator= (const BaseClass& other);
  73. tstring& operator= (TCHAR ch);
  74. tstring& operator= (const TCHAR* psz);
  75. tstring& operator+= (const tstring& strToAppend);
  76. tstring& operator+= (const BaseClass& strToAppend);
  77. tstring& operator+= (TCHAR chToAppend);
  78. tstring& operator+= (const TCHAR* pszToAppend);
  79. tstring& append (const tstring& str);
  80. tstring& append (const BaseClass& str);
  81. tstring& append (const tstring& str, size_type pos, size_type n);
  82. tstring& append (const BaseClass& str, size_type pos, size_type n);
  83. tstring& append (const TCHAR* psz);
  84. tstring& append (const TCHAR* psz, size_type n);
  85. tstring& append (size_type n, TCHAR ch);
  86. tstring& append (const_iterator first, const_iterator last);
  87. tstring& assign (const tstring& str);
  88. tstring& assign (const BaseClass& str);
  89. tstring& assign (const tstring& str, size_type pos, size_type n);
  90. tstring& assign (const BaseClass& str, size_type pos, size_type n);
  91. tstring& assign (const TCHAR* psz);
  92. tstring& assign (const TCHAR* psz, size_type n);
  93. tstring& assign (size_type n, TCHAR ch);
  94. tstring& assign (const_iterator first, const_iterator last);
  95. tstring& insert (size_type p0, const tstring& str);
  96. tstring& insert (size_type p0, const BaseClass& str);
  97. tstring& insert (size_type p0, const tstring& str, size_type pos, size_type n);
  98. tstring& insert (size_type p0, const BaseClass& str, size_type pos, size_type n);
  99. tstring& insert (size_type p0, const TCHAR* psz, size_type n);
  100. tstring& insert (size_type p0, const TCHAR* psz);
  101. tstring& insert (size_type p0, size_type n, TCHAR ch);
  102. iterator insert (iterator it, TCHAR ch);
  103. void insert (iterator it, size_type n, TCHAR ch);
  104. void insert (iterator it, const_iterator first, const_iterator last);
  105. tstring& erase (size_type p0 = 0, size_type n = npos);
  106. iterator erase (iterator it);
  107. iterator erase (iterator first, iterator last);
  108. tstring& replace (size_type p0, size_type n0, const tstring& str);
  109. tstring& replace (size_type p0, size_type n0, const BaseClass& str);
  110. tstring& replace (size_type p0, size_type n0, const tstring& str, size_type pos, size_type n);
  111. tstring& replace (size_type p0, size_type n0, const BaseClass& str, size_type pos, size_type n);
  112. tstring& replace (size_type p0, size_type n0, const TCHAR* psz, size_type n);
  113. tstring& replace (size_type p0, size_type n0, const TCHAR* psz);
  114. tstring& replace (size_type p0, size_type n0, size_type n, TCHAR ch);
  115. tstring& replace (iterator first0, iterator last0, const tstring& str);
  116. tstring& replace (iterator first0, iterator last0, const BaseClass& str);
  117. tstring& replace (iterator first0, iterator last0, const TCHAR* psz, size_type n);
  118. tstring& replace (iterator first0, iterator last0, const TCHAR* psz);
  119. tstring& replace (iterator first0, iterator last0, size_type n, TCHAR ch);
  120. tstring& replace (iterator first0, iterator last0, const_iterator first, const_iterator last);
  121. tstring substr (size_type pos = 0, size_type n = npos) const;
  122. bool LoadString (HINSTANCE hInst, UINT nID);
  123. };
  124. #ifndef UNICODE
  125. IStream& operator>> (IStream& stm, tstring& task);
  126. IStream& operator<< (IStream& stm, const tstring& task);
  127. #endif // UNICODE
  128. /*+-------------------------------------------------------------------------*
  129. * CStringTableStringBase
  130. *
  131. *
  132. *--------------------------------------------------------------------------*/
  133. class CPersistor;
  134. class CStringTableStringBase
  135. {
  136. public:
  137. enum
  138. {
  139. eNoValue = -1,
  140. };
  141. CStringTableStringBase (IStringTablePrivate* pstp);
  142. CStringTableStringBase (const CStringTableStringBase& other);
  143. CStringTableStringBase (IStringTablePrivate* pstp, const tstring& str);
  144. CStringTableStringBase& operator= (const CStringTableStringBase& other);
  145. CStringTableStringBase& operator= (const tstring& str);
  146. CStringTableStringBase& operator= (LPCTSTR psz);
  147. virtual ~CStringTableStringBase ();
  148. MMC_STRING_ID CommitToStringTable () const;
  149. void RemoveFromStringTable () const;
  150. /* Call Detach if your string is deleted before the string table is.
  151. * Failing to do so will *remove* your string from the string table.*/
  152. void Detach()
  153. {
  154. m_id = eNoValue;
  155. }
  156. bool operator== (const CStringTableStringBase& other) const
  157. {
  158. ASSERT ((m_str == other.m_str) == (m_id == other.m_id));
  159. return (m_id == other.m_id);
  160. }
  161. bool operator!= (const CStringTableStringBase& other) const
  162. {
  163. return (!(*this == other));
  164. }
  165. bool operator== (const tstring& str) const
  166. {
  167. return (m_str == str);
  168. }
  169. bool operator!= (const tstring& str) const
  170. {
  171. return (m_str != str);
  172. }
  173. bool operator== (LPCTSTR psz) const
  174. {
  175. return (m_str == psz);
  176. }
  177. bool operator!= (LPCTSTR psz) const
  178. {
  179. return (m_str != psz);
  180. }
  181. LPCTSTR data() const
  182. { return (m_str.data()); }
  183. tstring str() const
  184. { return (m_str); }
  185. MMC_STRING_ID id() const
  186. { return (m_id); }
  187. private:
  188. void Assign (const CStringTableStringBase& other);
  189. mutable IStringTablePrivatePtr m_spStringTable;
  190. mutable MMC_STRING_ID m_id;
  191. tstring m_str;
  192. friend IStream& operator>> (IStream& stm, CStringTableStringBase& task);
  193. friend IStream& operator<< (IStream& stm, const CStringTableStringBase& task);
  194. friend class CPersistor;
  195. };
  196. template<class _E, class _Tr, class _Al>
  197. bool IsPartOfString (const std::basic_string<_E, _Tr, _Al>& str, const _E* psz)
  198. {
  199. return ((psz >= str.begin()) && (psz <= str.end()));
  200. }
  201. #include "tstring.inl"
  202. #endif /* TSTRING_H */