diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2016-03-31 19:38:49 +0200 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2016-03-31 19:46:33 +0200 |
| commit | 135eeef23be2372bd1821d19d17bfcd187b2da05 (patch) | |
| tree | 1fb178138e8f39d6684abf6268ce3c6e5610790f /src/ConfigBase.hpp | |
| parent | bb5431ba4d88018275d459b16dfdc129ab133b45 (diff) | |
| download | manager-135eeef23be2372bd1821d19d17bfcd187b2da05.tar.gz manager-135eeef23be2372bd1821d19d17bfcd187b2da05.zip | |
Move ConfigBase and Util into a separate qt-config repo for reuse
Diffstat (limited to 'src/ConfigBase.hpp')
| -rw-r--r-- | src/ConfigBase.hpp | 330 |
1 files changed, 0 insertions, 330 deletions
diff --git a/src/ConfigBase.hpp b/src/ConfigBase.hpp deleted file mode 100644 index ade454a..0000000 --- a/src/ConfigBase.hpp +++ /dev/null @@ -1,330 +0,0 @@ -#ifndef CONFIGBASE -#define CONFIGBASE - -#include <QString> -#include <QStringList> -#include <QList> -#include <QMap> -#include <QRegularExpression> - -#include <exception> -#include <type_traits> -#include <limits> - -#include "Util.hpp" - -using ConfigException = std::logic_error; - -namespace ConfigValueType { -enum Type { - None, - String, - Integer, - Boolean, - List, - Map -}; -} - -class ConfigValueBase { -public: - - virtual void setValue(const QString&) { } - virtual QString value() { return ""; } - virtual ConfigValueType::Type type() { return ConfigValueType::Type::None; } - - - - template<typename Type> - static Type getValue(const QString& value); - - template<typename Type> - static QString getStringValue(Type val); -}; - -template<typename Type, typename = void> -class ConfigValue : public ConfigValueBase { -public: - virtual void setValue(const QString&) { throw ConfigException("Cannot assign"); } - virtual QString value() { throw ConfigException("Cannot get the value"); } -}; - -using String = QString; -using Integer = int; -using Port = ushort; -using Boolean = bool; -#define List(Type) QList<Type> -#define Map(KeyType, ValueType) QMap<KeyType, ValueType> - - -#define ConfigVal(Value) {#Value, mkConfigValue(Value)} -#define ConfigValEx(Value, ...) {#Value, mkConfigValue(Value, __VA_ARGS__)} - -template<> -class ConfigValue<String> : public ConfigValueBase { - String& config; - bool trimmedQuotes = false; - -public: - ConfigValue(String& config) : config(config) { } - - virtual void setValue(const QString& value) { config = Util::trimQuotes(value, trimmedQuotes); } - virtual QString value() - { - static QRegularExpression quoteExp(R"(^(\s.*|.*\s)$)"); - if(trimmedQuotes || quoteExp.match(config).hasMatch()) - { - return "\"" + config + "\""; - } - else - { - return config; - } - } - virtual ConfigValueType::Type type() { return ConfigValueType::Type::String; } -}; - -template<typename Type> -class ConfigValue<Type, typename std::enable_if<std::is_integral<Type>::value>::type> : public ConfigValueBase { - Type& config; - -public: - ConfigValue(Type& config) : config(config) { } - - virtual void setValue(const QString& value) - { - bool ok = true; - long long longLongVal = value.toLongLong(&ok, 0); - if(!ok) throw ConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer"); - if(longLongVal < std::numeric_limits<Type>::min() || longLongVal > std::numeric_limits<Type>::max()) throw ConfigException((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 ConfigValueType::Type type() { return ConfigValueType::Type::Integer; } -}; - -template<> -class ConfigValue<Boolean> : public ConfigValueBase { - Boolean& config; - -public: - ConfigValue(Boolean& config) : config(config) { } - - virtual void setValue(const QString& value) - { - if(value == "true") - { - config = true; - } - else if(value == "false") - { - config = false; - } - else - { - throw ConfigException("\"" + value.toStdString() + "\" could not be parsed as a Boolean"); - } - } - virtual QString value() { return config ? "true" : "false"; } - virtual ConfigValueType::Type type() { return ConfigValueType::Type::Boolean; } -}; - -class ConfigValueList : public ConfigValueBase { -public: - virtual void append(const QString& entry) = 0; - virtual void remove(const QString& entry) = 0; - virtual QStringList getValues() = 0; -}; - -class ConfigValueMap : public ConfigValueBase { -public: - virtual void setKeyValue(const QString& key, const QString& value) = 0; - virtual void remove(const QString& key) = 0; - virtual QMap<QString, QString> getValues() = 0; -}; - -template<typename Type> -class ConfigValue<QList<Type>> : public ConfigValueList { - using ListType = QList<Type>; - - ListType& config; - const bool deduplicate; - const QChar splitChar; - -public: - ConfigValue(ListType& config, bool deduplicate = true, const QChar splitChar = ';') : config(config), deduplicate(deduplicate), splitChar(splitChar) {} - - virtual ListType& list() { return config; } - virtual ConfigValueType::Type type() { return ConfigValueType::Type::List; } - - virtual void append(const QString &entry) - { - Type val = ConfigValueBase::getValue<Type>(entry); - if(!deduplicate || !config.contains(val)) - { - config.append(val); - } - } - - virtual void remove(const QString &entry) { config.removeAll(ConfigValueBase::getValue<Type>(entry)); } - - virtual QStringList getValues() - { - QStringList ret; - - foreach(Type val, config) - { - ret.append(ConfigValueBase::getStringValue(val)); - } - - return ret; - } - - void setValue(const QString& value) - { - config.clear(); - if(value.isEmpty()) - { - return; - } - QStringList parts = Util::splitEscaped(value, splitChar); - foreach(const QString& part, parts) - { - config.append(ConfigValueBase::getValue<Type>(part)); - } - } - - QString value() - { - QStringList parts; - foreach(const Type& part, config) - { - parts.append(ConfigValueBase::getStringValue(part)); - } - return Util::joinEscape(parts, splitChar); - } -}; - -template<typename KeyType, typename ValueType> -class ConfigValue<QMap<KeyType, ValueType>> : public ConfigValueMap { - using MapType = QMap<KeyType, ValueType>; - - MapType& config; - const QChar assignChar; - const QChar splitChar; - -public: - ConfigValue(MapType& config, const QChar assignChar = ':', const QChar splitChar = ';') : config(config), assignChar(assignChar), splitChar(splitChar) {} - - virtual MapType& map() { return config; } - virtual ConfigValueType::Type type() { return ConfigValueType::Type::Map; } - - virtual void setKeyValue(const QString& key, const QString& value) - { - config[ConfigValueBase::getValue<KeyType>(key)] = ConfigValueBase::getValue<ValueType>(value); - } - - virtual QMap<QString, QString> getValues() - { - QMap<QString, QString> ret; - - foreach(KeyType key, config.keys()) - { - ret[ConfigValueBase::getStringValue(key)] = ConfigValueBase::getStringValue(config[key]); - } - - return ret; - } - - virtual void remove(const QString &key) - { - config.remove(ConfigValueBase::getValue<KeyType>(key)); - } - - QString value() - { - QStringList ret; - foreach(const KeyType& key, config.keys()) - { - ret.append(Util::joinEscape({ConfigValueBase::getStringValue(key), ConfigValueBase::getStringValue(config.value(key))}, assignChar)); - } - return Util::joinEscape(ret, splitChar); - } - - void setValue(const QString& value) - { - config.clear(); - if(value.isEmpty()) - { - return; - } - QStringList parts = Util::splitEscaped(value, splitChar); - foreach(const QString& part, parts) - { - QStringList parts = Util::splitEscaped(part, assignChar); - if(parts.length() != 2) - { - throw ConfigException("Cannot parse corrupt map key-value-pair"); - } - config[ConfigValueBase::getValue<KeyType>(parts.first())] = ConfigValueBase::getValue<ValueType>(parts.at(1)); - } - } -}; - -template<typename Type> -class ConfigValue<Type, typename std::enable_if<std::is_enum<Type>::value>::type> : public ConfigValue<typename std::underlying_type<Type>::type> { - using UndType = typename std::underlying_type<Type>::type; -public: - ConfigValue(Type& config) : ConfigValue<UndType>::ConfigValue((UndType&)config) {} -}; - -template<typename Type, typename... Types> -ConfigValue<Type>* mkConfigValue(Type& Value, Types... values) -{ - return new ConfigValue<Type>(Value, values...); -} - -template<typename Type> -Type ConfigValueBase::getValue(const QString& value) -{ - Type ret; - ConfigValue<Type> val(ret); - val.setValue(value); - return ret; -} - -template<typename Type> -QString ConfigValueBase::getStringValue(Type value) -{ - ConfigValue<Type> val(value); - return val.value(); -} - -class ConfigBase -{ -public: - explicit ConfigBase(QMap<QString, ConfigValueBase*> 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(QString fileName = ""); - - void setConfigLine(const QString& line); - -protected: - void addConfigValue(QString name, ConfigValueBase *value); - ConfigValueBase& getConfigValue(const QString &name); - - QMap<QString, ConfigValueBase*> configValues; - -private: - QString curFileName = ""; -}; - -#endif // CONFIGBASE - |
