1 #ifndef __ATLSTR_H__ 2 #define __ATLSTR_H__ 3 4 #pragma once 5 #include "atlbase.h" 6 #include "cstringt.h" 7 8 namespace ATL 9 { 10 11 class CAtlStringMgr : public IAtlStringMgr 12 { 13 protected: 14 IAtlMemMgr* m_MemMgr; 15 CNilStringData m_NilStrData; 16 17 public: 18 CAtlStringMgr(_In_opt_ IAtlMemMgr* MemMgr = NULL): m_MemMgr(MemMgr)19 m_MemMgr(MemMgr) 20 { 21 m_NilStrData.SetManager(this); 22 } 23 ~CAtlStringMgr(void)24 virtual ~CAtlStringMgr(void) 25 { 26 } 27 GetInstance(void)28 static IAtlStringMgr* GetInstance(void) 29 { 30 static CWin32Heap Win32Heap(::GetProcessHeap()); 31 static CAtlStringMgr StringMgr(&Win32Heap); 32 return &StringMgr; 33 } 34 Allocate(_In_ int NumChars,_In_ int CharSize)35 virtual _Ret_maybenull_ _Post_writable_byte_size_(sizeof(CStringData) + NumChars * CharSize) CStringData* Allocate( 36 _In_ int NumChars, 37 _In_ int CharSize) 38 { 39 size_t SizeBytes; 40 CStringData* StrData; 41 42 SizeBytes = sizeof(CStringData) + ((NumChars + 1) * CharSize); 43 44 StrData = static_cast<CStringData*>(m_MemMgr->Allocate(SizeBytes)); 45 if (StrData == NULL) return NULL; 46 47 StrData->pStringMgr = this; 48 StrData->nRefs = 1; 49 StrData->nAllocLength = NumChars; 50 StrData->nDataLength = 0; 51 52 return StrData; 53 } 54 Free(_In_ CStringData * StrData)55 virtual void Free(_In_ CStringData* StrData) 56 { 57 ATLASSERT(StrData->pStringMgr == this); 58 m_MemMgr->Free(StrData); 59 } 60 Reallocate(_Inout_ _Post_readable_byte_size_ (sizeof (CStringData))CStringData * StrData,_In_ int nChars,_In_ int nCharSize)61 virtual _Ret_maybenull_ _Post_writable_byte_size_(sizeof(CStringData) + nChars*nCharSize) CStringData* Reallocate( 62 _Inout_ _Post_readable_byte_size_(sizeof(CStringData)) CStringData* StrData, 63 _In_ int nChars, 64 _In_ int nCharSize) noexcept 65 { 66 ATLASSERT(StrData->pStringMgr == this); 67 68 CStringData* pNewData; 69 ULONG SizeBytes; 70 ULONG nDataBytes; 71 72 nChars++; 73 nDataBytes = nChars * nCharSize; 74 SizeBytes = sizeof(CStringData) + nDataBytes; 75 76 pNewData = static_cast<CStringData*>(m_MemMgr->Reallocate(StrData, SizeBytes)); 77 if (pNewData == NULL) return NULL; 78 79 pNewData->nAllocLength = nChars - 1; 80 return pNewData; 81 } GetNilString()82 virtual CStringData* GetNilString() noexcept 83 { 84 m_NilStrData.AddRef(); 85 return &m_NilStrData; 86 } Clone()87 virtual IAtlStringMgr* Clone() noexcept 88 { 89 return this; 90 } 91 92 private: StaticInitialize()93 static bool StaticInitialize() 94 { 95 GetInstance(); 96 return true; 97 } 98 }; 99 100 101 template< typename _CharType = wchar_t > 102 class ChTraitsOS : 103 public ChTraitsBase < _CharType > 104 { 105 106 }; 107 108 109 template<typename _BaseType = wchar_t, class StringIterator = ChTraitsOS<_BaseType> > 110 class StrTraitATL : 111 public StringIterator 112 { 113 public: FindStringResourceInstance(_In_ UINT nID)114 static HINSTANCE FindStringResourceInstance(_In_ UINT nID) noexcept 115 { 116 return AtlFindStringResourceInstance(nID); 117 } 118 GetDefaultManager()119 static IAtlStringMgr* GetDefaultManager() noexcept 120 { 121 return CAtlStringMgr::GetInstance(); 122 } 123 }; 124 125 126 typedef CStringT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT<wchar_t> > > CAtlStringW; 127 typedef CStringT< char, StrTraitATL< char, ChTraitsCRT<char> > > CAtlStringA; 128 129 130 typedef CAtlStringW CStringW; 131 typedef CAtlStringA CStringA; 132 133 134 #ifdef UNICODE 135 typedef CAtlStringW CAtlString; 136 typedef CStringW CString; 137 #else 138 typedef CAtlStringA CAtlString; 139 typedef CStringA CString; 140 #endif 141 142 143 } 144 145 #endif 146