aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.h
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-25 19:20:30 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-25 19:20:30 +0100
commitf4025c0ebcddffbb1b826cd666b94d9140a56663 (patch)
tree94d9990af2a41db4237154de1377151dd8997a74 /src/configuration.h
parentConfiguration class rework (diff)
downloadsmolbote-f4025c0ebcddffbb1b826cd666b94d9140a56663.tar.xz
Configuration class rework
- castToString is now a free function - setFromString split away from setValue - moved Configuration to src
Diffstat (limited to 'src/configuration.h')
-rw-r--r--src/configuration.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/configuration.h b/src/configuration.h
new file mode 100644
index 0000000..8173a86
--- /dev/null
+++ b/src/configuration.h
@@ -0,0 +1,83 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#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);
+
+class Configuration
+{
+public:
+ explicit Configuration(const std::string &path, const std::string &home);
+ ~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)) {
+ return std::nullopt;
+ }
+
+ const libconfig::Setting &v = m_userCfg->lookup(path);
+ 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)
+ {
+ 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;
+ }
+
+private:
+ bool changed = false;
+ std::string m_homePath;
+ std::string m_userCfgPath;
+ libconfig::Config *m_userCfg;
+};
+
+#endif // CONFIGURATION_H