00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _Variant_h
00018 #define _Variant_h
00019
00020 #include <spl/Convert.h>
00021 #include <spl/IVariant.h>
00022 #include <spl/interp/IJsObject.h>
00023 #include <spl/RefCountPtr.h>
00024
00031 class IVariantData : public IConvertable
00032 {
00033 public:
00034 IVariantData();
00035 virtual ~IVariantData();
00036
00037 virtual int32 HashCode() const = 0;
00038 virtual bool Equals( const IComparable& a ) const = 0;
00039 virtual int Compare( const IComparable& a ) const = 0;
00040
00041 virtual bool IsObject() const = 0;
00042
00043 virtual int32 MajicNumber() const = 0;
00044
00045 virtual bool ToChar(char& c2) const = 0;
00046 virtual bool ToBool(bool& c2) const = 0;
00047 virtual bool ToByte(byte& c2) const = 0;
00048 virtual bool ToInt16(int16& c2) const = 0;
00049 virtual bool ToInt32(int32& c2) const = 0;
00050 virtual bool ToInt64(int64& c2) const = 0;
00051 virtual bool ToUInt32(uint32& c2) const = 0;
00052 virtual bool ToUInt64(uint64& c2) const = 0;
00053
00054 virtual bool ToFloat32(float32& c2) const = 0;
00055 virtual bool ToFloat64(float64& c2) const = 0;
00056 virtual bool ToDateTime(DateTime& c2) const = 0;
00057 virtual bool ToDate(Date& c2) const = 0;
00058 virtual bool ToDecimal(Decimal& c2) const = 0;
00059 virtual bool ToString(String& c2) const = 0;
00060 virtual bool ToObject(IJsObject& c2) const = 0;
00061
00062 virtual String TypeName() const = 0;
00063
00064 virtual IVariantData *Clone() = 0;
00065 virtual void *Data() = 0;
00066 };
00067
00068 template<typename T>
00069 class _VariantData : public IVariantData
00070 {
00071 private:
00072 T m_data;
00073
00074 public:
00075 _VariantData()
00076 {
00077 }
00078
00079 _VariantData(T data)
00080 : m_data(data)
00081 {
00082 }
00083
00084 _VariantData(const _VariantData<T>& vd)
00085 : m_data()
00086 {
00087 throw new NotImplementedException("Do not copy construct _VariantData");
00088 }
00089
00090 virtual ~_VariantData()
00091 {
00092 }
00093
00094 inline _VariantData<T>& operator =(const _VariantData<T> &v)
00095 {
00096 throw new NotImplementedException("Do not assign _VariantData");
00097 }
00098
00099 virtual bool Equals( const IComparable& a ) const
00100 {
00101 return Compare::Equals(a, m_data);
00102 }
00103
00104 virtual int Compare( const IComparable& a ) const
00105 {
00106 return static_cast<int>(Compare::Cmp(m_data, a));
00107 }
00108
00109 virtual int32 MajicNumber() const
00110 {
00111 return Compare::Majic(m_data);
00112
00113 }
00114
00115 virtual int32 HashCode() const
00116 {
00117 return Math::Hash(m_data);
00118 }
00119
00120 virtual bool ToChar(char& c2) const
00121 {
00122 return Convert::ToChar(m_data, c2);
00123 }
00124
00125 virtual bool ToBool(bool& c2) const
00126 {
00127 return Convert::ToBool(m_data, c2);
00128 }
00129
00130 virtual bool ToByte(byte& c2) const
00131 {
00132 return Convert::ToByte(m_data, c2);
00133 }
00134
00135 virtual bool ToInt16(int16& c2) const
00136 {
00137 return Convert::ToInt16(m_data, c2);
00138 }
00139
00140 virtual bool ToInt32(int32& c2) const
00141 {
00142 return Convert::ToInt32(m_data, c2);
00143 }
00144
00145 virtual bool ToInt64(int64& c2) const
00146 {
00147 return Convert::ToInt64(m_data, c2);
00148 }
00149
00150 virtual bool ToUInt32(uint32& c2) const
00151 {
00152 return Convert::ToUInt32(m_data, c2);
00153 }
00154
00155 virtual bool ToUInt64(uint64& c2) const
00156 {
00157 return Convert::ToUInt64(m_data, c2);
00158 }
00159
00160
00161
00162
00163
00164 virtual bool ToFloat32(float32& c2) const
00165 {
00166 return Convert::ToFloat32(m_data, c2);
00167 }
00168
00169 virtual bool ToFloat64(float64& c2) const
00170 {
00171 return Convert::ToFloat64(m_data, c2);
00172 }
00173
00174 virtual bool ToDateTime(DateTime& c2) const
00175 {
00176 return Convert::ToDateTime(m_data, c2);
00177 }
00178
00179 virtual bool ToDate(Date& c2) const
00180 {
00181 return Convert::ToDate(m_data, c2);
00182 }
00183
00184 virtual bool ToDecimal(Decimal& c2) const
00185 {
00186 return Convert::ToDecimal(m_data, c2);
00187 }
00188
00189 virtual bool ToString(String& c2) const
00190 {
00191 return Convert::ToString(m_data, c2);
00192 }
00193
00194 virtual bool ToObject(IJsObject& c2) const
00195 {
00196 return Convert::ToObject(m_data, c2);
00197 }
00198
00199 virtual bool IsObject() const
00200 {
00201 return Convert::IsObject(m_data);
00202 }
00203
00204 virtual String TypeName() const
00205 {
00206 return Convert::TypeName(m_data);
00207 }
00208
00209 virtual IVariantData *Clone()
00210 {
00211 return new _VariantData<T>(m_data);
00212 }
00213
00214 inline T Get()
00215 {
00216 return m_data;
00217 }
00218
00219 inline T& GetRef()
00220 {
00221 return m_data;
00222 }
00223
00224 virtual void *Data()
00225 {
00226 return &m_data;
00227 }
00228
00229 #ifdef DEBUG
00230 void ValidateMem() const
00231 {
00232 ValidateType(m_data);
00233 }
00234
00235 void CheckMem() const
00236 {
00237 ValidateType(m_data);
00238 }
00239 #endif
00240 };
00241
00242 class Variant;
00243 typedef RefCountPtr<Variant> VariantPtr;
00244 typedef WeakReference<Variant, VariantPtr> VariantRef;
00245
00246 REGISTER_TYPEOF( 584, VariantPtr );
00247 REGISTER_TYPEOF( 586, VariantRef );
00248 REGISTER_TYPEOF( 606, Array<VariantPtr> );
00249 REGISTER_TYPEOF( 610, Vector<VariantPtr> );
00250
00254 class Variant : public IVariant
00255 {
00256 private:
00257 IVariantData *m_data;
00258
00259 enum CompareOperator
00260 {
00261 VOP_EQ,
00262 VOP_GT,
00263 VOP_LT
00264 };
00265
00266 bool BinOp(const Variant& b, enum CompareOperator op) const;
00267
00268 public:
00269 Variant();
00270 Variant(bool flag);
00271 Variant(int8 i8);
00272 Variant(int16 i16);
00273 Variant(int32 i32);
00274 Variant(int64 i64);
00275 Variant(float32 f32);
00276 Variant(float64 f64);
00277 Variant(const DateTime& dtm);
00278 Variant(const Date& dt);
00279 Variant(const String& str);
00280 Variant(const String *str);
00281 Variant(const char *str);
00282 Variant(const Decimal& dec);
00283 Variant(const Variant& v);
00284 Variant(const Undefined& v);
00285 Variant(RefCountPtr<IJsObject> jso);
00286 virtual ~Variant();
00287
00288 VariantPtr ToVarBool() const;
00289 VariantPtr ToVarByte() const;
00290 VariantPtr ToVarInt16() const;
00291 VariantPtr ToVarInt32() const;
00292 VariantPtr ToVarInt64() const;
00293 VariantPtr ToVarFloat32() const;
00294 VariantPtr ToVarFloat64() const;
00295 VariantPtr ToVarDateTime() const;
00296 VariantPtr ToVarDate() const;
00297 VariantPtr ToVarDecimal() const;
00298 VariantPtr ToVarString() const;
00299
00300 virtual bool ToBool() const;
00301 virtual byte ToByte() const;
00302 virtual int16 ToInt16() const;
00303 virtual int32 ToInt32() const;
00304 virtual int64 ToInt64() const;
00305 virtual float32 ToFloat32() const;
00306 virtual float64 ToFloat64() const;
00307 virtual DateTime ToDateTime() const;
00308 virtual Date ToDate() const;
00309 virtual Decimal ToDecimal() const;
00310 virtual StringPtr ToString() const;
00311 virtual RefCountPtr<IJsObject> ToObject() const;
00312
00313 virtual bool ToChar(char& c2) const;
00314 virtual bool ToBool(bool& c2) const;
00315 virtual bool ToByte(byte& c2) const;
00316 virtual bool ToInt16(int16& c2) const;
00317 virtual bool ToInt32(int32& c2) const;
00318 virtual bool ToInt64(int64& c2) const;
00319 virtual bool ToUInt32(uint32& c2) const;
00320 virtual bool ToUInt64(uint64& c2) const;
00321
00322 virtual bool ToFloat32(float32& c2) const;
00323 virtual bool ToFloat64(float64& c2) const;
00324 virtual bool ToDateTime(DateTime& c2) const;
00325 virtual bool ToDate(Date& c2) const;
00326 virtual bool ToDecimal(Decimal& c2) const;
00327 virtual bool ToString(String& c2) const;
00328
00329 virtual bool Equals( const IComparable& a ) const;
00330 bool Equals( const Variant& v ) const;
00331 virtual int Compare( const IComparable& a ) const;
00332 virtual int32 MajicNumber() const;
00333 virtual int32 HashCode() const;
00334
00335 bool IsObject() const;
00336
00337 VariantPtr ParseInt() const;
00338 VariantPtr ParseFloat() const;
00339
00340 String TypeName() const;
00341
00342 inline String TypeNameInner() const
00343 {
00344 return m_data->TypeName();
00345 }
00346
00347 inline VariantPtr ToRValue()
00348 {
00349 return RefCountPtr<Variant>(new Variant(*this));
00350 }
00351
00352 inline void Clear()
00353 {
00354 delete m_data;
00355 m_data = new _VariantData<Undefined>(Undefined());
00356 }
00357
00358 Variant& operator =(const Variant& v);
00359 Variant& operator =(const int32 i);
00360 Variant& operator =(const int64 i);
00361 Variant& operator =(const float64 d);
00362 Variant& operator =(const String& s);
00363 Variant& operator =(const char *s);
00364 Variant& operator =(const DateTime& s);
00365 Variant& operator =(const Date& s);
00366 Variant& operator =(const Decimal& s);
00367 Variant& operator =(RefCountPtr<IJsObject> s);
00368
00369 bool operator ==(const Variant& v) const;
00370 bool operator !=(const Variant& v) const;
00371 bool operator >(const Variant& v) const;
00372 bool operator <(const Variant& v) const;
00373 bool operator >=(const Variant& v) const;
00374 bool operator <=(const Variant& v) const;
00375
00376 VariantPtr Add(const Variant& v) const;
00377 VariantPtr Sub(const Variant& v) const;
00378 VariantPtr Div(const Variant& v) const;
00379 VariantPtr Mul(const Variant& v) const;
00380 VariantPtr Mod(const Variant& v) const;
00381 VariantPtr Exp(const Variant& v) const;
00382 VariantPtr ShiftLeft(const Variant& v) const;
00383 VariantPtr ShiftRigth(const Variant& v) const;
00384 VariantPtr OrBin(const Variant& v) const;
00385 VariantPtr OrLog(const Variant& v) const;
00386 VariantPtr AndBin(const Variant& v) const;
00387 VariantPtr AndLog(const Variant& v) const;
00388 VariantPtr Not() const;
00389 VariantPtr Comp() const;
00390 VariantPtr Neg() const;
00391
00392 inline Variant operator +(const Variant& v) const { return *Add(v); }
00393 inline Variant operator -(const Variant& v) const { return *Sub(v); }
00394 inline Variant operator /(const Variant& v) const { return *Div(v); }
00395 inline Variant operator *(const Variant& v) const { return *Mul(v); }
00396 inline Variant operator %(const Variant& v) const { return *Mod(v); }
00397 inline Variant operator ^(const Variant& v) const { return *Exp(v); }
00398 inline Variant operator <<(const Variant& v) const { return *ShiftLeft(v); }
00399 inline Variant operator >>(const Variant& v) const { return *ShiftRigth(v); }
00400 inline Variant operator |(const Variant& v) const { return *OrBin(v); }
00401 inline Variant operator ||(const Variant& v) const { return *OrLog(v); }
00402 inline Variant operator &(const Variant& v) const { return *AndBin(v); }
00403 inline Variant operator &&(const Variant& v) const { return *AndLog(v); }
00404 inline Variant operator !() const { return *Not(); }
00405 inline Variant operator ~() const { return *Comp(); }
00406 inline Variant operator -() const { return *Neg(); }
00407
00408 VariantPtr Clone() const;
00409
00410 inline bool IsUndefined() const { return m_data->MajicNumber() == UNDEFINED_MAJIC; }
00411
00412 #ifdef DEBUG
00413 virtual void ValidateMem() const;
00414 virtual void CheckMem() const;
00415 #endif
00416 };
00417
00418 inline void ValidateType(VariantPtr v)
00419 {
00420 #ifdef DEBUG
00421 v.ValidateMem();
00422 v.CheckMem();
00423 #endif
00424 }
00425
00426 REGISTER_TYPEOF( 582, Variant );
00427 REGISTER_TYPEOF( 600, Array<Variant> );
00428 REGISTER_TYPEOF( 604, Vector<Variant> );
00429
00432 #endif