summaryrefslogtreecommitdiffstats
path: root/src/CRSMConfig.hpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2015-09-28 15:38:30 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2015-09-28 15:39:19 +0200
commitd923aa73c3c76a0f1943a8cf4ed96197e7d20c77 (patch)
tree8abc730ce4d564278c3a314a0538061f1ff90b28 /src/CRSMConfig.hpp
parent50d9907af2522a9a6e02a171b9a26938a743971c (diff)
downloadmanager-d923aa73c3c76a0f1943a8cf4ed96197e7d20c77.tar.gz
manager-d923aa73c3c76a0f1943a8cf4ed96197e7d20c77.zip
use SFINAE for integer CRSMConfigValue-specialization instead of hard-
coded int and throw an exception when the value is out of range.
Diffstat (limited to 'src/CRSMConfig.hpp')
-rw-r--r--src/CRSMConfig.hpp17
1 files 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 <QMap>
#include <exception>
+#include <type_traits>
+#include <limits>
using CRSMConfigException = std::logic_error;
@@ -36,7 +38,7 @@ public:
static QString getStringValue(Type val);
};
-template<typename Type>
+template<typename Type, typename = void>
class CRSMConfigValue : public CRSMConfigValueBase {
public:
// CRSMConfigValue(Type&) { }
@@ -66,19 +68,20 @@ public:
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::String; }
};
-template<>
-class CRSMConfigValue<Integer> : public CRSMConfigValueBase {
- Integer& config;
+template<typename Type>
+class CRSMConfigValue<Type, typename std::enable_if<std::is_integral<Type>::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<Type>::min() || longLongVal > std::numeric_limits<Type>::max()) throw CRSMConfigException((QString::number(longLongVal) + " is out of range (" + QString::number(std::numeric_limits<Type>::min()) + " - " + QString::number(std::numeric_limits<Type>::max()) + ")").toStdString());
+ config = static_cast<Type>(longLongVal);
}
virtual QString value() { return QString::number(config); }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Integer; }