|
3 | 3 |
|
4 | 4 | #pragma once |
5 | 5 |
|
6 | | -#ifndef FTL_BASE_H |
7 | | -# error ftlConversion.h requires ftlbase.h to be included first |
8 | | -#endif |
| 6 | +#include "ftlDefine.h" |
9 | 7 |
|
10 | 8 | /************************************************************************************************************ |
11 | 9 | * CodePage -- 代码页,是Windows为不同的字符编码方案所分配的一个数字编号。 |
|
36 | 34 |
|
37 | 35 | namespace FTL |
38 | 36 | { |
| 37 | +#ifndef DEFAULT_MEMALLOCATOR_FIXED_COUNT |
| 38 | +#define DEFAULT_MEMALLOCATOR_FIXED_COUNT 32 |
| 39 | +#endif |
| 40 | + |
| 41 | + //! 方便的管理需要分配的临时内存,在类的构造中分配内存,析构中释放 |
| 42 | + //! 是否可能生成小的内存碎片?使用Pool优化? |
| 43 | + enum MemoryAllocType |
| 44 | + { |
| 45 | + matNew, //使用new分配,使用delete释放,为了方便管理,即使只分配一个,也使用数组方式 |
| 46 | + matVirtualAlloc, //使用VirtualAlloc直接在进程的地址空间中保留一快内存(既非堆又非栈),速度快 |
| 47 | + matLocalAlloc, //使用LocalAlloc分配,LocalFree释放,主要是为了兼容老的程序? |
| 48 | + //matMalloca, //使用 _malloca 在栈上分配,通过 _freea 释放 |
| 49 | + }; |
| 50 | + |
| 51 | + template <typename T, UINT DefaultFixedCount = DEFAULT_MEMALLOCATOR_FIXED_COUNT, MemoryAllocType allocType = matNew> |
| 52 | + class CFMemAllocator |
| 53 | + { |
| 54 | + //和ATL中的 CTempBuffer 模板类比较 |
| 55 | + DISABLE_COPY_AND_ASSIGNMENT(CFMemAllocator); |
| 56 | + public: |
| 57 | + FTLINLINE CFMemAllocator(); |
| 58 | + FTLINLINE CFMemAllocator(DWORD nCount, BOOL bAlignment = FALSE); |
| 59 | + FTLINLINE ~CFMemAllocator(); |
| 60 | + FTLINLINE T* GetMemory(UINT nMaxSize); |
| 61 | + FTLINLINE T* GetMemory(); |
| 62 | + FTLINLINE operator T*() |
| 63 | + { |
| 64 | + if (!m_pMem && m_nCount <= DefaultFixedCount) |
| 65 | + { |
| 66 | + return m_FixedMem; |
| 67 | + } |
| 68 | + return m_pMem; |
| 69 | + } |
| 70 | + |
| 71 | + FTLINLINE T* Detatch(); |
| 72 | + FTLINLINE UINT GetCount() const; |
| 73 | + protected: |
| 74 | + FTLINLINE VOID _Init(DWORD nCount, BOOL bAlignment); |
| 75 | + FTLINLINE UINT _AdjustAlignmentSize(UINT nCount); |
| 76 | + FTLINLINE VOID _FreeMemory(); |
| 77 | + FTLINLINE UINT _GetBlockSize(UINT nMaxCount); |
| 78 | + private: |
| 79 | + T* m_pMem; |
| 80 | + T m_FixedMem[DefaultFixedCount]; |
| 81 | + MemoryAllocType m_allocType; |
| 82 | + UINT m_nCount; |
| 83 | + BOOL m_bAlignment; |
| 84 | + }; |
| 85 | + |
| 86 | + //! 字符串格式化,可以根据传入的格式化字符长度自动调整,析构时释放分配的内存 |
| 87 | + class CFStringFormater |
| 88 | + { |
| 89 | + DISABLE_COPY_AND_ASSIGNMENT(CFStringFormater); |
| 90 | + public: |
| 91 | + //可以分配的最大内存空间为 : dwInitAllocLength * dwMaxBufferTimes(注意:dwMaxBufferTimes 最好是2的倍数) |
| 92 | + FTLINLINE CFStringFormater(DWORD dwMaxBufferLength = MAX_BUFFER_LENGTH); |
| 93 | + FTLINLINE virtual ~CFStringFormater(); |
| 94 | + FTLINLINE BOOL Reset(DWORD dwNewLength = 0); |
| 95 | + FTLINLINE VOID Clear(); //清除内容 |
| 96 | + FTLINLINE HRESULT __cdecl Format(LPCTSTR lpszFormat, ...); |
| 97 | + FTLINLINE HRESULT __cdecl FormatV(LPCTSTR lpszFormat, va_list argList); |
| 98 | + FTLINLINE HRESULT __cdecl AppendFormat(LPCTSTR lpszFormat, ...); |
| 99 | + FTLINLINE HRESULT __cdecl AppendFormatV(LPCTSTR lpszFormat, va_list argList); |
| 100 | + |
| 101 | + //各种支持函数的定义 |
| 102 | +#if 0 |
| 103 | + const CFStringFormater& operator=(const CFStringFormater& src); |
| 104 | + const CFStringFormater& operator=(const TCHAR ch); |
| 105 | + const CFStringFormater& operator=(LPCTSTR pstr); |
| 106 | +# ifdef _UNICODE |
| 107 | + const CFStringFormater& CFStringFormater::operator=(LPCSTR lpStr); |
| 108 | + const CFStringFormater& CFStringFormater::operator+=(LPCSTR lpStr); |
| 109 | +# else |
| 110 | + const CFStringFormater& CFStringFormater::operator=(LPCWSTR lpwStr); |
| 111 | + const CFStringFormater& CFStringFormater::operator+=(LPCWSTR lpwStr); |
| 112 | +# endif |
| 113 | + CFStringFormater operator+(const CFStringFormater& src) const; |
| 114 | + CFStringFormater operator+(LPCTSTR pstr) const; |
| 115 | + const CFStringFormater& operator+=(const CFStringFormater& src); |
| 116 | + const CFStringFormater& operator+=(LPCTSTR pstr); |
| 117 | + const CFStringFormater& operator+=(const TCHAR ch); |
| 118 | + TCHAR operator[] (int nIndex) const; |
| 119 | + bool operator == (LPCTSTR str) const; |
| 120 | + bool operator != (LPCTSTR str) const; |
| 121 | + bool operator <= (LPCTSTR str) const; |
| 122 | + bool operator < (LPCTSTR str) const; |
| 123 | + bool operator >= (LPCTSTR str) const; |
| 124 | + bool operator > (LPCTSTR str) const; |
| 125 | +#endif |
| 126 | + |
| 127 | + FTLINLINE operator LPCTSTR() const |
| 128 | + { |
| 129 | + return m_pBuf; |
| 130 | + } |
| 131 | + FTLINLINE LPCTSTR GetString() const; |
| 132 | + FTLINLINE LPTSTR GetString(); |
| 133 | + FTLINLINE LONG GetStringLength() const; |
| 134 | + FTLINLINE LONG GetSize() const; |
| 135 | + FTLINLINE LPTSTR Detach(); |
| 136 | + protected: |
| 137 | + TCHAR m_szInitBuf[DEFAULT_BUFFER_LENGTH]; |
| 138 | + LPTSTR m_pBuf; |
| 139 | + DWORD m_dwTotalBufferLength; |
| 140 | + const DWORD m_dwMaxBufferLength; |
| 141 | + FTLINLINE HRESULT __cdecl _InnerFormatV(LPTSTR pszBuf, DWORD dwBufLength, BOOL bAppend, LPCTSTR lpszFormat, va_list argList); |
| 142 | + FTLINLINE VOID _CheckAndReleaseBuf(); |
| 143 | + }; |
| 144 | + |
39 | 145 | class CFConversion |
40 | 146 | { |
41 | 147 | public: |
@@ -85,6 +191,9 @@ namespace FTL |
85 | 191 | class CFConvUtil |
86 | 192 | { |
87 | 193 | public: |
| 194 | + //return True or False |
| 195 | + FTLINLINE static LPCTSTR GetVariantBoolString(VARIANT_BOOL varBool); |
| 196 | + |
88 | 197 | //ex. pb = {255,15} -> sz = {"FF,0F"} -- TODO: AtlHexDecode/AtlHexEncode |
89 | 198 | FTLINLINE static BOOL HexFromBinary(__in const BYTE* pBufSrc, __in LONG nSrcLen, |
90 | 199 | __out LPTSTR pBufDest, __inout LONG* pDestCharCount, |
|
0 commit comments