From b5e22002ef491cdbae8ab04e96200f101ca6312b Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Sat, 10 Oct 2015 20:17:56 +0200 Subject: Split CRSMConfig and CRSMConfigBase into separate files --- src/CRSMConfig.cpp | 202 ---------------------------------- src/CRSMConfig.hpp | 286 ++---------------------------------------------- src/ConfigBase.cpp | 195 +++++++++++++++++++++++++++++++++ src/ConfigBase.hpp | 283 +++++++++++++++++++++++++++++++++++++++++++++++ src/CrServerManager.pro | 5 +- 5 files changed, 488 insertions(+), 483 deletions(-) delete mode 100644 src/CRSMConfig.cpp create mode 100644 src/ConfigBase.cpp create mode 100644 src/ConfigBase.hpp diff --git a/src/CRSMConfig.cpp b/src/CRSMConfig.cpp deleted file mode 100644 index 5ec0f9b..0000000 --- a/src/CRSMConfig.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include "CRSMConfig.hpp" - -#include - -void CRSMConfigBase::addConfigValue(QString name, CRSMConfigValueBase* value) -{ - configValues[name] = value; -} - -void CRSMConfigBase::setConfigValue(const QString &name, const QString &value) -{ - CRSMConfigValueBase& configValue = getConfigValue(name); - configValue.setValue(value); -} - -QString CRSMConfigBase::getConfigValueLine(const QString &name) -{ - if(configValues.contains(name)) - { - CRSMConfigValueBase& value = *configValues[name]; - switch(value.type()) - { - case CRSMConfigValueType::Map: - { - QString ret; - const QMap& values = (dynamic_cast(&value))->getValues(); - foreach(const QString& mapKey, values.keys()) - { - ret += name + "[" + mapKey + "]" + " = " + values.value(mapKey) + "\n"; - } - return ret; - break; - } - case CRSMConfigValueType::List: - { - QString ret; - foreach(const QString& val, (dynamic_cast(&value))->getValues()) - { - ret += name + " += " + val + "\n"; - } - return ret; - break; - } - default: - return name + " = " + value.value() + "\n"; - } - } - else - { - throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\""); - } -} - -void CRSMConfigBase::addConfigListEntry(const QString &name, const QString &value) -{ - CRSMConfigValueBase& configValue = getConfigValue(name); - if(configValue.type() != CRSMConfigValueType::List) - { - throw CRSMConfigException("Cannot add a list entry to a non-list-config value \"" + name.toStdString() + "\""); - } - ((CRSMConfigValueList*)&configValue)->append(value); -} - -void CRSMConfigBase::removeConfigMapListEntry(const QString &name, const QString &value) -{ - CRSMConfigValueBase& configValue = getConfigValue(name); - if(configValue.type() == CRSMConfigValueType::List) - { - ((CRSMConfigValueList*)&configValue)->remove(value); - } - else if(configValue.type() == CRSMConfigValueType::Map) - { - ((CRSMConfigValueMap*)&configValue)->remove(value); - } - else - { - throw CRSMConfigException("Cannot remove a list entry from a value which is neither a list nor a map \"" + name.toStdString() + "\""); - } -} - -void CRSMConfigBase::setConfigMapValue(const QString &name, const QString &key, const QString &value) -{ - CRSMConfigValueBase& configValue = getConfigValue(name); - if(configValue.type() != CRSMConfigValueType::Map) - { - throw CRSMConfigException("Cannot set a map value on a non-map-config value \"" + name.toStdString() + "\""); - } - ((CRSMConfigValueMap*)&configValue)->setKeyValue(key, value); -} - -CRSMConfigValueBase& CRSMConfigBase::getConfigValue(const QString& name) -{ - if(!configValues.contains(name)) - { - throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\""); - } - else - { - return *configValues[name]; - } -} - -QString CRSMConfigBase::read(const QString &fileName, bool writeDefault) -{ - QString ret = ""; - - QFile config(fileName); - if(!config.exists()) - { - if(writeDefault) - { - if(write(fileName)) - { - return fileName + ": The config-file did not exist, a new one with default values has been created.\n"; - } - else - { - return fileName + ": The config-file did not exist, a new one could not be created.\n"; - } - } - else - { - return ret; - } - } - else if(config.open(QIODevice::ReadOnly | QIODevice::Text)) - { - QString line; - for(size_t lineNr = 1; !config.atEnd(); ++lineNr) - { - try - { - line = config.readLine().trimmed(); - setConfigLine(line); - } - catch(CRSMConfigException e) - { - ret += fileName + ":" + QString::number(lineNr) + ": " + e.what() + "\n"; - } - } - } - else - { - return fileName + ": The config-file could not be read.\n"; - } - - return ret; -} - -bool CRSMConfigBase::write(const QString &fileName) -{ - QFile config(fileName); - if(config.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) - { - foreach(const QString& key, configValues.keys()) - { - config.write(getConfigValueLine(key).toUtf8()); - } - - return true; - } - else - { - return false; - } -} - -void CRSMConfigBase::setConfigLine(const QString &line) -{ - static QRegExp valueExp(R"(^([^=]+)=(.*)$)"); - static QRegExp listExp(R"(^([^=]+)\+=(.*)$)"); - static QRegExp listMapRemoveExp(R"(^([^=]+)\-=(.*)$)"); - static QRegExp mapExp(R"(^([^\[]+)\[([^\]]+)\]\s*=\s*(.*)$)"); - - if(listExp.exactMatch(line)) - { - addConfigListEntry(listExp.cap(1).trimmed(), listExp.cap(2).trimmed()); - } - else if(listMapRemoveExp.exactMatch(line)) - { - removeConfigMapListEntry(listMapRemoveExp.cap(1).trimmed(), listMapRemoveExp.cap(2).trimmed()); - } - else if(mapExp.exactMatch(line)) - { - setConfigMapValue(mapExp.cap(1).trimmed(), mapExp.cap(2).trimmed(), mapExp.cap(3).trimmed()); - } - else if(valueExp.exactMatch(line)) - { - setConfigValue(valueExp.cap(1).trimmed(), valueExp.cap(2).trimmed()); - } - else - { - throw CRSMConfigException("Syntax error: unkown syntax"); - } -} - -void CRSMConfig::clear() -{ - auto configVals = configValues; - *this = CRSMConfig(); - configValues = configVals; -} diff --git a/src/CRSMConfig.hpp b/src/CRSMConfig.hpp index 7cdd501..2b75dad 100644 --- a/src/CRSMConfig.hpp +++ b/src/CRSMConfig.hpp @@ -1,283 +1,6 @@ #pragma once -#include -#include -#include -#include - -#include -#include -#include - -using CRSMConfigException = std::logic_error; - -namespace CRSMConfigValueType { -enum Type { - None, - String, - Integer, - Boolean, - List, - Map -}; -} - -class CRSMConfigValueBase { -public: - - virtual void setValue(const QString&) { } - virtual QString value() { return ""; } - virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::None; } - - - - template - static Type getValue(const QString& value); - - template - static QString getStringValue(Type val); -}; - -template -class CRSMConfigValue : public CRSMConfigValueBase { -public: -// CRSMConfigValue(Type&) { } - - virtual void setValue(const QString&) { throw CRSMConfigException("Cannot assign"); } - virtual QString value() { throw CRSMConfigException("Cannot get the value"); } -}; - -using String = QString; -using Integer = int; -using Port = ushort; -using Boolean = bool; -#define List(Type) QList -#define Map(KeyType, ValueType) QMap - - -#define ConfigValue(Value) {#Value, mkConfigValue(Value)} -#define ConfigValueEx(Value, ...) {#Value, mkConfigValue(Value, __VA_ARGS__)} - -template<> -class CRSMConfigValue : public CRSMConfigValueBase { - String& config; - bool trimmedQuotes = false; - -public: - CRSMConfigValue(String& config) : config(config) { } - - virtual void setValue(const QString& value) { config = trimQuotes(QString(value), trimmedQuotes); } - virtual QString value() - { - if(trimmedQuotes) - { - return "\"" + config + "\""; - } - else - { - return config; - } - } - virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::String; } - -private: - QString& trimQuotes(QString&& string, bool& trimmed) - { - trimmed = false; - if(string.length() >= 2 && string.at(0) == '"' && string.at(string.length() - 1) == '"') - { - string.remove(0, 1); - string.remove(string.length() - 1, 1); - trimmed = true; - } - return string; - } - -}; - -template -class CRSMConfigValue::value>::type> : public CRSMConfigValueBase { - Type& config; - -public: - CRSMConfigValue(Type& config) : config(config) { } - - virtual void setValue(const QString& value) - { - bool ok = true; - long long longLongVal = value.toLongLong(&ok, 0); - if(!ok) throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer"); - 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; } -}; - -template<> -class CRSMConfigValue : public CRSMConfigValueBase { - Boolean& config; - -public: - CRSMConfigValue(Boolean& config) : config(config) { } - - virtual void setValue(const QString& value) - { - if(value == "true") - { - config = true; - } - else if(value == "false") - { - config = false; - } - else - { - throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as a Boolean"); - } - } - virtual QString value() { return config ? "true" : "false"; } - virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Boolean; } -}; - -class CRSMConfigValueList : public CRSMConfigValueBase { -public: - virtual void append(const QString& entry) = 0; - virtual void remove(const QString& entry) = 0; - virtual QStringList getValues() = 0; -}; - -class CRSMConfigValueMap : public CRSMConfigValueBase { -public: - virtual void setKeyValue(const QString& key, const QString& value) = 0; - virtual void remove(const QString& key) = 0; - virtual QMap getValues() = 0; -}; - -template -class CRSMConfigValue> : public CRSMConfigValueList { - using ListType = QList; - - ListType& config; - const bool deduplicate; - -public: - CRSMConfigValue(ListType& config, bool deduplicate = true) : config(config), deduplicate(deduplicate) {} - - virtual ListType& list() { return config; } - virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::List; } - - virtual void append(const QString &entry) - { - Type val = CRSMConfigValueBase::getValue(entry); - if(!deduplicate || !config.contains(val)) - { - config.append(val); - } - } - - virtual void remove(const QString &entry) { config.removeAll(CRSMConfigValueBase::getValue(entry)); } - - virtual QStringList getValues() - { - QStringList ret; - - foreach(Type val, config) - { - ret.append(CRSMConfigValueBase::getStringValue(val)); - } - - return ret; - } -}; - -template -class CRSMConfigValue> : public CRSMConfigValueMap { - using MapType = QMap; - - MapType& config; - -public: - CRSMConfigValue(MapType& config) : config(config) {} - - virtual MapType& map() { return config; } - virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Map; } - - virtual void setKeyValue(const QString& key, const QString& value) - { - config[CRSMConfigValueBase::getValue(key)] = CRSMConfigValueBase::getValue(value); - } - - virtual QMap getValues() - { - QMap ret; - - foreach(KeyType key, config.keys()) - { - ret[CRSMConfigValueBase::getStringValue(key)] = CRSMConfigValueBase::getStringValue(config[key]); - } - - return ret; - } - - virtual void remove(const QString &key) - { - config.remove(CRSMConfigValueBase::getValue(key)); - } -}; - -template -class CRSMConfigValue::value>::type> : public CRSMConfigValue::type> { - using UndType = typename std::underlying_type::type; -public: - CRSMConfigValue(Type& config) : CRSMConfigValue::CRSMConfigValue((UndType&)config) {} -}; - -template -CRSMConfigValue* mkConfigValue(Type& Value, Types... values) -{ - return new CRSMConfigValue(Value, values...); -} - -template -Type CRSMConfigValueBase::getValue(const QString& value) -{ - Type ret; - CRSMConfigValue val(ret); - val.setValue(value); - return ret; -} - -template -QString CRSMConfigValueBase::getStringValue(Type value) -{ - CRSMConfigValue val(value); - return val.value(); -} - -class CRSMConfigBase -{ -public: - explicit CRSMConfigBase(QMap configValues) : configValues(configValues) {} - - void setConfigValue(const QString& name, const QString& value); - QString getConfigValueLine(const QString& name); - void addConfigListEntry(const QString& name, const QString& value); - void removeConfigMapListEntry(const QString &name, const QString &value); - void setConfigMapValue(const QString& name, const QString& key, const QString& value); - - QString read(const QString& fileName, bool writeDefault = true); - bool write(const QString& fileName); - - void setConfigLine(const QString& line); - -protected: - void addConfigValue(QString name, CRSMConfigValueBase *value); - CRSMConfigValueBase& getConfigValue(const QString &name); - - QMap configValues; -}; - +#include "ConfigBase.hpp" class CRSMConfig : public CRSMConfigBase { public: @@ -415,5 +138,10 @@ public: ConfigValue(Readline.RereadLimit) }) {} - void clear(); + void clear() + { + auto configVals = configValues; + *this = CRSMConfig(); + configValues = configVals; + } }; diff --git a/src/ConfigBase.cpp b/src/ConfigBase.cpp new file mode 100644 index 0000000..ea1f874 --- /dev/null +++ b/src/ConfigBase.cpp @@ -0,0 +1,195 @@ +#include "CRSMConfig.hpp" + +#include + +void CRSMConfigBase::addConfigValue(QString name, CRSMConfigValueBase* value) +{ + configValues[name] = value; +} + +void CRSMConfigBase::setConfigValue(const QString &name, const QString &value) +{ + CRSMConfigValueBase& configValue = getConfigValue(name); + configValue.setValue(value); +} + +QString CRSMConfigBase::getConfigValueLine(const QString &name) +{ + if(configValues.contains(name)) + { + CRSMConfigValueBase& value = *configValues[name]; + switch(value.type()) + { + case CRSMConfigValueType::Map: + { + QString ret; + const QMap& values = (dynamic_cast(&value))->getValues(); + foreach(const QString& mapKey, values.keys()) + { + ret += name + "[" + mapKey + "]" + " = " + values.value(mapKey) + "\n"; + } + return ret; + break; + } + case CRSMConfigValueType::List: + { + QString ret; + foreach(const QString& val, (dynamic_cast(&value))->getValues()) + { + ret += name + " += " + val + "\n"; + } + return ret; + break; + } + default: + return name + " = " + value.value() + "\n"; + } + } + else + { + throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\""); + } +} + +void CRSMConfigBase::addConfigListEntry(const QString &name, const QString &value) +{ + CRSMConfigValueBase& configValue = getConfigValue(name); + if(configValue.type() != CRSMConfigValueType::List) + { + throw CRSMConfigException("Cannot add a list entry to a non-list-config value \"" + name.toStdString() + "\""); + } + ((CRSMConfigValueList*)&configValue)->append(value); +} + +void CRSMConfigBase::removeConfigMapListEntry(const QString &name, const QString &value) +{ + CRSMConfigValueBase& configValue = getConfigValue(name); + if(configValue.type() == CRSMConfigValueType::List) + { + ((CRSMConfigValueList*)&configValue)->remove(value); + } + else if(configValue.type() == CRSMConfigValueType::Map) + { + ((CRSMConfigValueMap*)&configValue)->remove(value); + } + else + { + throw CRSMConfigException("Cannot remove a list entry from a value which is neither a list nor a map \"" + name.toStdString() + "\""); + } +} + +void CRSMConfigBase::setConfigMapValue(const QString &name, const QString &key, const QString &value) +{ + CRSMConfigValueBase& configValue = getConfigValue(name); + if(configValue.type() != CRSMConfigValueType::Map) + { + throw CRSMConfigException("Cannot set a map value on a non-map-config value \"" + name.toStdString() + "\""); + } + ((CRSMConfigValueMap*)&configValue)->setKeyValue(key, value); +} + +CRSMConfigValueBase& CRSMConfigBase::getConfigValue(const QString& name) +{ + if(!configValues.contains(name)) + { + throw CRSMConfigException("Unknown config value \"" + name.toStdString() + "\""); + } + else + { + return *configValues[name]; + } +} + +QString CRSMConfigBase::read(const QString &fileName, bool writeDefault) +{ + QString ret = ""; + + QFile config(fileName); + if(!config.exists()) + { + if(writeDefault) + { + if(write(fileName)) + { + return fileName + ": The config-file did not exist, a new one with default values has been created.\n"; + } + else + { + return fileName + ": The config-file did not exist, a new one could not be created.\n"; + } + } + else + { + return ret; + } + } + else if(config.open(QIODevice::ReadOnly | QIODevice::Text)) + { + QString line; + for(size_t lineNr = 1; !config.atEnd(); ++lineNr) + { + try + { + line = config.readLine().trimmed(); + setConfigLine(line); + } + catch(CRSMConfigException e) + { + ret += fileName + ":" + QString::number(lineNr) + ": " + e.what() + "\n"; + } + } + } + else + { + return fileName + ": The config-file could not be read.\n"; + } + + return ret; +} + +bool CRSMConfigBase::write(const QString &fileName) +{ + QFile config(fileName); + if(config.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) + { + foreach(const QString& key, configValues.keys()) + { + config.write(getConfigValueLine(key).toUtf8()); + } + + return true; + } + else + { + return false; + } +} + +void CRSMConfigBase::setConfigLine(const QString &line) +{ + static QRegExp valueExp(R"(^([^=]+)=(.*)$)"); + static QRegExp listExp(R"(^([^=]+)\+=(.*)$)"); + static QRegExp listMapRemoveExp(R"(^([^=]+)\-=(.*)$)"); + static QRegExp mapExp(R"(^([^\[]+)\[([^\]]+)\]\s*=\s*(.*)$)"); + + if(listExp.exactMatch(line)) + { + addConfigListEntry(listExp.cap(1).trimmed(), listExp.cap(2).trimmed()); + } + else if(listMapRemoveExp.exactMatch(line)) + { + removeConfigMapListEntry(listMapRemoveExp.cap(1).trimmed(), listMapRemoveExp.cap(2).trimmed()); + } + else if(mapExp.exactMatch(line)) + { + setConfigMapValue(mapExp.cap(1).trimmed(), mapExp.cap(2).trimmed(), mapExp.cap(3).trimmed()); + } + else if(valueExp.exactMatch(line)) + { + setConfigValue(valueExp.cap(1).trimmed(), valueExp.cap(2).trimmed()); + } + else + { + throw CRSMConfigException("Syntax error: unkown syntax"); + } +} diff --git a/src/ConfigBase.hpp b/src/ConfigBase.hpp new file mode 100644 index 0000000..5e2a50f --- /dev/null +++ b/src/ConfigBase.hpp @@ -0,0 +1,283 @@ +#ifndef CONFIGBASE +#define CONFIGBASE + +#include +#include +#include +#include + +#include +#include +#include + +using CRSMConfigException = std::logic_error; + +namespace CRSMConfigValueType { +enum Type { + None, + String, + Integer, + Boolean, + List, + Map +}; +} + +class CRSMConfigValueBase { +public: + + virtual void setValue(const QString&) { } + virtual QString value() { return ""; } + virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::None; } + + + + template + static Type getValue(const QString& value); + + template + static QString getStringValue(Type val); +}; + +template +class CRSMConfigValue : public CRSMConfigValueBase { +public: +// CRSMConfigValue(Type&) { } + + virtual void setValue(const QString&) { throw CRSMConfigException("Cannot assign"); } + virtual QString value() { throw CRSMConfigException("Cannot get the value"); } +}; + +using String = QString; +using Integer = int; +using Port = ushort; +using Boolean = bool; +#define List(Type) QList +#define Map(KeyType, ValueType) QMap + + +#define ConfigValue(Value) {#Value, mkConfigValue(Value)} +#define ConfigValueEx(Value, ...) {#Value, mkConfigValue(Value, __VA_ARGS__)} + +template<> +class CRSMConfigValue : public CRSMConfigValueBase { + String& config; + bool trimmedQuotes = false; + +public: + CRSMConfigValue(String& config) : config(config) { } + + virtual void setValue(const QString& value) { config = trimQuotes(QString(value), trimmedQuotes); } + virtual QString value() + { + if(trimmedQuotes) + { + return "\"" + config + "\""; + } + else + { + return config; + } + } + virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::String; } + +private: + QString& trimQuotes(QString&& string, bool& trimmed) + { + trimmed = false; + if(string.length() >= 2 && string.at(0) == '"' && string.at(string.length() - 1) == '"') + { + string.remove(0, 1); + string.remove(string.length() - 1, 1); + trimmed = true; + } + return string; + } + +}; + +template +class CRSMConfigValue::value>::type> : public CRSMConfigValueBase { + Type& config; + +public: + CRSMConfigValue(Type& config) : config(config) { } + + virtual void setValue(const QString& value) + { + bool ok = true; + long long longLongVal = value.toLongLong(&ok, 0); + if(!ok) throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer"); + 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; } +}; + +template<> +class CRSMConfigValue : public CRSMConfigValueBase { + Boolean& config; + +public: + CRSMConfigValue(Boolean& config) : config(config) { } + + virtual void setValue(const QString& value) + { + if(value == "true") + { + config = true; + } + else if(value == "false") + { + config = false; + } + else + { + throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as a Boolean"); + } + } + virtual QString value() { return config ? "true" : "false"; } + virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Boolean; } +}; + +class CRSMConfigValueList : public CRSMConfigValueBase { +public: + virtual void append(const QString& entry) = 0; + virtual void remove(const QString& entry) = 0; + virtual QStringList getValues() = 0; +}; + +class CRSMConfigValueMap : public CRSMConfigValueBase { +public: + virtual void setKeyValue(const QString& key, const QString& value) = 0; + virtual void remove(const QString& key) = 0; + virtual QMap getValues() = 0; +}; + +template +class CRSMConfigValue> : public CRSMConfigValueList { + using ListType = QList; + + ListType& config; + const bool deduplicate; + +public: + CRSMConfigValue(ListType& config, bool deduplicate = true) : config(config), deduplicate(deduplicate) {} + + virtual ListType& list() { return config; } + virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::List; } + + virtual void append(const QString &entry) + { + Type val = CRSMConfigValueBase::getValue(entry); + if(!deduplicate || !config.contains(val)) + { + config.append(val); + } + } + + virtual void remove(const QString &entry) { config.removeAll(CRSMConfigValueBase::getValue(entry)); } + + virtual QStringList getValues() + { + QStringList ret; + + foreach(Type val, config) + { + ret.append(CRSMConfigValueBase::getStringValue(val)); + } + + return ret; + } +}; + +template +class CRSMConfigValue> : public CRSMConfigValueMap { + using MapType = QMap; + + MapType& config; + +public: + CRSMConfigValue(MapType& config) : config(config) {} + + virtual MapType& map() { return config; } + virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Map; } + + virtual void setKeyValue(const QString& key, const QString& value) + { + config[CRSMConfigValueBase::getValue(key)] = CRSMConfigValueBase::getValue(value); + } + + virtual QMap getValues() + { + QMap ret; + + foreach(KeyType key, config.keys()) + { + ret[CRSMConfigValueBase::getStringValue(key)] = CRSMConfigValueBase::getStringValue(config[key]); + } + + return ret; + } + + virtual void remove(const QString &key) + { + config.remove(CRSMConfigValueBase::getValue(key)); + } +}; + +template +class CRSMConfigValue::value>::type> : public CRSMConfigValue::type> { + using UndType = typename std::underlying_type::type; +public: + CRSMConfigValue(Type& config) : CRSMConfigValue::CRSMConfigValue((UndType&)config) {} +}; + +template +CRSMConfigValue* mkConfigValue(Type& Value, Types... values) +{ + return new CRSMConfigValue(Value, values...); +} + +template +Type CRSMConfigValueBase::getValue(const QString& value) +{ + Type ret; + CRSMConfigValue val(ret); + val.setValue(value); + return ret; +} + +template +QString CRSMConfigValueBase::getStringValue(Type value) +{ + CRSMConfigValue val(value); + return val.value(); +} + +class CRSMConfigBase +{ +public: + explicit CRSMConfigBase(QMap configValues) : configValues(configValues) {} + + void setConfigValue(const QString& name, const QString& value); + QString getConfigValueLine(const QString& name); + void addConfigListEntry(const QString& name, const QString& value); + void removeConfigMapListEntry(const QString &name, const QString &value); + void setConfigMapValue(const QString& name, const QString& key, const QString& value); + + QString read(const QString& fileName, bool writeDefault = true); + bool write(const QString& fileName); + + void setConfigLine(const QString& line); + +protected: + void addConfigValue(QString name, CRSMConfigValueBase *value); + CRSMConfigValueBase& getConfigValue(const QString &name); + + QMap configValues; +}; + +#endif // CONFIGBASE + diff --git a/src/CrServerManager.pro b/src/CrServerManager.pro index eab24e6..4587c1a 100644 --- a/src/CrServerManager.pro +++ b/src/CrServerManager.pro @@ -20,14 +20,15 @@ TEMPLATE = app SOURCES += main.cpp \ crsm.cpp \ ProcessManager.cpp \ - CRSMConfig.cpp + ConfigBase.cpp HEADERS += \ CmdFunctionRef.hpp \ ClientInfo.hpp \ crsm.hpp \ ProcessManager.hpp \ - CRSMConfig.hpp + CRSMConfig.hpp \ + ConfigBase.hpp equals(QT_ARCH, "x86_64"):linux-*: DEFINES += Q_OS_LINUX64 QMAKE_CXXFLAGS *= -std=c++11 -Wall -Wextra -Werror -Wunused -- cgit v1.2.3-54-g00ecf