[ATL]
[reactos.git] / reactos / lib / atl / atlstr.h
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):
19 m_MemMgr(MemMgr)
20 {
21 m_NilStrData.SetManager(this);
22 }
23
24 virtual ~CAtlStringMgr(void)
25 {
26 }
27
28 static IAtlStringMgr* GetInstance(void)
29 {
30 static CWin32Heap Win32Heap(::GetProcessHeap());
31 static CAtlStringMgr StringMgr(&Win32Heap);
32 return &StringMgr;
33 }
34
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
55 virtual void Free(_In_ CStringData* StrData)
56 {
57 ATLASSERT(StrData->pStringMgr == this);
58 m_MemMgr->Free(StrData);
59 }
60
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) throw()
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 }
82 virtual CStringData* GetNilString() throw()
83 {
84 m_NilStrData.AddRef();
85 return &m_NilStrData;
86 }
87 virtual IAtlStringMgr* Clone() throw()
88 {
89 return this;
90 }
91
92 private:
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:
114 static HINSTANCE FindStringResourceInstance(_In_ UINT nID) throw()
115 {
116 return AtlFindStringResourceInstance(nID);
117 }
118
119 static IAtlStringMgr* GetDefaultManager() throw()
120 {
121 return CAtlStringMgr::GetInstance();
122 }
123 };
124
125
126 typedef CStringT<wchar_t, StrTraitATL<wchar_t, ChTraitsCRT<wchar_t>>> CAtlStringW;
127
128 }
129
130 #endif