/******************************************************************************************************************************************
创建日期: 2005.3.31
原文件名: soVariant.h
作者: 大康(sosolife@163.com)
文件版本: 1.0
说明: 自动数据类型
类试COM中的VARIANT数据类型(受其启发而作)
******************************************************************************************************************************************/
#pragma once
#include <string>
using namespace std;
enum soVariantType { boolType, longType, doubleType, strType, wstrType };
class soVariant
{
public:
//构造(不允许隐式转换)
explicit soVariant( bool b )
{
m_type = boolType;
m_bValue = b;
}
explicit soVariant( long n )
{
m_type = longType;
m_nValue = n;
}
explicit soVariant( double d )
{
m_type = doubleType;
m_dValue = d;
}
explicit soVariant( const char* sz )
{
m_type = strType;
m_szValue = new char[strlen(sz)+1];
strcpy(m_szValue,sz);
}
explicit soVariant( const wchar_t* wsz )
{
m_type = wstrType;
m_wszValue = new wchar_t[wcslen(wsz)+1];
wcscpy(m_wszValue,wsz);
}
//复制
soVariant( const soVariant& copy )
{
switch ( copy.m_type )
{
case boolType:
m_bValue = copy.m_bValue;
break;
case longType:
m_nValue = copy.m_nValue;
break;
case doubleType:
m_dValue = copy.m_dValue;
break;
case strType:
m_szValue = new char[strlen(copy.m_szValue)+1];
strcpy(m_szValue,copy.m_szValue);
break;
case wstrType:
m_wszValue = new wchar_t[wcslen(copy.m_wszValue)+1];
wcscpy(m_wszValue,copy.m_wszValue);
break;
}
m_type = copy.m_type;
}
//解构
virtual ~soVariant() { clear(); }
//操作符
soVariant& operator = ( bool b )
{
clear();
m_type = boolType;
m_bValue = b;
return *this;
}
soVariant& operator = ( long n )
{
clear();
m_type = longType;
m_nValue = n;
return *this;
}
soVariant& operator = ( double d )
{
clear();
m_type = doubleType;
m_dValue = d;
return *this;
}
soVariant& operator = ( const char* sz )
{
clear();
m_type = strType;
m_szValue = new char[strlen(sz)+1];
strcpy(m_szValue,sz);
return *this;
}
soVariant& operator = ( const wchar_t* wsz )
{
clear();
m_type = wstrType;
m_wszValue = new wchar_t[wcslen(wsz)+1];
wcscpy(m_wszValue,wsz);
return *this;
}
soVariant& operator = ( const soVariant& rhs )
{
clear();
switch ( rhs.m_type )
{
case boolType:
m_bValue = rhs.m_bValue;
break;
case longType:
m_nValue = rhs.m_nValue;
break;
case doubleType:
m_dValue = rhs.m_dValue;
break;
case strType:
m_szValue = new char[strlen(rhs.m_szValue)+1];
strcpy(m_szValue,rhs.m_szValue);
break;
case wstrType:
m_wszValue = new wchar_t[wcslen(rhs.m_wszValue)+1];
wcscpy(m_wszValue,rhs.m_wszValue);
break;
}
m_type = rhs.m_type;
return *this;
}
//显式转换方法
bool to_bool() const { return m_bValue; }
long to_long() const { return m_nValue; }
double to_double() const { return m_dValue; }
const char* to_str() const { return m_szValue; }
const wchar_t* to_wstr() const { return m_wszValue; }
soVariantType type() const { return m_type; }
private:
void clear()
{
switch ( m_type )
{
case boolType:
m_bValue = false;
break;
case longType:
m_nValue = 0;
break;
case doubleType:
m_dValue = 0;
break;
case strType:
delete[] m_szValue;
m_szValue = NULL;
break;
case wstrType:
delete[] m_wszValue;
m_wszValue = NULL;
break;
}
}
private:
union
{
bool m_bValue;
long m_nValue;
double m_dValue;
char *m_szValue;
wchar_t *m_wszValue;
};
soVariantType m_type;
};