From d923aa73c3c76a0f1943a8cf4ed96197e7d20c77 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Mon, 28 Sep 2015 15:38:30 +0200 Subject: use SFINAE for integer CRSMConfigValue-specialization instead of hard- coded int and throw an exception when the value is out of range. --- src/CRSMConfig.hpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/CRSMConfig.hpp b/src/CRSMConfig.hpp index b875d6f..b705c62 100644 --- a/src/CRSMConfig.hpp +++ b/src/CRSMConfig.hpp @@ -6,6 +6,8 @@ #include #include +#include +#include using CRSMConfigException = std::logic_error; @@ -36,7 +38,7 @@ public: static QString getStringValue(Type val); }; -template +template class CRSMConfigValue : public CRSMConfigValueBase { public: // CRSMConfigValue(Type&) { } @@ -66,19 +68,20 @@ public: virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::String; } }; -template<> -class CRSMConfigValue : public CRSMConfigValueBase { - Integer& config; +template +class CRSMConfigValue::value>::type> : public CRSMConfigValueBase { + Type& config; public: - CRSMConfigValue(Integer& config) : config(config) { } + CRSMConfigValue(Type& config) : config(config) { } virtual void setValue(const QString& value) { bool ok = true; - Integer val = value.toInt(&ok, 0); + long long longLongVal = value.toLongLong(&ok, 0); if(!ok) throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer"); - config = val; + if(longLongVal < std::numeric_limits::min() || longLongVal > std::numeric_limits::max()) throw CRSMConfigException((QString::number(longLongVal) + " is out of range (" + QString::number(std::numeric_limits::min()) + " - " + QString::number(std::numeric_limits::max()) + ")").toStdString()); + config = static_cast(longLongVal); } virtual QString value() { return QString::number(config); } virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Integer; } -- cgit v1.2.3-54-g00ecf