最近看了Android源码之后,里面有一个用于参数传递的类Bundle,于是自己写了一个简单的KBundle用于在C++下传递多个参数。
以下是源代码:
#include <map>
#include <atlstr.h>
#include <Windows.h>
class KBundle
{
public:
enum _ENUM_DATA_TYPE
{
enumDataType_none,
enumDataType_int,
enumDataType_long,
enumDataType_byte,
enumDataType_double,
enumDataType_char,
enumDataType_wstring,
enumDataType_int64,
};
typedef struct _BUNDLE_DATA
{
_ENUM_DATA_TYPE enType;
union
{
byte bItem;
char cItem;
int nItem;
long lItem; ///>__time32_t
float fItem;
double dItem;
wchar_t* pWItem;
__int64 _n64Item; ///>__time64_t
};
}BUNDLE_DATA;
typedef std::map<CStringW, BUNDLE_DATA> DATA_MAP;
public:
KBundle()
{
}
~KBundle()
{
}
public:
KBundle(const KBundle& _kb)
{
this->m_mapData = _kb.m_mapData;
_CopyWString(_kb);
}
KBundle& operator=(const KBundle& _kb)
{
if (this == &_kb)
{
return *this;
}
this->m_mapData = _kb.m_mapData;
_CopyWString(_kb);
return *this;
}
public:
void PutItem(LPCWSTR lpstrKey, int nValue)
{
BUNDLE_DATA bd = {enumDataType_int};
bd.nItem = nValue;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, byte bValue)
{
BUNDLE_DATA bd = {enumDataType_byte};
bd.bItem = bValue;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, char cValue)
{
BUNDLE_DATA bd = {enumDataType_char};
bd.cItem = cValue;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, double dValue)
{
BUNDLE_DATA bd = {enumDataType_double};
bd.dItem = dValue;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, long lValue)
{
BUNDLE_DATA bd = {enumDataType_long};
bd.lItem = lValue;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, __int64 _n64Value)
{
BUNDLE_DATA bd = {enumDataType_int64};
bd._n64Item = _n64Value;
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
void PutItem(LPCWSTR lpstrKey, LPCWSTR lpstrValue)
{
BUNDLE_DATA bd = {enumDataType_wstring};
size_t nLength = wcslen(lpstrValue) + 1;
bd.pWItem = new wchar_t[nLength];
if (bd.pWItem != NULL)
{
StrCpyW(bd.pWItem, lpstrValue);
_Remove(bd.enType, lpstrKey);
m_mapData.insert(std::make_pair(lpstrKey, bd));
}
}
int GetInt(LPCWSTR lpstrKey, int nDefault = 0)
{
BUNDLE_DATA bd = {enumDataType_int};
bd.nItem = nDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.nItem;
}
long GetLong(LPCWSTR lpstrKey, long lDefault = 0l)
{
BUNDLE_DATA bd = {enumDataType_long};
bd.lItem = lDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.lItem;
}
byte GetByte(LPCWSTR lpstrKey, byte bDefault = 0)
{
BUNDLE_DATA bd = {enumDataType_byte};
bd.bItem = bDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.bItem;
}
double GetDouble(LPCWSTR lpstrKey, double dDefault = 0.0)
{
BUNDLE_DATA bd = {enumDataType_double};
bd.dItem = dDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.dItem;
}
char GetChar(LPCWSTR lpstrKey, char cDefault = '\0')
{
BUNDLE_DATA bd = {enumDataType_char};
bd.cItem = cDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.cItem;
}
LPCWSTR GetStringW(LPCWSTR lpstrKey, LPWSTR lpstrDefault = NULL)
{
BUNDLE_DATA bd = {enumDataType_wstring};
bd.pWItem = lpstrDefault;
_GetValue(bd.enType, lpstrKey, bd);
return bd.pWItem;
}
__int64 GetInt64(LPCWSTR lpstrKey, __int64 n64Default = 0i64)
{
BUNDLE_DATA bd = {enumDataType_int64};
bd._n64Item = n64Default;
_GetValue(bd.enType, lpstrKey, bd);
return bd._n64Item;
}
size_t GetSize() const
{
return m_mapData.size();
}
BOOL IsEmpty() const
{
return m_mapData.empty();
}
BOOL IsContainsKey(LPCWSTR lpstrKey) const
{
return m_mapData.find(lpstrKey) != m_mapData.end();
}
void RemoveKey(LPCWSTR lpstrKey)
{
DATA_MAP::iterator iterDele = m_mapData.find(lpstrKey);
if (iterDele != m_mapData.end())
{
if (iterDele->second.enType == enumDataType_wstring &&
iterDele->second.pWItem != NULL)
{
delete[] iterDele->second.pWItem;
iterDele->second.pWItem = NULL;
}
m_mapData.erase(iterDele);
}
}
void Clear()
{
DATA_MAP::iterator iterDele = m_mapData.begin();
for (; iterDele != m_mapData.end(); ++iterDele)
{
if (iterDele->second.enType == enumDataType_wstring &&
iterDele->second.pWItem != NULL)
{
delete[] iterDele->second.pWItem;
iterDele->second.pWItem = NULL;
}
}
m_mapData.clear();
}
private:
void _Remove(int nType, LPCWSTR lpstrKey)
{
DATA_MAP::iterator iterDele = m_mapData.find(lpstrKey);
if (iterDele == m_mapData.end())
{
goto Exit0;
}
switch (nType)
{
case enumDataType_int:
case enumDataType_long:
case enumDataType_byte:
case enumDataType_double:
case enumDataType_char:
case enumDataType_int64:
break;
case enumDataType_wstring:
if (iterDele->second.pWItem != NULL &&
iterDele->second.enType == enumDataType_wstring)
{
delete[] iterDele->second.pWItem;
iterDele->second.pWItem = NULL;
}
break;
default:
break;
}
m_mapData.erase(iterDele);
Exit0:
return;
}
BOOL _GetValue(int nType, LPCWSTR lpstrKey, BUNDLE_DATA& bd)
{
BOOL bRet = FALSE;
DATA_MAP::const_iterator iterFind = m_mapData.find(lpstrKey);
if (iterFind == m_mapData.end())
{
goto Exit0;
}
switch (nType)
{
case enumDataType_int:
bd.nItem = iterFind->second.nItem;
break;
case enumDataType_long:
bd.lItem = iterFind->second.lItem;
break;
case enumDataType_byte:
bd.bItem = iterFind->second.bItem;
break;
case enumDataType_double:
bd.dItem = iterFind->second.dItem;
break;
case enumDataType_char:
bd.cItem = iterFind->second.cItem;
break;
case enumDataType_int64:
bd._n64Item = iterFind->second._n64Item;
break;
case enumDataType_wstring:
bd.pWItem = iterFind->second.pWItem;
break;
default:
break;
}
bRet = TRUE;
Exit0:
return bRet;
}
///>深拷贝
void _CopyWString(const KBundle& _kb)
{
DATA_MAP::iterator iterDele = m_mapData.begin();
for (; iterDele != m_mapData.end(); ++iterDele)
{
if (iterDele->second.enType != enumDataType_wstring)
{
continue;
}
if (iterDele->second.pWItem == NULL)
{
continue;
}
LPCWSTR lpstrTemp = iterDele->second.pWItem;
iterDele->second.pWItem = NULL;
iterDele->second.pWItem = new wchar_t[wcslen(lpstrTemp) + 1];
if (iterDele->second.pWItem != NULL)
{
StrCpyW(iterDele->second.pWItem, lpstrTemp);
}
}
}
public:
DATA_MAP m_mapData; ///>Item_Map
};