diff options
Diffstat (limited to 'src/core/variant')
-rw-r--r-- | src/core/variant/Variant.cpp | 115 | ||||
-rw-r--r-- | src/core/variant/Variant.hpp | 69 |
2 files changed, 165 insertions, 19 deletions
diff --git a/src/core/variant/Variant.cpp b/src/core/variant/Variant.cpp index c86905c..d33cd4f 100644 --- a/src/core/variant/Variant.cpp +++ b/src/core/variant/Variant.cpp @@ -16,10 +16,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <sstream> + +#include <core/Utils.hpp> + #include "Variant.hpp" namespace ousia { +/* Class Variant::TypeException */ + +Variant::TypeException::TypeException(Type actualType, Type requestedType) + : OusiaException(std::string("Variant: Requested \"") + + Variant::getTypeName(requestedType) + + std::string("\" but is \"") + + Variant::getTypeName(actualType) + std::string("\"")), + actualType(actualType), + requestedType(requestedType) +{ +} + /* Class Variant */ const char *Variant::getTypeName(Type type) @@ -32,7 +48,7 @@ const char *Variant::getTypeName(Type type) case Type::INT: return "integer"; case Type::DOUBLE: - return "number"; + return "double"; case Type::STRING: return "string"; case Type::ARRAY: @@ -43,16 +59,97 @@ const char *Variant::getTypeName(Type type) return "unknown"; } -/* Class VariantTypeException */ +Variant::boolType Variant::toBool() const +{ + switch (getType()) { + case Type::NULLPTR: + return false; + case Type::BOOL: + return asBool(); + case Type::INT: + return asInt() != 0; + case Type::DOUBLE: + return asDouble() != 0.0; + case Type::STRING: + return true; + case Type::ARRAY: + return true; + case Type::MAP: + return true; + } + return false; +} -Variant::TypeException::TypeException(Type actualType, Type requestedType) - : OusiaException(std::string("Variant: Requested \"") + - Variant::getTypeName(actualType) + - std::string("\" but is \"") + - Variant::getTypeName(requestedType) + std::string("\"")), - actualType(actualType), - requestedType(requestedType) +Variant::intType Variant::toInt() const { + switch (getType()) { + case Type::NULLPTR: + return 0; + case Type::BOOL: + return asBool() ? 1 : 0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0; // TODO: Parse string as int + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toInt() : 0; + } + case Type::MAP: + return 0; + } + return false; } + +Variant::doubleType Variant::toDouble() const +{ + switch (getType()) { + case Type::NULLPTR: + return 0.0; + case Type::BOOL: + return asBool() ? 1.0 : 0.0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0.0; // TODO: Parse string as double + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toDouble() : 0; + } + case Type::MAP: + return 0; + } + return false; +} + +Variant::stringType Variant::toString(bool escape) const +{ + switch (getType()) { + case Type::NULLPTR: + return "null"; + case Type::BOOL: + return asBool() ? "true" : "false"; + case Type::INT: + return std::to_string(asInt()); + case Type::DOUBLE: + return std::to_string(asDouble()); + case Type::STRING: { + // TODO: Use proper serialization function + std::stringstream ss; + ss << "\"" << asString() << "\""; + return ss.str(); + } + case Type::ARRAY: + return Utils::join(asArray(), ", ", "[", "]"); + case Type::MAP: + return Utils::join(asMap(), ", ", "{", "}"); + } + return ""; +} + } diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index f438d3e..d65e14a 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -20,8 +20,8 @@ * @file Variant.hpp * * The Variant class is used to efficiently represent a variables of varying - * type. Variant instances are used to represent user data and to exchange - * information between the host application and the script clients. + * type. Variant instances are used to represent data given by the end user and + * to exchange information between the host application and the script clients. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ @@ -33,6 +33,7 @@ #include <map> #include <string> #include <vector> +#include <ostream> // TODO: Use // http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html @@ -260,6 +261,11 @@ public: ~Variant() { destroy(); } /** + * Constructor for null values. Initializes the variant as null value. + */ + Variant(std::nullptr_t) : ptrVal(nullptr) { setNull(); } + + /** * Constructor for boolean values. * * @param b boolean value. @@ -294,10 +300,7 @@ public: * * @param a is a reference to the array */ - Variant(arrayType a) : ptrVal(nullptr) - { - setArray(std::move(a)); - } + Variant(arrayType a) : ptrVal(nullptr) { setArray(std::move(a)); } /** * Constructor for map values. The given map is copied and managed by the @@ -305,10 +308,7 @@ public: * * @param m is a reference to the map. */ - Variant(mapType m) : ptrVal(nullptr) - { - setMap(std::move(m)); - } + Variant(mapType m) : ptrVal(nullptr) { setMap(std::move(m)); } /** * Copy assignment operator. @@ -528,6 +528,36 @@ public: mapType &asMap() { return asObj<mapType>(Type::MAP); } /** + * Returns the value of the Variant as boolean, performs type conversion. + * + * @return the Variant value converted to a boolean value. + */ + boolType toBool() const; + + /** + * Returns the value of the Variant as integer, performs type conversion. + * + * @return the Variant value converted to an integer value. + */ + intType toInt() const; + + /** + * Returns the value of the Variant as double, performs type conversion. + * + * @return the Variant value converted to a double value. + */ + doubleType toDouble() const; + + /** + * Returns the value of the Variant as string, performs type conversion. + * + * @return the value of the variant as string. + * @param escape if set to true, adds double quotes to strings and escapes + * them properly (resulting in a more or less JSONesque output). + */ + stringType toString(bool escape = false) const; + + /** * Sets the variant to null. */ void setNull() @@ -637,7 +667,26 @@ public: * Returns the name of the type of this variant instance. */ const char *getTypeName() { return Variant::getTypeName(getType()); } + + /** + * Prints the Variant to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, const Variant &v) + { + return os << v.toString(true); + } + + /** + * Prints a key value pair to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, + const mapType::value_type &v) + { + // TODO: Use proper serialization function + return os << "\"" << v.first << "\": " << v.second.toString(true); + } }; + } #endif /* _OUSIA_VARIANT_HPP_ */ |