00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Double.h>
00018 #include <spl/Int32.h>
00019 #include <spl/Int64.h>
00020 #include <spl/Variant.h>
00021 #include <spl/math/Math.h>
00022 #include <spl/interp/JsArray.h>
00023 #include <spl/interp/JsMethod.h>
00024 #include <spl/interp/JsObject.h>
00025
00026 IVariant::IVariant()
00027 {
00028 }
00029
00030 IVariant::~IVariant()
00031 {
00032 }
00033
00034 IVariantData::IVariantData()
00035 {
00036 }
00037
00038 IVariantData::~IVariantData()
00039 {
00040 }
00041
00042 Variant::Variant()
00043 : m_data(new _VariantData<Undefined>(Undefined()))
00044 {
00045 }
00046
00047 Variant::Variant(bool flag)
00048 : m_data(new _VariantData<bool>(flag))
00049 {
00050 }
00051
00052 Variant::Variant(int8 i8)
00053 : m_data(new _VariantData<int8>(i8))
00054 {
00055 }
00056
00057 Variant::Variant(int16 i16)
00058 : m_data(new _VariantData<int16>(i16))
00059 {
00060 }
00061
00062 Variant::Variant(int32 i32)
00063 : m_data(new _VariantData<int32>(i32))
00064 {
00065 }
00066
00067 Variant::Variant(int64 i64)
00068 : m_data(new _VariantData<int64>(i64))
00069 {
00070 }
00071
00072 Variant::Variant(float32 f32)
00073 : m_data(new _VariantData<float32>(f32))
00074 {
00075 }
00076
00077 Variant::Variant(float64 f64)
00078 : m_data(new _VariantData<float64>(f64))
00079 {
00080 }
00081
00082 Variant::Variant(const DateTime &dtm)
00083 : m_data(new _VariantData<DateTime>(dtm))
00084 {
00085 }
00086
00087 Variant::Variant(const Date& dt)
00088 : m_data(new _VariantData<Date>(dt))
00089 {
00090 }
00091
00092 Variant::Variant(const String& str)
00093 : m_data(new _VariantData<String>(str))
00094 {
00095 }
00096
00097 Variant::Variant(const String *str)
00098 : m_data(new _VariantData<String>(*str))
00099 {
00100 }
00101
00102 Variant::Variant(const char * str)
00103 : m_data(new _VariantData<String>(String(str)))
00104 {
00105 }
00106
00107 Variant::Variant(const Decimal& dec)
00108 : m_data(new _VariantData<Decimal>(dec))
00109 {
00110 }
00111
00112 Variant::Variant(const Variant& v)
00113 : m_data(v.m_data->Clone())
00114 {
00115 }
00116
00117 Variant::Variant(const Undefined& u)
00118 : m_data(new _VariantData<Undefined>(u))
00119 {
00120 }
00121
00122 Variant::Variant(RefCountPtr<IJsObject> jso)
00123 : m_data(new _VariantData<RefCountPtr<IJsObject> >(jso))
00124 {
00125 }
00126
00127 Variant::~Variant()
00128 {
00129 delete m_data;
00130 }
00131
00132 RefCountPtr<Variant> Variant::ParseInt() const
00133 {
00134 if (Convert::CanConvertToInt(m_data))
00135 {
00136 int64 a;
00137 Convert::ToInt64(m_data, a);
00138 return VariantPtr(new Variant(a));
00139 }
00140 StringPtr val = ToString();
00141 if (Int64::IsInt(*val))
00142 {
00143 return VariantPtr(new Variant(Int64::Parse(*val)));
00144 }
00145 return VariantPtr(new Variant());
00146 }
00147
00148 RefCountPtr<Variant> Variant::ParseFloat() const
00149 {
00150 if (Convert::CanConvertToFloat(m_data))
00151 {
00152 float64 a;
00153 Convert::ToFloat64(m_data, a);
00154 return VariantPtr(new Variant(a));
00155 }
00156 StringPtr val = ToString();
00157 if (Double::IsDouble(*val))
00158 {
00159 return VariantPtr(new Variant(Double::Parse(*val)));
00160 }
00161 return VariantPtr(new Variant());
00162 }
00163
00164 bool Variant::IsObject() const
00165 {
00166 return m_data->IsObject();
00167 }
00168
00169 String Variant::TypeName() const
00170 {
00171 return "Variant";
00172 }
00173
00174 Variant &Variant::operator =(const Variant &v)
00175 {
00176 ASSERT(m_data != v.m_data);
00177 ValidateMem();
00178 delete m_data;
00179 m_data = v.m_data->Clone();
00180 return *this;
00181 }
00182
00183 Variant& Variant::operator =(const int32 i)
00184 {
00185 ValidateMem();
00186 delete m_data;
00187 m_data = new _VariantData<int32>(i);
00188 return *this;
00189 }
00190
00191 Variant& Variant::operator =(const int64 i)
00192 {
00193 ValidateMem();
00194 delete m_data;
00195 m_data = new _VariantData<int64>(i);
00196 return *this;
00197 }
00198
00199 Variant& Variant::operator =(const float64 d)
00200 {
00201 ValidateMem();
00202 delete m_data;
00203 m_data = new _VariantData<float64>(d);
00204 return *this;
00205 }
00206
00207 Variant& Variant::operator =(const String& s)
00208 {
00209 ValidateMem();
00210 delete m_data;
00211 m_data = new _VariantData<String>(s);
00212 return *this;
00213 }
00214
00215 Variant& Variant::operator =(const char *cp)
00216 {
00217 ValidateMem();
00218 delete m_data;
00219 m_data = new _VariantData<String>(cp);
00220 return *this;
00221 }
00222
00223 Variant& Variant::operator =(const DateTime& s)
00224 {
00225 ValidateMem();
00226 delete m_data;
00227 m_data = new _VariantData<DateTime>(s);
00228 return *this;
00229 }
00230
00231 Variant& Variant::operator =(const Date& s)
00232 {
00233 ValidateMem();
00234 delete m_data;
00235 m_data = new _VariantData<Date>(s);
00236 return *this;
00237 }
00238
00239 Variant& Variant::operator =(const Decimal& s)
00240 {
00241 ValidateMem();
00242 delete m_data;
00243 m_data = new _VariantData<Decimal>(s);
00244 return *this;
00245 }
00246
00247 Variant& Variant::operator =(RefCountPtr<IJsObject> s)
00248 {
00249 ValidateMem();
00250 delete m_data;
00251 m_data = new _VariantData<RefCountPtr<IJsObject> >(s);
00252 return *this;
00253 }
00254
00255 bool Variant::ToBool() const
00256 {
00257 bool b;
00258
00259 if (m_data->ToBool(b))
00260 {
00261 return b;
00262 }
00263 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to bool.");
00264 }
00265
00266 byte Variant::ToByte() const
00267 {
00268 byte b;
00269 if (m_data->ToByte(b))
00270 {
00271 return b;
00272 }
00273 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to byte.");
00274 }
00275
00276 int16 Variant::ToInt16() const
00277 {
00278 int16 i;
00279 if(m_data->ToInt16(i))
00280 {
00281 return i;
00282 }
00283 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to int16.");
00284 }
00285
00286 int32 Variant::ToInt32() const
00287 {
00288 int32 i;
00289 if (m_data->ToInt32(i))
00290 {
00291 return i;
00292 }
00293 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to int32.");
00294 }
00295
00296 int64 Variant::ToInt64() const
00297 {
00298 int64 i;
00299 if (m_data->ToInt64(i))
00300 {
00301 return i;
00302 }
00303 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to int64.");
00304 }
00305
00306 float32 Variant::ToFloat32() const
00307 {
00308 float32 d;
00309 if (m_data->ToFloat32(d))
00310 {
00311 return d;
00312 }
00313 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to float32.");
00314 }
00315
00316 float64 Variant::ToFloat64() const
00317 {
00318 float64 d;
00319 if (m_data->ToFloat64(d))
00320 {
00321 return d;
00322 }
00323 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to float64.");
00324 }
00325
00326 DateTime Variant::ToDateTime() const
00327 {
00328 DateTime dtm;
00329 if(m_data->ToDateTime(dtm))
00330 {
00331 return dtm;
00332 }
00333 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to DateTime.");
00334 }
00335
00336 Date Variant::ToDate() const
00337 {
00338 Date dt;
00339 if(m_data->ToDate(dt))
00340 {
00341 return dt;
00342 }
00343 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to Date.");
00344 }
00345
00346 Decimal Variant::ToDecimal() const
00347 {
00348 Decimal d;
00349 if(m_data->ToDecimal(d))
00350 {
00351 return d;
00352 }
00353 throw InvalidTypeConversionException("Cannot convert " + m_data->TypeName() + " to Decimal.");
00354 }
00355
00356 StringPtr Variant::ToString() const
00357 {
00358 String s;
00359 m_data->ToString(s);
00360 return s.Clone();
00361 }
00362
00363 RefCountPtr<IJsObject> Variant::ToObject() const
00364 {
00365 ASSERT(IsObject());
00366
00367 RefCountPtr<IJsObject> obj = static_cast<_VariantData<RefCountPtr<IJsObject> > *>(m_data)->GetRef();
00368 obj.ValidateMem();
00369
00370 if (m_data->MajicNumber() == JSOBJECT_MAJIC)
00371 {
00372 ASSERT_MEM(obj.Get(), sizeof(JsObject));
00373 return obj;
00374 }
00375 if (m_data->MajicNumber() == JSARRAY_MAJIC)
00376 {
00377 ASSERT_MEM(obj.Get(), sizeof(JsArray));
00378 return obj;
00379 }
00380 if (m_data->MajicNumber() == JSMETHOD_MAJIC)
00381 {
00382 ASSERT_MEM(obj.Get(), sizeof(JsMethod));
00383 return obj;
00384 }
00385 throw new Exception("Internal error Variant::ToObject");
00386 }
00387
00388 bool Variant::ToChar(char& c2) const
00389 {
00390 return false;
00391 }
00392
00393 bool Variant::ToBool(bool& c2) const
00394 {
00395 c2 = ToBool();
00396 return true;
00397 }
00398
00399 bool Variant::ToByte(byte& c2) const
00400 {
00401 c2 = ToByte();
00402 return true;
00403 }
00404
00405 bool Variant::ToInt16(int16& c2) const
00406 {
00407 c2 = ToInt16();
00408 return true;
00409 }
00410
00411 bool Variant::ToInt32(int32& c2) const
00412 {
00413 c2 = ToInt32();
00414 return true;
00415 }
00416
00417 bool Variant::ToInt64(int64& c2) const
00418 {
00419 c2 = ToInt64();
00420 return true;
00421 }
00422
00423 bool Variant::ToUInt32(uint32& c2) const
00424 {
00425 return false;
00426 }
00427
00428 bool Variant::ToUInt64(uint64& c2) const
00429 {
00430 return false;
00431 }
00432
00433
00434 bool Variant::ToFloat32(float32& c2) const
00435 {
00436 c2 = ToFloat32();
00437 return true;
00438 }
00439
00440 bool Variant::ToFloat64(float64& c2) const
00441 {
00442 c2 = ToFloat64();
00443 return true;
00444 }
00445
00446 bool Variant::ToDateTime(DateTime& c2) const
00447 {
00448 c2 = ToDateTime();
00449 return true;
00450 }
00451
00452 bool Variant::ToDate(Date& c2) const
00453 {
00454 c2 = ToDate();
00455 return true;
00456 }
00457
00458 bool Variant::ToDecimal(Decimal& c2) const
00459 {
00460 c2 = ToDecimal(c2);
00461 return true;
00462 }
00463
00464 bool Variant::ToString(String& c2) const
00465 {
00466 c2 = *ToString();
00467 return true;
00468 }
00469
00470 bool Variant::Equals(const IComparable& a) const
00471 {
00472 if (a.MajicNumber() != MajicNumber())
00473 {
00474 return false;
00475 }
00476 const Variant& vp = static_cast<const Variant&>(a);
00477 vp.ValidateMem();
00478 return m_data->Equals(*vp.m_data);
00479 }
00480
00481 bool Variant::Equals( const Variant& v ) const
00482 {
00483 return m_data->Equals(*v.m_data);
00484 }
00485
00486 int Variant::Compare(const IComparable& a) const
00487 {
00488 if (a.MajicNumber() != MajicNumber())
00489 {
00490 return false;
00491 }
00492 return m_data->Compare(a);
00493 }
00494
00495 int32 Variant::MajicNumber() const
00496 {
00497 return 0x1001;
00498 }
00499
00500 int32 Variant::HashCode() const
00501 {
00502 return m_data->HashCode();
00503 }
00504
00505 bool Variant::BinOp(const Variant& v, enum CompareOperator op) const
00506 {
00507 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00508 {
00509 float64 a, b;
00510 if (!m_data->ToFloat64(a) || !v.m_data->ToFloat64(b))
00511 {
00512 return false;
00513 }
00514 if (op == Variant::VOP_EQ)
00515 {
00516 return a == b;
00517 }
00518 if (op == Variant::VOP_GT)
00519 {
00520 return a > b;
00521 }
00522 if (op == Variant::VOP_LT)
00523 {
00524 return a < b;
00525 }
00526 }
00527 if (Convert::IsInt(m_data) || Convert::IsInt(v.m_data))
00528 {
00529 int64 a, b;
00530 if (!m_data->ToInt64(a) || !v.m_data->ToInt64(b))
00531 {
00532 return false;
00533 }
00534 if (op == Variant::VOP_EQ)
00535 {
00536 return a == b;
00537 }
00538 if (op == Variant::VOP_GT)
00539 {
00540 return a > b;
00541 }
00542 if (op == Variant::VOP_LT)
00543 {
00544 return a < b;
00545 }
00546 }
00547 if (Compare::IsBoolMajic(m_data->MajicNumber()) || Compare::IsBoolMajic(v.m_data->MajicNumber()))
00548 {
00549 bool a, b;
00550 if (!m_data->ToBool(a) || !v.m_data->ToBool(b))
00551 {
00552 return false;
00553 }
00554 if (op == Variant::VOP_EQ)
00555 {
00556 return a == b;
00557 }
00558 if (op == Variant::VOP_GT)
00559 {
00560 return a > b;
00561 }
00562 if (op == Variant::VOP_LT)
00563 {
00564 return a < b;
00565 }
00566 }
00567 if (Compare::IsDateMajic(m_data->MajicNumber()) || Compare::IsDateMajic(v.m_data->MajicNumber()))
00568 {
00569 Date a, b;
00570 if (!m_data->ToDate(a) || !v.m_data->ToDate(b))
00571 {
00572 return false;
00573 }
00574 if (op == Variant::VOP_EQ)
00575 {
00576 return a == b;
00577 }
00578 if (op == Variant::VOP_GT)
00579 {
00580 return a > b;
00581 }
00582 if (op == Variant::VOP_LT)
00583 {
00584 return a < b;
00585 }
00586 }
00587 if (Compare::IsDateTimeMajic(m_data->MajicNumber()) || Compare::IsDateTimeMajic(v.m_data->MajicNumber()))
00588 {
00589 DateTime a, b;
00590 if (!m_data->ToDateTime(a) || !v.m_data->ToDateTime(b))
00591 {
00592 return false;
00593 }
00594 if (op == Variant::VOP_EQ)
00595 {
00596 return a == b;
00597 }
00598 if (op == Variant::VOP_GT)
00599 {
00600 return a > b;
00601 }
00602 if (op == Variant::VOP_LT)
00603 {
00604 return a < b;
00605 }
00606 }
00607 String a, b;
00608 if (!m_data->ToString(a) || !v.m_data->ToString(b))
00609 {
00610 return false;
00611 }
00612 if (op == Variant::VOP_EQ)
00613 {
00614 return a == b;
00615 }
00616 if (op == Variant::VOP_GT)
00617 {
00618 return a > b;
00619 }
00620 if (op == Variant::VOP_LT)
00621 {
00622 return a < b;
00623 }
00624 return false;
00625 }
00626
00627 bool Variant::operator ==(const Variant& v) const
00628 {
00629 return BinOp(v, Variant::VOP_EQ);
00630 }
00631
00632 bool Variant::operator !=(const Variant& v) const
00633 {
00634 return !(*this == v);
00635 }
00636
00637 bool Variant::operator >(const Variant& v) const
00638 {
00639 return BinOp(v, Variant::VOP_GT);
00640 }
00641
00642 bool Variant::operator <(const Variant& v) const
00643 {
00644 return BinOp(v, Variant::VOP_LT);
00645 }
00646
00647 bool Variant::operator >=(const Variant& v) const
00648 {
00649 return BinOp(v, Variant::VOP_GT) || BinOp(v, Variant::VOP_EQ);
00650 }
00651
00652 bool Variant::operator <=(const Variant& v) const
00653 {
00654 return BinOp(v, Variant::VOP_LT) || BinOp(v, Variant::VOP_EQ);
00655 }
00656
00657 RefCountPtr<Variant> Variant::ToVarBool() const
00658 {
00659 bool z;
00660 if (m_data->ToBool(z))
00661 {
00662 return VariantPtr(new Variant(z));
00663 }
00664 return VariantPtr(new Variant());
00665 }
00666
00667 RefCountPtr<Variant> Variant::ToVarByte() const
00668 {
00669 byte z;
00670 if (m_data->ToByte(z))
00671 {
00672 return VariantPtr(new Variant(z));
00673 }
00674 return VariantPtr(new Variant());
00675 }
00676
00677 RefCountPtr<Variant> Variant::ToVarInt16() const
00678 {
00679 int16 z;
00680 if (m_data->ToInt16(z))
00681 {
00682 return VariantPtr(new Variant(z));
00683 }
00684 return VariantPtr(new Variant());
00685 }
00686
00687 RefCountPtr<Variant> Variant::ToVarInt32() const
00688 {
00689 int32 z;
00690 if (m_data->ToInt32(z))
00691 {
00692 return VariantPtr(new Variant(z));
00693 }
00694 return VariantPtr(new Variant());
00695 }
00696
00697 RefCountPtr<Variant> Variant::ToVarInt64() const
00698 {
00699 int64 z;
00700 if (m_data->ToInt64(z))
00701 {
00702 return VariantPtr(new Variant(z));
00703 }
00704 return VariantPtr(new Variant());
00705 }
00706
00707 RefCountPtr<Variant> Variant::ToVarFloat32() const
00708 {
00709 float32 z;
00710 if (m_data->ToFloat32(z))
00711 {
00712 return VariantPtr(new Variant(z));
00713 }
00714 return VariantPtr(new Variant());
00715 }
00716
00717 RefCountPtr<Variant> Variant::ToVarFloat64() const
00718 {
00719 float64 z;
00720 if (m_data->ToFloat64(z))
00721 {
00722 return VariantPtr(new Variant(z));
00723 }
00724 return VariantPtr(new Variant());
00725 }
00726
00727 RefCountPtr<Variant> Variant::ToVarDateTime() const
00728 {
00729 DateTime z;
00730 if (m_data->ToDateTime(z))
00731 {
00732 return VariantPtr(new Variant(z));
00733 }
00734 return VariantPtr(new Variant());
00735 }
00736
00737 RefCountPtr<Variant> Variant::ToVarDate() const
00738 {
00739 Date z;
00740 if (m_data->ToDate(z))
00741 {
00742 return VariantPtr(new Variant(z));
00743 }
00744 return VariantPtr(new Variant());
00745 }
00746
00747 RefCountPtr<Variant> Variant::ToVarDecimal() const
00748 {
00749 Decimal z;
00750 if (m_data->ToDecimal(z))
00751 {
00752 return VariantPtr(new Variant(z));
00753 }
00754 return VariantPtr(new Variant());
00755 }
00756
00757 RefCountPtr<Variant> Variant::ToVarString() const
00758 {
00759 String z;
00760 if (m_data->ToString(z))
00761 {
00762 return VariantPtr(new Variant(z));
00763 }
00764 return VariantPtr(new Variant());
00765 }
00766
00767 RefCountPtr<Variant> Variant::Add(const Variant& v) const
00768 {
00769 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00770 {
00771 float64 a, b;
00772 if (!Convert::ToFloat64(m_data, a) || !Convert::ToFloat64(v.m_data, b))
00773 {
00774 return VariantPtr(new Variant());
00775 }
00776 return VariantPtr(new Variant(a + b));
00777 }
00778 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00779 {
00780 int64 a, b;
00781 Convert::ToInt64(m_data, a);
00782 Convert::ToInt64(v.m_data, b);
00783 return VariantPtr(new Variant(a + b));
00784 }
00785
00786 String a, b;
00787 Convert::ToString(m_data, a);
00788 Convert::ToString(v.m_data, b);
00789
00790 return VariantPtr(new Variant( a + b ));
00791 }
00792
00793 RefCountPtr<Variant> Variant::Sub(const Variant& v) const
00794 {
00795 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00796 {
00797 float64 a, b;
00798 if (!Convert::ToFloat64(m_data, a) || !Convert::ToFloat64(v.m_data, b))
00799 {
00800 return VariantPtr(new Variant());
00801 }
00802 return VariantPtr(new Variant(a - b));
00803 }
00804 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00805 {
00806 int64 a, b;
00807 Convert::ToInt64(m_data, a);
00808 Convert::ToInt64(v.m_data, b);
00809 return VariantPtr(new Variant(a - b));
00810 }
00811 return VariantPtr(new Variant());
00812 }
00813
00814 RefCountPtr<Variant> Variant::Div(const Variant& v) const
00815 {
00816 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00817 {
00818 float64 a, b;
00819 if (!Convert::ToFloat64(m_data, a) || !Convert::ToFloat64(v.m_data, b))
00820 {
00821 return VariantPtr(new Variant());
00822 }
00823 return VariantPtr(new Variant(a / b));
00824 }
00825 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00826 {
00827 int64 a, b;
00828 Convert::ToInt64(m_data, a);
00829 Convert::ToInt64(v.m_data, b);
00830 return VariantPtr(new Variant(a / b));
00831 }
00832 return VariantPtr(new Variant());
00833 }
00834
00835 RefCountPtr<Variant> Variant::Mul(const Variant& v) const
00836 {
00837 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00838 {
00839 float64 a, b;
00840 if (!Convert::ToFloat64(m_data, a) || !Convert::ToFloat64(v.m_data, b))
00841 {
00842 return VariantPtr(new Variant());
00843 }
00844 return VariantPtr(new Variant(a * b));
00845 }
00846 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00847 {
00848 int64 a, b;
00849 Convert::ToInt64(m_data, a);
00850 Convert::ToInt64(v.m_data, b);
00851 return VariantPtr(new Variant(a * b));
00852 }
00853 return VariantPtr(new Variant());
00854 }
00855
00856 RefCountPtr<Variant> Variant::Mod(const Variant& v) const
00857 {
00858 if (Convert::IsFloat(m_data) || Convert::IsFloat(v.m_data))
00859 {
00860 float64 a, b;
00861 if (!Convert::ToFloat64(m_data, a) || !Convert::ToFloat64(v.m_data, b))
00862 {
00863 return VariantPtr(new Variant());
00864 }
00865 return VariantPtr(new Variant(Math::Remainder(a, b)));
00866 }
00867 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00868 {
00869 int64 a, b;
00870 Convert::ToInt64(m_data, a);
00871 Convert::ToInt64(v.m_data, b);
00872 return VariantPtr(new Variant(a % b));
00873 }
00874 return VariantPtr(new Variant());
00875 }
00876
00877 RefCountPtr<Variant> Variant::Exp(const Variant& v) const
00878 {
00879 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00880 {
00881 int64 a, b;
00882 Convert::ToInt64(m_data, a);
00883 Convert::ToInt64(v.m_data, b);
00884 return VariantPtr(new Variant(a ^ b));
00885 }
00886 bool ba, bb;
00887 if (Convert::ToBool(m_data, ba) && Convert::ToBool(v.m_data, bb))
00888 {
00889 return VariantPtr(new Variant(ba ^ bb));
00890 }
00891 return VariantPtr(new Variant());
00892 }
00893
00894 RefCountPtr<Variant> Variant::ShiftLeft(const Variant& v) const
00895 {
00896 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00897 {
00898 int64 a, b;
00899 Convert::ToInt64(m_data, a);
00900 Convert::ToInt64(v.m_data, b);
00901 return VariantPtr(new Variant(a << b));
00902 }
00903 return VariantPtr(new Variant());
00904 }
00905
00906 RefCountPtr<Variant> Variant::ShiftRigth(const Variant& v) const
00907 {
00908 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00909 {
00910 int64 a, b;
00911 Convert::ToInt64(m_data, a);
00912 Convert::ToInt64(v.m_data, b);
00913 return VariantPtr(new Variant(a >> b));
00914 }
00915 return VariantPtr(new Variant());
00916 }
00917
00918 RefCountPtr<Variant> Variant::OrBin(const Variant& v) const
00919 {
00920 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00921 {
00922 int64 a, b;
00923 Convert::ToInt64(m_data, a);
00924 Convert::ToInt64(v.m_data, b);
00925 return VariantPtr(new Variant(a | b));
00926 }
00927 bool ba, bb;
00928 if (Convert::ToBool(m_data, ba) && Convert::ToBool(v.m_data, bb))
00929 {
00930 return VariantPtr(new Variant(ba | bb));
00931 }
00932 return VariantPtr(new Variant());
00933 }
00934
00935 RefCountPtr<Variant> Variant::OrLog(const Variant& v) const
00936 {
00937 bool ba, bb;
00938 if (Convert::ToBool(m_data, ba) && Convert::ToBool(v.m_data, bb))
00939 {
00940 return VariantPtr(new Variant(ba || bb));
00941 }
00942 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00943 {
00944 int64 a, b;
00945 Convert::ToInt64(m_data, a);
00946 Convert::ToInt64(v.m_data, b);
00947 return VariantPtr(new Variant(a || b));
00948 }
00949 return VariantPtr(new Variant());
00950 }
00951
00952 RefCountPtr<Variant> Variant::AndBin(const Variant& v) const
00953 {
00954 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00955 {
00956 int64 a, b;
00957 Convert::ToInt64(m_data, a);
00958 Convert::ToInt64(v.m_data, b);
00959 return VariantPtr(new Variant(a & b));
00960 }
00961 bool ba, bb;
00962 if (Convert::ToBool(m_data, ba) && Convert::ToBool(v.m_data, bb))
00963 {
00964 return VariantPtr(new Variant(ba & bb));
00965 }
00966 return VariantPtr(new Variant());
00967 }
00968
00969 RefCountPtr<Variant> Variant::AndLog(const Variant& v) const
00970 {
00971 bool ba, bb;
00972 if (Convert::ToBool(m_data, ba) && Convert::ToBool(v.m_data, bb))
00973 {
00974 return VariantPtr(new Variant(ba && bb));
00975 }
00976 if (Convert::CanConvertToInt(m_data) && Convert::CanConvertToInt(v.m_data))
00977 {
00978 int64 a, b;
00979 Convert::ToInt64(m_data, a);
00980 Convert::ToInt64(v.m_data, b);
00981 return VariantPtr(new Variant(a && b));
00982 }
00983 return VariantPtr(new Variant());
00984 }
00985
00986 RefCountPtr<Variant> Variant::Not() const
00987 {
00988 bool b;
00989 if (Convert::ToBool(m_data, b))
00990 {
00991 return VariantPtr(new Variant(!b));
00992 }
00993 if (Convert::CanConvertToInt(m_data))
00994 {
00995 int64 a;
00996 Convert::ToInt64(m_data, a);
00997 return VariantPtr(new Variant(!a));
00998 }
00999 return VariantPtr(new Variant());
01000 }
01001
01002 RefCountPtr<Variant> Variant::Comp() const
01003 {
01004 if (Convert::CanConvertToInt(m_data))
01005 {
01006 int64 a;
01007 Convert::ToInt64(m_data, a);
01008 return VariantPtr(new Variant(~a));
01009 }
01010 return VariantPtr(new Variant());
01011 }
01012
01013 RefCountPtr<Variant> Variant::Neg() const
01014 {
01015 if (Convert::CanConvertToInt(m_data))
01016 {
01017 int64 a;
01018 Convert::ToInt64(m_data, a);
01019 return VariantPtr(new Variant(-a));
01020 }
01021 if (Convert::CanConvertToFloat(m_data))
01022 {
01023 float64 a;
01024 Convert::ToFloat64(m_data, a);
01025 return VariantPtr(new Variant(-a));
01026 }
01027 return VariantPtr(new Variant());
01028 }
01029
01030 VariantPtr Variant::Clone() const
01031 {
01032 VariantPtr v(new Variant());
01033 *v = *this;
01034 return v;
01035 }
01036
01037 #ifdef DEBUG
01038 void Variant::ValidateMem() const
01039 {
01040 ASSERT_PTR(m_data);
01041 m_data->ValidateMem();
01042 }
01043
01044 void Variant::CheckMem() const
01045 {
01046 DEBUG_NOTE_MEM(m_data);
01047 m_data->CheckMem();
01048 }
01049 #endif