文章目录
如果采用一篇文章将基础类介绍下来,会显得比较长,阅读不便,所以分成两部分,接下来讲解CDuiString、CStdStringPtrMap和CDuiVariant,这三个基本对象。
1.CDuiString
- 1.CDuiString
- 2.CStdStringPtrMap
- 3.CDuiVariant
- 4.作者答疑
它是Duilib库中对字符串对象的一个封装,里面包含了对字符串的常用操作,见注释。如下所示:
class UILIB_API CDuiString
{
public:
enum { MAX_LOCAL_STRING_LEN = 63 };
//---------初始化的方式------------
CDuiString();
CDuiString(const TCHAR ch);
CDuiString(const CDuiString& src);
CDuiString(LPCTSTR lpsz, int nLen = -1);
~CDuiString();
void Empty();//清空
int GetLength() const;
bool IsEmpty() const;
TCHAR GetAt(int nIndex) const;
void Append(LPCTSTR pstr);
void Assign(LPCTSTR pstr, int nLength = -1);
LPCTSTR GetData() const;
std::wstring GetStringW();
std::string GetStringA();
void SetAt(int nIndex, TCHAR ch);
operator LPCTSTR() const;
//字符串的赋值和链接
TCHAR operator[] (int nIndex) const;
const CDuiString& operator=(const CDuiString& src);
const CDuiString& operator=(const TCHAR ch);
const CDuiString& operator=(LPCTSTR pstr);
#ifdef _UNICODE
const CDuiString& CDuiString::operator=(LPCSTR lpStr);
const CDuiString& CDuiString::operator+=(LPCSTR lpStr);
#else
const CDuiString& CDuiString::operator=(LPCWSTR lpwStr);
const CDuiString& CDuiString::operator+=(LPCWSTR lpwStr);
#endif
CDuiString operator+(const CDuiString& src) const;
CDuiString operator+(LPCTSTR pstr) const;
const CDuiString& operator+=(const CDuiString& src);
const CDuiString& operator+=(LPCTSTR pstr);
const CDuiString& operator+=(const TCHAR ch);
//字符串的比较函数
bool operator == (LPCTSTR str) const;
bool operator != (LPCTSTR str) const;
bool operator (LPCTSTR str) const;
int Compare(LPCTSTR pstr) const;//
int CompareNoCase(LPCTSTR pstr) const;
void MakeUpper();//字符串转大写
void MakeLower();//字符串转小写
//字符串截断
CDuiString Left(int nLength) const;
CDuiString Mid(int iPos, int nLength = -1) const;
CDuiString Right(int nLength) const;
//字符串查找替换
int Find(TCHAR ch, int iPos = 0) const;//
int Find(LPCTSTR pstr, int iPos = 0) const;//
int ReverseFind(TCHAR ch) const;//
int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);//
//格式化字符串
int __cdecl Format(LPCTSTR pstrFormat, ...);//
int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);//
protected:
int __cdecl InnerFormat(LPCTSTR pstrFormat, va_list Args);//
protected:
LPTSTR m_pstr;
TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
};
2.CStdStringPtrMap
它是以CDuiString为key的一种map对象,内部数组元素是TITEM对象,而相关对象是对象指针。类里包含有map数据机构的基本操作。如下所示:
class UILIB_API CStdStringPtrMap
{
public:
CStdStringPtrMap(int nSize = 83);
~CStdStringPtrMap();
void Resize(int nSize = 83);
LPVOID Find(LPCTSTR key, bool optimize = true) const;
bool Insert(LPCTSTR key, LPVOID pData);
LPVOID Set(LPCTSTR key, LPVOID pData);
bool Remove(LPCTSTR key);
void RemoveAll();
int GetSize() const;
LPCTSTR GetAt(int iIndex) const;
LPCTSTR operator[] (int nIndex) const;
protected:
TITEM** m_aT;
int m_nBuckets;
int m_nCount;
};
3.CDuiVariant
它是继承至VARIANT的一种变体数据类型,同一种结构保存不同类型的数据,提高内存的利用效率。
class CDuiVariant : public VARIANT
{
public:
CDuiVariant()
{
VariantInit(this);
}
CDuiVariant(int i)
{
VariantInit(this);
this->vt = VT_I4;
this->intVal = i;
}
CDuiVariant(float f)
{
VariantInit(this);
this->vt = VT_R4;
this->fltVal = f;
}
CDuiVariant(LPOLESTR s)
{
VariantInit(this);
this->vt = VT_BSTR;
this->bstrVal = s;
}
CDuiVariant(IDispatch *disp)
{
VariantInit(this);
this->vt = VT_DISPATCH;
this->pdispVal = disp;
}
~CDuiVariant()
{
VariantClear(this);
}
};
4.作者答疑
如有疑问,请留言。