diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-17 13:32:13 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-17 13:32:13 +0100 |
commit | d592ee191e8aedc84c670e6d818fcf97c0bd7a92 (patch) | |
tree | b2bd8413332cd1567b9ae954835a7a4c930de730 /lib/settings/configuration.h | |
parent | Fixed build scripts for use with the mercurial repository (diff) | |
download | smolbote-d592ee191e8aedc84c670e6d818fcf97c0bd7a92.tar.xz |
Configuration creates missing items based on defaults
Diffstat (limited to 'lib/settings/configuration.h')
-rw-r--r-- | lib/settings/configuration.h | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/lib/settings/configuration.h b/lib/settings/configuration.h index 4fc614e..67ba7b1 100644 --- a/lib/settings/configuration.h +++ b/lib/settings/configuration.h @@ -9,26 +9,20 @@ #ifndef CONFIGURATION_H #define CONFIGURATION_H +#include <libconfig.h++> #include <optional> #include <string> #include <vector> -namespace libconfig -{ -class Config; -class Setting; -} - class Configuration { public: - Configuration(); + explicit Configuration(const std::string &path, const std::string &home); ~Configuration(); - bool readUserConfiguration(const std::string &path); + bool read(); bool parse(const std::string &contents); - bool writeUserConfiguration(const std::string &path); - bool writeIfNeeded(); + bool writeIfNeeded(const std::string &path = std::string()); bool parseDefaultConfiguration(const std::string &contents); @@ -38,39 +32,39 @@ public: void resetValue(const char *path); template <typename T> - std::optional<T> value(const char *path) const; + std::optional<T> value(const char *path) const + { + // if setting doesn't exist, give back a nullopt + if(!m_userCfg->exists(path)) { + const_cast<Configuration *>(this)->resetValue(path); + return value<T>(path); + } + + const libconfig::Setting &v = m_userCfg->lookup(path); + if constexpr(std::is_same_v<T, std::string>) + return std::optional<std::string>(castToString(v)); + else + return std::optional<T>(static_cast<T>(v)); + } template <typename T> bool setValue(std::string path, const T &val); private: + std::string castToString(const libconfig::Setting &v) const; + bool changed = false; + std::string m_homePath; std::string m_userCfgPath; libconfig::Config *m_userCfg, *m_defaultCfg; }; // replace ~ with home -std::string &patchHome(std::string &path, const std::string &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::value<> -extern template std::optional<int> Configuration::value<int>(const char *path) const; -extern template std::optional<unsigned int> Configuration::value<unsigned int>(const char *path) const; -extern template std::optional<long> Configuration::value<long>(const char *path) const; -extern template std::optional<unsigned long> Configuration::value<unsigned long>(const char *path) const; - -extern template std::optional<long long> Configuration::value<long long>(const char *path) const; -extern template std::optional<unsigned long long> Configuration::value<unsigned long long>(const char *path) const; - -extern template std::optional<float> Configuration::value<float>(const char *path) const; -extern template std::optional<double> Configuration::value<double>(const char *path) const; - -extern template std::optional<bool> Configuration::value<bool>(const char *path) const; - -extern template std::optional<std::string> Configuration::value<std::string>(const char *path) const; - // Settings::setValue<> extern template bool Configuration::setValue<int>(std::string path, const int &val); extern template bool Configuration::setValue<unsigned int>(std::string path, const unsigned int &val); |