aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.h
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-03-15 16:26:38 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-03-15 16:26:38 +0100
commit44421abbe89be2c6a6290182571fff82dfec9651 (patch)
tree69d7980953968f900411db190254a4d05acd2cae /lib/configuration/configuration.h
parentAdd missing AUTOUIC in ProfileEditor (diff)
downloadsmolbote-44421abbe89be2c6a6290182571fff82dfec9651.tar.xz
Moved Configuration class into library
Diffstat (limited to 'lib/configuration/configuration.h')
-rw-r--r--lib/configuration/configuration.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h
new file mode 100644
index 0000000..4d58a90
--- /dev/null
+++ b/lib/configuration/configuration.h
@@ -0,0 +1,76 @@
+/*
+ * 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 SMOLBOTE_CONFIGURATION_H
+#define SMOLBOTE_CONFIGURATION_H
+
+#include <optional>
+#include <string>
+#include <vector>
+#include <QString>
+#include <boost/program_options.hpp>
+#include <QStringList>
+
+class Configuration
+{
+public:
+ explicit Configuration();
+ ~Configuration();
+
+ bool read(const QString &path);
+ bool parse(int argc, const char **argv);
+
+ template <typename T>
+ std::optional<T> value(const char *path) const
+ {
+ // 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
+
+ if constexpr(std::is_same_v<T, std::string>) {
+ std::string r;
+ try {
+ r = vm[path].as<std::string>();
+ } catch (boost::bad_any_cast &) {
+ // try int
+ try {
+ r = std::to_string(vm[path].as<int>());
+ } catch (boost::bad_any_cast &) {
+
+ // try bool, and crash if not that either
+ r = vm[path].as<bool>() ? "true" : "false";
+ }
+
+ }
+
+ // 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>(vm[path].as<T>());
+ }
+
+ const std::vector<boost::shared_ptr<boost::program_options::option_description>> & options() {
+ return desc.options();
+ }
+
+private:
+ boost::program_options::options_description desc;
+ boost::program_options::variables_map vm;
+
+ std::string m_homePath;
+};
+
+#endif // SMOLBOTE_CONFIGURATION_H