Val.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- // Val.cpp: implementation of the CVal class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testBT.h"
- #include "Val.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CVal::CVal(long lSrc)
- {
- vt = VT_LONG;
- lVal = lSrc;
- }
- CVal::CVal(string strSrc)
- {
- setBuf((char*)strSrc.data(), strSrc.size());
- }
- CVal::CVal(char* pstrSrc, long lstrLenSrc)
- {
- setBuf(pstrSrc, lstrLenSrc);
- }
- void CVal::setBuf(char* pstrSrc, long lstrLenSrc)
- {
- assert(lstrLenSrc >= 0);
- vt = VT_PSTR;
- pstrVal = new char[lstrLenSrc + 1];
- pstrVal [lstrLenSrc] = 0;
- memcpy(pstrVal, pstrSrc, lstrLenSrc);
- // pstrVal = pstrSrc;
- lstrLen = lstrLenSrc;
- }
- CVal::CVal(PVALMAP pmapSrc)
- {
- vt = VT_PMAP;
- pmapVal = pmapSrc;
- }
- CVal::CVal(PVALLIST plistSrc)
- {
- vt = VT_PLIST;
- plistVal= plistSrc;
- }
- CVal::~CVal()
- {
- switch(vt)
- {
- case VT_LONG:
- break;
- case VT_PSTR:
- {
- // ShowMessage(pstrVal);
- delete [] pstrVal;
- }
- break;
- case VT_PMAP:
- {
- VALMAP::iterator iter = pmapVal->begin();
- while(iter != pmapVal->end())
- {
- // ShowMessage(*(*iter).second);
- delete (*iter).second;
- iter++;
- }
- delete pmapVal;
- }
- break;
- case VT_PLIST:
- {
- VALLIST::iterator iter = plistVal->begin();
- while(iter != plistVal->end())
- {
- // ShowMessage(*(*iter));
- delete *iter;
- iter++;
- }
- delete plistVal;
- }
- break;
- }
- }
- CVal& CVal::operator = (CVal& v)
- {
- vt = v.vt;
- lVal = v.lVal;
- pstrVal = v.pstrVal;
- pmapVal = v.pmapVal;
- plistVal = v.plistVal;
- return *this;
- }
- /*
- CVal::operator string() const
- {
- char szMsg[100] = {0};
- switch(vt)
- {
- case VT_LONG:
- sprintf(szMsg, "long:");
- break;
- case VT_PSTR:
- {
- sprintf(szMsg, "string:");
- }
- break;
- case VT_PMAP:
- {
- sprintf(szMsg, "map:");
- }
- break;
- case VT_PLIST:
- {
- sprintf(szMsg, "list:");
- }
- break;
- }
- return string(szMsg);
- }
- //*/
- /*
- CVal::operator long()const
- {
- if (vt != VT_LONG)
- throw string("operator[] error: not a long type");
- return lVal;
- }
- //*/
- CVal::operator char*()const
- {
- if (vt != VT_PSTR)
- throw string("operator[] error: not a VT_PSTR type");
- assert(pstrVal [lstrLen] == 0);
- return pstrVal;
- }
- long CVal::size()
- {
- if (vt == VT_PMAP)
- return pmapVal->size();
- if (vt == VT_PLIST)
- return plistVal->size();
- throw string("size() error: not a map or list object");
- }
- CVal* CVal::operator [](string strKey)
- {
- if (vt != VT_PMAP)
- throw string("operator[] error: not a map object");
- VALMAP::iterator iter = pmapVal->find(strKey);
- if (iter == pmapVal->end())
- throw string("operator[](string strKey) error : not find key (") + strKey + ")";
- return (*iter).second;
- }
- CVal* CVal::operator [](int inx)
- {
- if (vt != VT_PLIST)
- throw string("operator[] error: not a list object");
- if (inx >= plistVal->size() || inx < 0)
- throw string("operator[](int inx) error : inx out of range list ");
- return (*plistVal)[inx];
- }
- void CVal::test()
- {
- VALMAP* pmv = new VALMAP;
- (*pmv)["sdfd"] = new CVal(10);
- VALLIST* plv = new VALLIST();
- plv->push_back(new CVal(100));
- (*pmv)["zzz"] = new CVal(plv);
- CVal* pv = new CVal(pmv);
- VALMAP m = VALMAP();
- m["l"] = pv;
- delete pv;
- }