From f3a4607d6a722a862af0eb9747a15dcdf624b6fb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 3 Nov 2019 00:18:10 +0200 Subject: Drop boost dependency - wrote not-invented-here config file parser and conf class - spent obscene amount of time plugging in said conf class --- lib/configuration/configuration.h | 136 +++++++++++++------------------------- 1 file changed, 46 insertions(+), 90 deletions(-) (limited to 'lib/configuration/configuration.h') diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index 40da50e..bc8f52e 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -9,121 +9,77 @@ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H -#include -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include +#include +#include -class Configuration : public QObject +typedef std::variant conf_value_t; + +class [[clang::consumable(unconsumed)]] Configuration : private std::unordered_map { - Q_OBJECT public: - explicit Configuration(int argc, char** argv, const std::string &path, QObject *parent = nullptr); + [[clang::return_typestate(unconsumed)]] + explicit Configuration(); + + [[clang::return_typestate(unconsumed)]] + explicit Configuration(std::initializer_list> l) noexcept; + + explicit Configuration(Configuration &&other [[clang::param_typestate(unconsumed)]]) = default; + ~Configuration() = default; - bool exists(const char *path) - { - return vm.count(path) ? true : false; - } + [[clang::callable_when(unconsumed)]] + void read(std::basic_istream &input); template - std::optional value(const char *path) const + [[clang::callable_when(unconsumed)]] std::optional value(const char *path) const { - // if setting doesn't exist, we crash - // in debug builds, check if setting exists - - if(vm.count(path) == 0) { + if(use_global) + return instance()->value(path); + + if(this->count(path) == 0) { return std::nullopt; } - // path is guaranteed to exist, so using vm[path] is safe - - if constexpr(std::is_same_v) { - QStringList r; - for(const std::string &item : vm[path].as>()) { - r.append(QString::fromStdString(item)); - } - return std::optional(r); - - } else if constexpr(std::is_same_v || std::is_same_v) { - - if(vm[path].value().type() == typeid(int)) { - if constexpr(std::is_same_v) - return std::optional(std::to_string(vm[path].as())); - else if constexpr(std::is_same_v) - return std::optional(QString::number(vm[path].as())); - } - - if(vm[path].value().type() == typeid(bool)) { - return std::optional(vm[path].as() ? "true" : "false"); - } - - std::string r = vm[path].as(); + // path is guaranteed to exist + const auto value = at(path); - // check if it's a path - if(r.front() == '~') { + if constexpr(std::is_same_v || std::is_same_v) { + auto r = std::get(value); + if(r.front() == '~') r.replace(0, 1, m_homePath); - } - if constexpr(std::is_same_v) - return std::optional(r); - else if constexpr(std::is_same_v) - return std::optional(QString::fromStdString(r)); + if constexpr(std::is_same_v) + return std::make_optional(QString::fromStdString(r)); + else + return std::make_optional(r); - } else - return std::optional(vm[path].as()); - } - - template - void setValue(const char *path, const T &value) - { - if(vm.count(path) == 0) { - qWarning("value(%s) does not exist", path); - } - - vm.at(path).value() = value; - - emit settingChanged(path, value); - } + } else if constexpr(std::is_same_v) { + return std::make_optional(QString::fromStdString(std::get(value)).split(';')); - void setShortcut(QAction *action, const char *name) const - { - Q_CHECK_PTR(action); - - const auto shortcutText = this->value(name); - if(shortcutText) { - const QString tooltip = action->toolTip(); - action->setShortcut(QKeySequence::fromString(shortcutText.value())); - action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value())); - - connect(this, &Configuration::settingChanged, action, [=](const std::string &path, const QString &value) { - if(path == name) { - action->setShortcut(QKeySequence::fromString(value)); - action->setToolTip(QString("%1 (%2)").arg(tooltip, value)); - } - }); - } - } - - QHash section(const std::string &prefix) const; - const boost::program_options::options_description& description() const - { - return configuration_desc; - } + } else if (std::holds_alternative(value)) { + return std::optional(std::get(value)); + } else + return std::nullopt; + + } // std::optional value(path) const -signals: - void settingChanged(const std::string &path, const QString &value); + static void move_global(std::unique_ptr &&conf); private: - boost::program_options::options_description configuration_desc; - boost::program_options::variables_map vm; + static Configuration *instance(); const std::string m_homePath; + const bool use_global = false; }; +void setShortcut(QAction *action, const char *name); + #endif // SMOLBOTE_CONFIGURATION_H -- cgit v1.2.1