aboutsummaryrefslogtreecommitdiff
path: root/lib/settings/configuration.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/settings/configuration.h')
-rw-r--r--lib/settings/configuration.h97
1 files changed, 65 insertions, 32 deletions
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 <optional>
#include <string>
#include <vector>
+#include <QString>
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<std::string> childrenSettings(const char *name = "");
std::vector<std::string> childrenGroups(const char *name = "");
- void resetValue(const char *path);
-
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)) {
- const_cast<Configuration *>(this)->resetValue(path);
- return value<T>(path);
+ return std::nullopt;
}
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
+ if constexpr(std::is_same_v<T, std::string>) {
+ std::string r = castToString(v);
+
+ // check if it's a path
+ if(r.front() == '~') {
+ r.replace(0, 1, m_homePath);
+ }
+
+ 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);
+ 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>) {
+ switch(setting.getType()) {
+ case libconfig::Setting::TypeNone:
+ break;
+
+ case libconfig::Setting::TypeInt:
+ case libconfig::Setting::TypeInt64:
+ setting = std::stoi(static_cast<std::string>(val));
+ break;
+
+ case libconfig::Setting::TypeFloat:
+ setting = std::stod(static_cast<std::string>(val));
+ break;
+
+ case libconfig::Setting::TypeString:
+ setting = static_cast<std::string>(val).c_str();
+ break;
+
+ case libconfig::Setting::TypeBoolean:
+ if(static_cast<std::string>(val) == "true") {
+ setting = true;
+ } else if(static_cast<std::string>(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<int>(std::string path, const int &val);
-extern template bool Configuration::setValue<unsigned int>(std::string path, const unsigned int &val);
-extern template bool Configuration::setValue<long>(std::string path, const long &val);
-extern template bool Configuration::setValue<unsigned long>(std::string path, const unsigned long &val);
-
-extern template bool Configuration::setValue<long long>(std::string path, const long long &val);
-extern template bool Configuration::setValue<unsigned long long>(std::string path, const unsigned long long &val);
-
-extern template bool Configuration::setValue<float>(std::string path, const float &val);
-extern template bool Configuration::setValue<double>(std::string path, const double &val);
-
-extern template bool Configuration::setValue<bool>(std::string path, const bool &val);
-
-extern template bool Configuration::setValue<std::string>(std::string path, const std::string &val);
-
#endif // CONFIGURATION_H