From 7950e05b381308a3beb3c6d1538de6af047e5c0c Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 11 Jan 2015 01:03:07 +0100 Subject: Refactored conversion routines used in the Typesystem class and the Variant class into an own class, implemented missing conversion from string to integer/double, implemented proper JSON serialization of variants --- src/core/model/Typesystem.cpp | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) (limited to 'src/core/model') diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index f082f39..117b50a 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -20,6 +20,7 @@ #include #include +#include namespace ousia { namespace model { @@ -38,57 +39,32 @@ bool Type::build(Variant &data, Logger &logger) const } } -/* Class StringType */ +/* Class BoolType */ -bool StringType::doBuild(Variant &data, Logger &logger) const +bool BoolType::doBuild(Variant &data, Logger &logger) const { - // Cannot convert non-primitive values to strings - if (!data.isPrimitive()) { - throw LoggableException{"Expected string or primitive input."}; - } - - // Perform an implicit type conversion - if (!data.isString()) { - // Convert the variant value to a string and set it - const char *oldName = data.getTypeName(); - data = data.toString().c_str(); - - // Log conversions as these may be potentially unwanted - logger.note(std::string("Implicit conversion from ") + oldName + - " to string."); - } - return true; + return VariantConverter::toBool(data, logger); } /* Class IntType */ bool IntType::doBuild(Variant &data, Logger &logger) const { - if (!data.isInt()) { - throw LoggableException{"Expected integer value."}; - } - return true; + return VariantConverter::toInt(data, logger); } /* Class DoubleType */ bool DoubleType::doBuild(Variant &data, Logger &logger) const { - if (!data.isInt() && !data.isDouble()) { - throw LoggableException{"Expected double value."}; - } - data = Variant{data.toDouble()}; - return true; + return VariantConverter::toDouble(data, logger); } -/* Class BoolType */ +/* Class StringType */ -bool BoolType::doBuild(Variant &data, Logger &logger) const +bool StringType::doBuild(Variant &data, Logger &logger) const { - if (!data.isBool()) { - throw LoggableException("Expected boolean value!"); - } - return true; + return VariantConverter::toString(data, logger); } /* Class EnumType */ -- cgit v1.2.3