diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-26 23:09:31 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-26 23:09:31 +0100 |
commit | fcf3870b7c0f30a5991e518ad8a404a9d38c3a45 (patch) | |
tree | 3168f904fb2dd205ce1fbbac2daa0f381ec86b70 /src/configuration.h | |
parent | Added warning when trying to get value of non-existent options (diff) | |
download | smolbote-fcf3870b7c0f30a5991e518ad8a404a9d38c3a45.tar.xz |
Using boost::program_options instead of libconfig
Diffstat (limited to 'src/configuration.h')
-rw-r--r-- | src/configuration.h | 54 |
1 files changed, 14 insertions, 40 deletions
diff --git a/src/configuration.h b/src/configuration.h index 913fa65..27988b9 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -9,39 +9,34 @@ #ifndef CONFIGURATION_H #define CONFIGURATION_H -#include <libconfig.h++> #include <optional> #include <string> #include <vector> #include <QString> - -std::string castToString(const libconfig::Setting &v); -void setFromString(libconfig::Setting &setting, const std::string &value); +#include <boost/program_options.hpp> +#include <QStringList> class Configuration { public: - explicit Configuration(const std::string &path, const std::string &home); + explicit Configuration(const QStringList &options = QStringList()); ~Configuration(); bool read(const QString &path); - bool writeIfNeeded(const std::string &path = std::string()); - - std::vector<std::string> childrenSettings(const char *name = ""); - std::vector<std::string> childrenGroups(const char *name = ""); template <typename T> std::optional<T> value(const char *path) const { - // if setting doesn't exist, give back a nullopt - if(!m_userCfg->exists(path)) { - qWarning("Requesting non-existent option %s", path); - return std::nullopt; + // if setting doesn't exist, we crash + // in debug builds, check if setting exists +#ifdef QT_DEBUG + if(vm.count(path) == 0) { + qWarning("value(%s) does not exist, probably crashing now", path); } +#endif - const libconfig::Setting &v = m_userCfg->lookup(path); if constexpr(std::is_same_v<T, std::string>) { - std::string r = castToString(v); + std::string r = vm[path].as<std::string>(); // check if it's a path if(r.front() == '~') { @@ -50,35 +45,14 @@ public: return std::optional<std::string>(r); } else - return std::optional<T>(static_cast<T>(v)); - } - - template <typename T> - 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<T> && !std::is_same_v<T, bool>) { - setting = static_cast<typename std::make_signed_t<T>>(val); - } else if constexpr(std::is_same_v<T, std::string>) { - setFromString(setting, val); - } else { - setting = val; - } - - changed = true; - return true; + return std::optional<T>(vm[path].as<T>()); } private: - bool changed = false; + boost::program_options::options_description desc; + boost::program_options::variables_map vm; + std::string m_homePath; - std::string m_userCfgPath; - libconfig::Config *m_userCfg; }; #endif // CONFIGURATION_H |