include/berkelium/ScriptVariant.hpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #ifndef _BERKELIUM_SCRIPT_VARIANT_HPP_
00034 #define _BERKELIUM_SCRIPT_VARIANT_HPP_
00035 #include "berkelium/WeakString.hpp"
00036 
00037 namespace Berkelium {
00038 namespace Script {
00039 
00040 class BERKELIUM_EXPORT Variant {
00041 public:
00042     enum Type {
00043         JSSTRING,
00044         JSDOUBLE,
00045         JSBOOLEAN,
00046         JSNULL,
00047         JSEMPTYOBJECT,
00048         JSEMPTYARRAY,
00049         JSBINDFUNC,
00050         JSBINDSYNCFUNC
00051     };
00052 private:
00053     union {
00054         WideString mStrPointer;
00055         double mDoubleValue;
00056     };
00057 
00058     Type mType;
00059     void initwc(const wchar_t* str, size_t length) ;
00060     void initmb(const char* str, size_t length);
00061     void initdbl(double dblval);
00062     void initbool(bool boolval);
00063     void initnull(Type typ);
00064     void initvariant(const Variant& other);
00065     void destroy();
00066     bool hasString() {
00067         return mType == JSSTRING || mType == JSBINDFUNC || mType == JSBINDSYNCFUNC;
00068     }
00069     Variant(Type type) {
00070         initnull(type);
00071     }
00072     Variant(WideString str, Type type);
00073 public:
00074     Variant(const char* str);
00075     Variant(const wchar_t* str);
00076     Variant(WideString str);
00077     Variant(double dblval) {
00078         initdbl(dblval);
00079     }
00080     Variant(int intval) {
00081         initdbl(intval);
00082     }
00083     Variant(bool boolval) {
00084         initbool(boolval);
00085     }
00086     Variant() {
00087         initnull(JSNULL);
00088     }
00089 
00090     static Variant emptyArray();
00091     static Variant emptyObject();
00092 
00093     static Variant bindFunction(WideString name, bool synchronous) {
00094         return Variant(name, synchronous ? JSBINDSYNCFUNC: JSBINDFUNC);
00095     }
00096 
00097     Variant(const Variant& other);
00098     Variant& operator=(const Variant& other);
00099 
00100     bool toBoolean() const {
00101         if (mType == JSDOUBLE || mType == JSBOOLEAN) {
00102             return mDoubleValue != 0;
00103         } else if (mType == JSSTRING) {
00104             return mStrPointer.length() ? true : false;
00105         } else {
00106             return false;
00107         }
00108     }
00109     int toInteger() const {
00110         if (mType == JSDOUBLE || mType == JSBOOLEAN) {
00111             return (int)mDoubleValue;
00112         } else {
00113             return 0;
00114         }
00115     }
00116     double toDouble() const {
00117         if (mType == JSDOUBLE || mType == JSBOOLEAN) {
00118             return mDoubleValue;
00119         } else {
00120             return 0;
00121         }
00122     }
00123 
00124     WideString toString() const {
00125         if (mType == JSSTRING) {
00126             return mStrPointer;
00127         } else {
00128             return WideString::empty();
00129         }
00130     }
00131 
00132     WideString toFunctionName() const {
00133         if (mType == JSBINDFUNC || mType == JSBINDSYNCFUNC) {
00134             return mStrPointer;
00135         } else {
00136             return WideString::empty();
00137         }
00138     }
00139 
00140     Type type() const {
00141         return mType;
00142     }
00143 
00144     ~Variant();
00145 };
00146 
00147 }
00148 }
00149 
00150 #endif