From 3ef990acb99b954beda6614815022b7f017c2b4b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 24 Jan 2018 17:55:24 +0100 Subject: Configuration class rework - Moved setValue to the header - Code readability - Removed secondary defaultCfg --- lib/settings/configuration.h | 97 +++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 32 deletions(-) (limited to 'lib/settings/configuration.h') diff --git a/lib/settings/configuration.h b/lib/settings/configuration.h index db93760..82e0c58 100644 --- a/lib/settings/configuration.h +++ b/lib/settings/configuration.h @@ -13,6 +13,7 @@ #include #include #include +#include class Configuration { @@ -20,35 +21,86 @@ public: explicit Configuration(const std::string &path, const std::string &home); ~Configuration(); - bool read(); - bool parse(const std::string &contents); + bool read(const QString &path); bool writeIfNeeded(const std::string &path = std::string()); - bool parseDefaultConfiguration(const std::string &contents); - std::vector childrenSettings(const char *name = ""); std::vector childrenGroups(const char *name = ""); - void resetValue(const char *path); - template std::optional value(const char *path) const { // if setting doesn't exist, give back a nullopt if(!m_userCfg->exists(path)) { - const_cast(this)->resetValue(path); - return value(path); + return std::nullopt; } const libconfig::Setting &v = m_userCfg->lookup(path); - if constexpr(std::is_same_v) - return std::optional(castToString(v)); - else + if constexpr(std::is_same_v) { + std::string r = castToString(v); + + // check if it's a path + if(r.front() == '~') { + r.replace(0, 1, m_homePath); + } + + return std::optional(r); + } else return std::optional(static_cast(v)); } template - bool setValue(std::string path, const T &val); + bool setValue(std::string path, const T &val) + { + if(!m_userCfg->exists(path)) { + return false; + } + + libconfig::Setting &setting = m_userCfg->lookup(path); + // compiler complained about operator= not taking unsinged ints, longs and long longs + if constexpr(std::is_unsigned_v && !std::is_same_v) { + setting = static_cast>(val); + } else if constexpr(std::is_same_v) { + switch(setting.getType()) { + case libconfig::Setting::TypeNone: + break; + + case libconfig::Setting::TypeInt: + case libconfig::Setting::TypeInt64: + setting = std::stoi(static_cast(val)); + break; + + case libconfig::Setting::TypeFloat: + setting = std::stod(static_cast(val)); + break; + + case libconfig::Setting::TypeString: + setting = static_cast(val).c_str(); + break; + + case libconfig::Setting::TypeBoolean: + if(static_cast(val) == "true") { + setting = true; + } else if(static_cast(val) == "false") { + setting = false; + } + break; + + case libconfig::Setting::TypeGroup: + break; + case libconfig::Setting::TypeArray: + break; + case libconfig::Setting::TypeList: + break; + } + + } else { + setting = val; + } + + changed = true; + return true; + } private: std::string castToString(const libconfig::Setting &v) const; @@ -56,29 +108,10 @@ private: bool changed = false; std::string m_homePath; std::string m_userCfgPath; - libconfig::Config *m_userCfg, *m_defaultCfg; + libconfig::Config *m_userCfg; }; // replace ~ with home std::string patchHome(const std::string &path, const std::string &home); -// instantiate functions -// this needs to be done because the implementation is in the cpp file - -// Settings::setValue<> -extern template bool Configuration::setValue(std::string path, const int &val); -extern template bool Configuration::setValue(std::string path, const unsigned int &val); -extern template bool Configuration::setValue(std::string path, const long &val); -extern template bool Configuration::setValue(std::string path, const unsigned long &val); - -extern template bool Configuration::setValue(std::string path, const long long &val); -extern template bool Configuration::setValue(std::string path, const unsigned long long &val); - -extern template bool Configuration::setValue(std::string path, const float &val); -extern template bool Configuration::setValue(std::string path, const double &val); - -extern template bool Configuration::setValue(std::string path, const bool &val); - -extern template bool Configuration::setValue(std::string path, const std::string &val); - #endif // CONFIGURATION_H -- cgit v1.2.1