diff options
Diffstat (limited to 'src/core/script/Variant.hpp')
-rw-r--r-- | src/core/script/Variant.hpp | 229 |
1 files changed, 31 insertions, 198 deletions
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<std::string*>(v.objectValue)); - break; - case VariantType::array: - objectValue = new std::vector<Variant>( - *static_cast<std::vector<Variant>*>(v.objectValue)); - break; - case VariantType::map: - objectValue = new std::map<std::string, Variant>( - *static_cast<std::map<std::string, Variant>*>(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<std::string*>(objectValue); - break; - case VariantType::array: - delete static_cast<std::vector<Variant>*>(objectValue); - break; - case VariantType::map: - delete static_cast<std::map<std::string, Variant>*>(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<Variant> &a) : - type(VariantType::array), - objectValue(new std::vector<Variant>(a)) {} - - Variant(const std::map<std::string, Variant> &m) : - type(VariantType::map), - objectValue(new std::map<std::string, Variant>(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<Variant> &a); + Variant(const std::map<std::string, Variant> &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<int64_t>(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<Variant> &getArrayValue() const; + const std::map<std::string, Variant> &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<double>(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<std::string*>(objectValue)); - default: - throw VariantTypeException{type, VariantType::string}; - } - } - - const std::vector<Variant>& getArrayValue() const - { - switch (type) { - case VariantType::array: - return *(static_cast<std::vector<Variant>*>(objectValue)); - default: - throw VariantTypeException{type, VariantType::array}; - } - } - - const std::map<std::string, Variant>& getMapValue() const - { - switch (type) { - case VariantType::map: - return *(static_cast<std::map<std::string, Variant>*>(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; - } } |