From 7e51bf3804e50ea063fcc97b2682a32a8505902f Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 24 Oct 2014 22:22:26 +0000 Subject: added Function, Property and Object classes; added CMakeLists entries for the mozjs-24 library (the Firefox JavaScript engine which is available as Package on Fedora); added Doxygen target to makefile git-svn-id: file:///var/local/svn/basicwriter@75 daaaf23c-2e50-4459-9457-1e69db5a47bf --- src/core/script/Variant.hpp | 229 ++++++-------------------------------------- 1 file changed, 31 insertions(+), 198 deletions(-) (limited to 'src/core/script/Variant.hpp') diff --git a/src/core/script/Variant.hpp b/src/core/script/Variant.hpp index 923c8ca..295c044 100644 --- a/src/core/script/Variant.hpp +++ b/src/core/script/Variant.hpp @@ -29,6 +29,10 @@ namespace ousia { namespace script { +/* Class forward declarations */ +class Object; +class Function; + /** * Enum containing the possible types a variant may have. */ @@ -50,7 +54,6 @@ enum class VariantType : int16_t { * is not supported for the current variant type. */ class VariantTypeException : public std::exception { - private: /** * Internally used string holding the exception message. @@ -82,8 +85,7 @@ public: * * @return the error message as C string. */ - virtual const char* what() const noexcept override; - + virtual const char *what() const noexcept override; }; /** @@ -91,9 +93,7 @@ public: * between the host application and the script engine. Variants are immutable. */ class Variant { - private: - const VariantType type; union { @@ -104,213 +104,46 @@ private: }; public: - - Variant(const Variant &v) : - type(v.type) - { - switch (v.type) { - case VariantType::null: - break; - case VariantType::boolean: - booleanValue = v.booleanValue; - break; - case VariantType::integer: - integerValue = v.integerValue; - break; - case VariantType::number: - numberValue = v.numberValue; - break; - case VariantType::string: - objectValue = new std::string( - *static_cast(v.objectValue)); - break; - case VariantType::array: - objectValue = new std::vector( - *static_cast*>(v.objectValue)); - break; - case VariantType::map: - objectValue = new std::map( - *static_cast*>(v.objectValue)); - break; - case VariantType::function: - case VariantType::object: - case VariantType::buffer: - // TODO - break; - } - } - - Variant(Variant &&v) : - type(v.type) - { - switch (type) { - case VariantType::null: - break; - case VariantType::boolean: - booleanValue = v.booleanValue; - break; - case VariantType::integer: - integerValue = v.integerValue; - break; - case VariantType::number: - numberValue = v.numberValue; - break; - case VariantType::string: - case VariantType::array: - case VariantType::map: - case VariantType::function: - case VariantType::object: - case VariantType::buffer: - objectValue = v.objectValue; - v.objectValue = nullptr; - break; - } - } - - ~Variant() - { - switch (type) { - case VariantType::string: - delete static_cast(objectValue); - break; - case VariantType::array: - delete static_cast*>(objectValue); - break; - case VariantType::map: - delete static_cast*>(objectValue); - break; - default: - break; - } - } - - Variant& operator=(const Variant &v) = delete; - Variant& operator=(Variant &&v) = delete; - - Variant() : - type(VariantType::null) {} - - Variant(bool b) : - type(VariantType::boolean), - booleanValue(b) {} - - Variant(int64_t i) : - type(VariantType::integer), - integerValue(i) {} - - Variant(double d) : - type(VariantType::number), - numberValue(d) {} - - Variant(const char *s) : - type(VariantType::string), - objectValue(new std::string(s)) {} - - Variant(const std::vector &a) : - type(VariantType::array), - objectValue(new std::vector(a)) {} - - Variant(const std::map &m) : - type(VariantType::map), - objectValue(new std::map(m)) {} + Variant(const Variant &v); + Variant(Variant &&v); + + Variant(); + Variant(bool b); + Variant(int64_t i); + Variant(double d); + Variant(const char *s); + Variant(const std::vector &a); + Variant(const std::map &m); + Variant(const Function *f); + Variant(const Object &o); + ~Variant(); + + Variant &operator=(const Variant &v) = delete; + Variant &operator=(Variant &&v) = delete; VariantType getType() const { return type; } - bool getBooleanValue() const - { - switch (type) { - case VariantType::null: - return false; - case VariantType::boolean: - return booleanValue; - case VariantType::integer: - return integerValue != 0; - case VariantType::number: - return numberValue != 0.0; - case VariantType::string: - return !getStringValue().empty(); - case VariantType::array: - return !getArrayValue().empty(); - case VariantType::map: - return !getMapValue().empty(); - default: - throw VariantTypeException{type, VariantType::boolean}; - } - } - - int64_t getIntegerValue() const - { - switch (type) { - case VariantType::boolean: - return booleanValue ? 1 : 0; - case VariantType::integer: - return integerValue; - case VariantType::number: - return static_cast(numberValue); - default: - throw VariantTypeException{type, VariantType::integer}; - } - } + bool getBooleanValue() const; + int64_t getIntegerValue() const; + double getNumberValue() const; + const std::string &getStringValue() const; + const std::vector &getArrayValue() const; + const std::map &getMapValue() const; + const Function *getFunctionValue() const; + const Object &getObjectValue() const; - double getNumberValue() const - { - switch (type) { - case VariantType::boolean: - return booleanValue ? 1.0 : 0.0; - case VariantType::integer: - return static_cast(integerValue); - case VariantType::number: - return numberValue; - default: - throw VariantTypeException{type, VariantType::number}; - } - } - - const std::string& getStringValue() const - { - switch (type) { - case VariantType::string: - return *(static_cast(objectValue)); - default: - throw VariantTypeException{type, VariantType::string}; - } - } - - const std::vector& getArrayValue() const - { - switch (type) { - case VariantType::array: - return *(static_cast*>(objectValue)); - default: - throw VariantTypeException{type, VariantType::array}; - } - } - - const std::map& getMapValue() const - { - switch (type) { - case VariantType::map: - return *(static_cast*>(objectValue)); - default: - throw VariantTypeException{type, VariantType::map}; - } - } - - static const char* getTypeName(VariantType type); - - friend std::ostream& operator<< (std::ostream& os, const Variant &v); + static const char *getTypeName(VariantType type); + friend std::ostream &operator<<(std::ostream &os, const Variant &v); }; - /** * Shorthand for a constant representing a "null" as a variant. */ static const Variant VarNull; - } } -- cgit v1.2.3