summaryrefslogtreecommitdiff
path: root/src/core/variant/Variant.cpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-12-03 00:03:01 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-12-03 00:03:01 +0100
commit2ca83f15d5ca81ce8b45fd99d959aee49a6f2eea (patch)
tree36b6a928974f45299268b9cda8a1879df31d51e3 /src/core/variant/Variant.cpp
parentb143fe4e0df319a88df9cba22c5dd707000810d4 (diff)
added type conversion functions and creation from nullptr
Diffstat (limited to 'src/core/variant/Variant.cpp')
-rw-r--r--src/core/variant/Variant.cpp115
1 files changed, 106 insertions, 9 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 "";
+}
+
}