summaryrefslogtreecommitdiff
path: root/src/core/script/Variant.hpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-10-24 22:22:26 +0000
committerandreas <andreas@daaaf23c-2e50-4459-9457-1e69db5a47bf>2014-10-24 22:22:26 +0000
commit7e51bf3804e50ea063fcc97b2682a32a8505902f (patch)
tree6a6097216708049b5dd7a4c43020c85aafb046a1 /src/core/script/Variant.hpp
parent4a9912f516bf096c6f8c6259b3fc6ba4b95b8d69 (diff)
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
Diffstat (limited to 'src/core/script/Variant.hpp')
-rw-r--r--src/core/script/Variant.hpp229
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;
-
}
}