added type security to settings

This commit is contained in:
Dorian Zedler 2020-07-24 13:07:27 +02:00
parent 10b2f7b888
commit 615ac508d6
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
2 changed files with 26 additions and 4 deletions

View file

@ -37,6 +37,7 @@ public:
explicit ScStwSettings(QObject *parent = nullptr);
typedef QString(*keyToStringConverter)(int);
typedef QVariant::Type(*keyToTypeConverter)(int);
/*!
* \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client
@ -79,11 +80,26 @@ public:
return QMetaEnum::fromType<BaseStationSetting>().valueToKey(key);
}
static QVariant::Type keyToType(int key) {
QMap<BaseStationSetting, QVariant::Type> types = {
{ReadySoundEnableSetting, QVariant::Bool},
{ReadySoundDelaySetting, QVariant::Double},
{AtYourMarksSoundEnableSetting, QVariant::Bool},
{AtYourMarksSoundDelaySetting, QVariant::Double},
{SoundVolumeSetting, QVariant::Double}
};
if(types.contains(BaseStationSetting(key)))
return types[BaseStationSetting(key)];
return QVariant::Invalid;
}
protected:
virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1);
virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1);
virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1);
bool registerKeyLevelHandler(int keyLevel, keyToStringConverter);
bool registerKeyLevelConverters(int keyLevel, keyToStringConverter, keyToTypeConverter);
private:
QFile * settingsFile;
@ -113,6 +129,7 @@ private:
};
QMap<int, keyToStringConverter> keyToStringConverters;
QMap<int, keyToTypeConverter> keyToTypeConverters;
QMap<int, QMap<int, ScStwSettingInternal*>> internalSettingHandlers;
private slots:

View file

@ -35,7 +35,7 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile);
this->registerKeyLevelHandler(ScStwSettings::KeyLevel, &ScStwSettings::keyToString);
this->registerKeyLevelConverters(ScStwSettings::KeyLevel, &ScStwSettings::keyToString, &ScStwSettings::keyToType);
}
QVariant ScStwSettings::readSetting(BaseStationSetting key) {
@ -78,6 +78,10 @@ QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) {
}
bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) {
if(!this->keyToTypeConverters.contains(keyInt) || !value.convert(this->keyToTypeConverters[keyLevel](keyInt)) || value.type() == QVariant::Invalid) {
return false;
}
if(!this->settingsCache.contains(key))
this->settingsCache.insert(key, value);
else if (this->settingsCache[key] == value)
@ -106,11 +110,12 @@ ScStwSetting * ScStwSettings::getSetting(int key, int keyLevel) {
return this->internalSettingHandlers[keyLevel][key];
}
bool ScStwSettings::registerKeyLevelHandler(int keyLevel, keyToStringConverter converterFunction) {
bool ScStwSettings::registerKeyLevelConverters(int keyLevel, keyToStringConverter keyToStringConverterFunction, keyToTypeConverter keyToTypeConverterFunction) {
if(this->keyToStringConverters.contains(keyLevel))
return false;
this->keyToStringConverters.insert(keyLevel, converterFunction);
this->keyToStringConverters.insert(keyLevel, keyToStringConverterFunction);
this->keyToTypeConverters.insert(keyLevel, keyToTypeConverterFunction);
return true;
}