VariantEx.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:7k
- #include ".VariantEx.h"
- //constructor procedure
- CVariantEx::CVariantEx()
- {
- this->m_var.Clear();
- }
- //constructor procedure
- CVariantEx::CVariantEx(_variant_t &vt)
- {
- try{
- if( this->Clear() ){
- this->m_var = vt;
- }
- }
- catch(_com_error e){
- //throw e.ErrorMessage();
- }
- }
- //constructor procedure
- CVariantEx::CVariantEx(CVariantEx &var)
- {
- this->m_var = var.get_variant_t();
- }
- CVariantEx::CVariantEx(const bool bVar)
- {
- _variant_t v(bVar);
- this->m_var = v;
- }
- CVariantEx::CVariantEx(const long nVar)
- {
- _variant_t v(nVar);
- this->m_var = v;
- }
- CVariantEx::CVariantEx(const double dVar)
- {
- _variant_t v(dVar);
- this->m_var = v;
- }
- CVariantEx::CVariantEx(LPCSTR lpszVar)
- {
- _variant_t v(lpszVar);
- this->m_var = v;
- }
- //operator "=" override
- CVariantEx& CVariantEx::operator=(_variant_t &vt)
- {
- this->m_var = vt;
- return (*this);
- }
- //operator "=" override
- CVariantEx& CVariantEx::operator=(CVariantEx &var)
- {
- this->m_var = var.get_variant_t();
- return (*this);
- }
- CVariantEx& CVariantEx::operator=(const bool bVar)
- {
- _variant_t v(bVar);
- this->m_var = v;
- return (*this);
- }
- CVariantEx& CVariantEx::operator=(const long nVar)
- {
- _variant_t v(nVar);
- this->m_var = v;
-
- return (*this);
- }
- CVariantEx& CVariantEx::operator=(const double dVar)
- {
- _variant_t v(dVar);
- this->m_var = v;
-
- return (*this);
- }
- CVariantEx& CVariantEx::operator=(LPCSTR lpszVar)
- {
- _variant_t v(lpszVar);
- this->m_var = v;
-
- return (*this);
- }
- //destructor procedure
- CVariantEx::~CVariantEx()
- {
- this->Clear();
- }
- //determine the var whether is null
- bool CVariantEx::IsNull()
- {
- //if you write "this->m_var == NULL"
- //then the compiler can warn syntax error
- return (this->m_var.vt == VT_NULL);
- }
- //clear the var value
- bool CVariantEx::Clear()
- {
- if( this->IsNull() ){
- return true;
- }
- else{
- this->m_var.Clear();
- return true;
- }
- }
- //get the variant type
- VARTYPE_ENUM CVariantEx::getVarType()
- {
- if(this->IsNull()){
- throw "variant is null!!!";
- }
- switch(this->m_var.vt) {
- case VT_BSTR:
- case VT_LPSTR:
- case VT_LPWSTR:
- return VTE_STRING;
- case VT_I2:
- return VTE_SHORT;
- case VT_I8:
- return VTE_LONG;
-
- case VT_VOID:
- case VT_INT:
- case VT_I4:
- return VTE_INT;
- case VT_UI2:
- return VTE_UNSHORT;
- case VT_UINT:
- case VT_UI4:
- return VTE_UNINT;
- case VT_UI8:
- return VTE_ULINT;
- case VT_R4:
- return VTE_FLOAT;
- case VT_DECIMAL:
- return VTE_DECIMAL;
- case VT_R8:
- return VTE_DOUBLE;
- case VT_BOOL:
- return VTE_BOOL;
- case VT_DATE:
- return VTE_DATETIME;
- case VT_NULL:
- return VTE_NULL;
- case VT_EMPTY:
- return VTE_EMPTY;
- case VT_UNKNOWN:
- default:
- return VTE_UNKNOWN;
- }
- }
- //get the variant value
- _variant_t& CVariantEx::get_variant_t()
- {
- return this->m_var;
- }
- //######################################################
- //convert to string type
- string CVariantEx::AsString()
- {
- string strRet = "NULL";
- if(this->IsNull()){
- return strRet;
- //throw "variant is null!!!";
- }
- char szInt[64],
- szDT[MAX_DATE_SIZE + MAX_TIME_SIZE];
- strRet.erase();
- memset(szInt, 0x0, sizeof(szInt));
- switch(this->getVarType()) {
- case VTE_STRING:
- strRet = (LPCTSTR)(_bstr_t)this->m_var;
- break;
- case VTE_INT:
- itoa(this->m_var.intVal, szInt, 10);
- break;
-
- case VTE_SHORT:
- itoa(this->m_var.iVal, szInt, 10);
- break;
-
- case VTE_LONG:
- ltoa(this->m_var.lVal, szInt, 10);
- break;
-
- case VTE_UNSHORT:
- itoa(this->m_var.uiVal, szInt, 10);
- break;
-
- case VTE_UNINT:
- ultoa(this->m_var.uintVal, szInt, 10);
- break;
-
- case VTE_ULINT:
- ultoa(this->m_var.ulVal, szInt, 10);
- break;
- //real type(%.4f format)
- case VTE_FLOAT:
- case VTE_DECIMAL:
- sprintf((char*)&szInt[0], "%.4f", this->m_var.fltVal);
- break;
-
- //real type(%.8f format)
- case VTE_DOUBLE:
- sprintf((char*)&szInt[0], "%.8f", this->m_var.dblVal);
- break;
-
- case VTE_BOOL:
- if(this->AsBool())
- strcpy((char*)&szInt[0], "True");
- else
- strcpy((char*)&szInt[0], "False");
- break;
- case VTE_DATETIME:
- {
- try{
- CDateTimeEx dtDateTime = this->AsDateTime();
- dtDateTime.AsDTString(szDT);
- strRet = strRet + szDT;
- }
- catch(std::exception e){
- throw e.what();
- }
- }
- break;
- case VTE_NULL:
- strRet = "NULL";
- break;
- case VTE_UNKNOWN:
- strRet = "UNKNOWN";
- break;
- case VTE_EMPTY:
- break;
- case VTE_BLOB:
- case VTE_BLOB_OBJECT:
- strRet = "BLOB";
- break;
- case VTE_TEXT:
- strRet = "TEXT";
- break;
-
- default:
- throw "couldn't convert this variant to string value!!!";
- }
- strRet = strRet + szInt;
- return strRet;
- }
- //convert to boolean type
- bool CVariantEx::AsBool()
- {
- if(this->IsNull()){
- throw "variant is null!!!";
- }
- switch(this->getVarType()) {
- case VTE_BOOL:
- return (0 != this->m_var.lVal) ? true: false;
- case VTE_UNSHORT:
- return (this->m_var.uiVal != 0) ? true:false;
- case VTE_UNINT:
- return (this->m_var.uintVal != 0) ? true:false;
- case VTE_ULINT:
- return (this->m_var.ulVal != 0) ? true:false;
- default:
- throw "couldn't convert this variant to boolean value!!!";
- }
- }
- //convert to integer type
- int CVariantEx::AsInteger()
- {
- if(this->IsNull()){
- throw "variant is null!!!";
- }
- /////////////////////////////////
- switch(this->getVarType()) {
-
- case VTE_INT:
- return (this->m_var.intVal);
-
- case VTE_SHORT:
- return (this->m_var.iVal);
- case VTE_LONG:
- return (this->m_var.lVal);
- case VTE_UNSHORT:
- return (this->m_var.uiVal);
-
- case VTE_UNINT:
- return (this->m_var.uintVal);
-
- case VTE_ULINT:
- return (this->m_var.ulVal);
- default:
- throw "couldn't convert this variant to int value!!!";
- }
- }
- //convert to real type
- double CVariantEx::AsReal()
- {
- if(this->IsNull()){
- throw "variant is null!!!";
- }
- /////////////////////////////////
- switch(this->getVarType()) {
- case VTE_FLOAT:
- return (this->m_var.fltVal);
- case VTE_DECIMAL:
- {
- char szDecimal[32];
- memset(szDecimal, 0x0, sizeof(szDecimal));
- sprintf(szDecimal, "%.4f", this->m_var.decVal);
-
- return (atof(szDecimal));
- }
-
- case VTE_DOUBLE:
- return (this->m_var.dblVal);
- default:
- throw "couldn't convert this variant to double value!!!";
- }
- }
- //convert to datetime type
- CDateTimeEx CVariantEx::AsDateTime()
- {
- if(this->IsNull()){
- throw "variant is null!!!";
- }
- /////////////////////////////////
- DATE dtTag = this->m_var.date;
- try{
- CDateTimeEx dtDateTime(dtTag);
- return dtDateTime;
- }
- catch(std::exception e){
- throw e.what();
- }
- }