📄 cvariant.cpp
字号:
i_temp=cTemp; return i_temp.c_str(); case ULong: sprintf (cTemp, "%lu", d->value.ul); i_temp=cTemp; return i_temp.c_str(); case Long: sprintf (cTemp, "%ld", d->value.l); i_temp=cTemp; return i_temp.c_str(); case Float: sprintf (cTemp, "%f", d->value.f); i_temp=cTemp; return i_temp.c_str(); case Double: sprintf (cTemp, "%f", d->value.d); i_temp=cTemp; return i_temp.c_str(); case Pchar: return (const char *) (d->value.ptr); default: return NULL; }}CDate_TCVariant::toMyDate (bool * ok) const{ if (ok) *ok = canCast (mydate);//是否能够转化 switch (d->typ)//不同类型不同处理 { case UShort: return CDate_T(d->value.us); case Short: return CDate_T(d->value.s); case UInt: return CDate_T(d->value.ui); case Int: return CDate_T(d->value.i); case ULong: return CDate_T(d->value.ul); case Long: return CDate_T(d->value.l); case Float: return CDate_T((int)d->value.f); case Double: return CDate_T((int)d->value.d); case mydate: return CDate_T(*(d->value.date)); case Pchar: return CDate_T((char*)d->value.ptr); default: return CDate_T(); }}boolCVariant::cast (Type t){ switch (t) { case CVariant::Bool: asBool (); break; case CVariant::UShort: asUShort (); break; case CVariant::Short: asShort (); break; case CVariant::UInt: asUInt (); break; case CVariant::Int: asInt (); break; case CVariant::ULong: asULong (); break; case CVariant::Long: asLong (); break; case CVariant::Float: asFloat (); break; case CVariant::Double: asDouble (); break; case CVariant::Pchar: asPchar (); break; case CVariant::mydate: asMyDate (); break; case CVariant::Invalid: default: (*this) = CVariant (); } return canCast (t);}boolCVariant::canCast (Type t) const{ if (Type (d->typ) == t) return true; switch (t) { case Bool: return (d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double ); case UShort: return (d->typ == Bool || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double ||d->typ == Pchar && str_can_long ((const char *) (d->value. ptr)) ); case Short: return (d->typ == Bool || d->typ == UShort || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double || d->typ == Pchar && str_can_long ((const char *) (d->value.ptr)) ); case UInt: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double || d->typ == Pchar && str_can_long ((const char *) (d->value. ptr)) ); case Int: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double || d->typ == Pchar && str_can_long ((const char *) (d->value.ptr)) ); case ULong: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == Long || d->typ == Float || d->typ == Double || d->typ == Pchar && str_can_long ((const char *) (d->value. ptr)) ); case Long: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Float || d->typ == Double || d->typ == Pchar && str_can_long ((const char *) (d->value.ptr)) ); case Float: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Pchar && str_can_double ((const char *) (d->value.ptr)) ); case Double: return (d->typ == Bool || d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Double || d->typ == Pchar && str_can_double ((const char *) (d->value.ptr)) ); case Pchar: return (d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double ); case mydate: return (d->typ == UShort || d->typ == Short || d->typ == UInt || d->typ == Int || d->typ == ULong || d->typ == Long || d->typ == Float || d->typ == Double || d->typ == Pchar ); case Invalid: default: return false; }}bool CVariant::asBool (){ detach (); if (d->typ != Bool) { bool b = toBool (); d->clear (); d->value.b = b; d->typ = Bool; } return d->value.b;}unsigned short CVariant::asUShort (){ detach (); if (d->typ != UShort) { unsigned short us = toUShort (); d->clear (); d->value.us = us; d->typ = UShort; } return d->value.us;}shortCVariant::asShort (){ detach (); if (d->typ != Short) { short s = toShort (); d->clear (); d->value.s = s; d->typ = Short; } return d->value.s;}unsigned int CVariant::asUInt (){ detach (); if (d->typ != UInt) { unsigned int ui = toUInt (); d->clear (); d->value.ui = ui; d->typ = UInt; } return d->value.ui;}intCVariant::asInt (){ detach (); if (d->typ != Int) { int i = toInt (); d->clear (); d->value.i = i; d->typ = Int; } return d->value.i;}unsigned long CVariant::asULong (){ detach (); if (d->typ != ULong) { unsigned long ul = toULong (); d->clear (); d->value.ul = ul; d->typ = ULong; } return d->value.ul;}longCVariant::asLong (){ detach (); if (d->typ != Long) { int l = toLong (); d->clear (); d->value.l = l; d->typ = Long; } return d->value.l;}floatCVariant::asFloat (){ detach (); //? ? ? ? if (d->typ != Float) { float f = toFloat (); d->clear (); d->value.f = f; d->typ = Float; } return d->value.f;}doubleCVariant::asDouble (){ detach (); //? ? ? ? if (d->typ != Double) { double dbl = toDouble (); d->clear (); d->value.d = dbl; d->typ = Double; } return d->value.d;}const char *CVariant::asPchar (){ detach (); if (d->typ != Pchar) { const char *pszTemp = toPchar (); d->clear (); d->value.ptr = new char [strlen (pszTemp)+1]; strcpy ((char *) (d->value.ptr), pszTemp); d->typ = Pchar; } return (const char *) (d->value.ptr);}CDate_TCVariant::asMyDate (){ detach (); //? ? ? if (d->typ != mydate ) { CDate_T t = toMyDate (); d->clear (); d->value.date = new CDate_T(t); d->typ = mydate; } return (CDate_T) (*(d->value.date));}//脱离当前值,产生自己的值拷贝voidCVariant::detach (){ # if defined(_REENTRANT) ZcMutexLocker locker (zc_thread_mutexpool.get (d)); # endif if (d->count == 1) return; d->deref (); d = new Private (d);}CVariant & CVariant::operator = (const CVariant & variant) { # if defined(_REENTRANT) ZcMutexLocker locker (zc_thread_mutexpool.get (d)); # endif CVariant & other = (CVariant &) variant; other.d->ref (); if (d->deref ()) delete d; d=NULL; d = other.d; return *this;}boolCVariant::operator == (const CVariant & v) const { if (!v.canCast (type ())) return false; switch (d->typ) { case Bool: return (v.toBool () == toBool ()); case UShort: return v.toUShort () == toUShort (); case Short: return v.toShort () == toShort (); case UInt: return v.toUInt () == toUInt (); case Int: return v.toInt () == toInt (); case ULong: return v.toULong () == toULong (); case Long: return v.toLong () == toLong (); case Float: return v.toFloat () == toFloat (); case Double: return v.toDouble () == toDouble (); case Pchar: return strcmp (v.toPchar (),toPchar ()); case mydate: return ( v.toMyDate() == toMyDate() ); }}boolCVariant::operator != (const CVariant & v) const { return !(v == *this);}//返回当前类型inline CVariant::TypeCVariant::type () const{ return d->typ;}//返回当前类型名const char *CVariant::typeName () const{ return typeToName ((Type) d->typ);}//返回名字对应的类型CVariant::Type CVariant::nameToType (const char *name){ for (int i = 0; i < ntypes; i++) { if (strcmp (type_map[i], name) == 0) return (Type) i; } return Invalid;}//将类型转化为类型名const char *CVariant::typeToName (Type typ){ if (typ >= ntypes) { return NULL; } return type_map[typ];}//判断是否是无效类型inline boolCVariant::isValid () const{ return (d->typ != Invalid);}//判断是否是空boolCVariant::isNull () const{ switch (d->typ) { case CVariant::Pchar: if (d->value.ptr == NULL) { return true; } else if (strcmp ((char *) (d->value.ptr), "") == 0) { return true; } else { return false; } break; case CVariant::mydate: if (d->value.date == NULL) { return true; } else { return false ; } break; case CVariant::Invalid: case CVariant::Bool: case CVariant::UShort: case CVariant::Short: case CVariant::UInt: case CVariant::Int: case CVariant::ULong: case CVariant::Long: case CVariant::Float: case CVariant::Double: return false; break; }}//判断参数中的字符串能否转化为long boolCVariant::str_can_long (const char *pszStr) const{ char *end; long val = strtol ((pszStr != NULL) ? pszStr : "", &end, 10); bool ok = (pszStr && *pszStr && (end == 0 || (end - pszStr) == (int) strlen (pszStr))); return ok;}//判断参数中的字符串能否转化为doubleboolCVariant::str_can_double (const char *pszStr) const{ char *end; double val = strtod (pszStr ? pszStr : "", &end); bool ok = (pszStr && *pszStr && (end == 0 || (end - pszStr) == (int) strlen (pszStr))); return ok;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -