Berkelium

include/berkelium/ScriptVariant.hpp

Go to the documentation of this file.
00001 /*  Berkelium - Embedded Chromium
00002  *  ScriptVariant.hpp
00003  *
00004  *  Copyright (c) 2010, Patrick Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated on 22 Nov 2013