added type security to settings
This commit is contained in:
parent
10b2f7b888
commit
615ac508d6
2 changed files with 26 additions and 4 deletions
|
@ -37,6 +37,7 @@ public:
|
||||||
explicit ScStwSettings(QObject *parent = nullptr);
|
explicit ScStwSettings(QObject *parent = nullptr);
|
||||||
|
|
||||||
typedef QString(*keyToStringConverter)(int);
|
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
|
* \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);
|
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:
|
protected:
|
||||||
virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1);
|
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 writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1);
|
||||||
virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,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:
|
private:
|
||||||
QFile * settingsFile;
|
QFile * settingsFile;
|
||||||
|
@ -113,6 +129,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
QMap<int, keyToStringConverter> keyToStringConverters;
|
QMap<int, keyToStringConverter> keyToStringConverters;
|
||||||
|
QMap<int, keyToTypeConverter> keyToTypeConverters;
|
||||||
QMap<int, QMap<int, ScStwSettingInternal*>> internalSettingHandlers;
|
QMap<int, QMap<int, ScStwSettingInternal*>> internalSettingHandlers;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
|
@ -35,7 +35,7 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
||||||
|
|
||||||
connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile);
|
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) {
|
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) {
|
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))
|
if(!this->settingsCache.contains(key))
|
||||||
this->settingsCache.insert(key, value);
|
this->settingsCache.insert(key, value);
|
||||||
else if (this->settingsCache[key] == value)
|
else if (this->settingsCache[key] == value)
|
||||||
|
@ -106,11 +110,12 @@ ScStwSetting * ScStwSettings::getSetting(int key, int keyLevel) {
|
||||||
return this->internalSettingHandlers[keyLevel][key];
|
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))
|
if(this->keyToStringConverters.contains(keyLevel))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
this->keyToStringConverters.insert(keyLevel, converterFunction);
|
this->keyToStringConverters.insert(keyLevel, keyToStringConverterFunction);
|
||||||
|
this->keyToTypeConverters.insert(keyLevel, keyToTypeConverterFunction);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue