diff options
Diffstat (limited to 'lib/configuration/configuration.h')
-rw-r--r-- | lib/configuration/configuration.h | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index b77685a..4459dd0 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -9,21 +9,21 @@ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H +#include <QAction> +#include <QString> +#include <initializer_list> +#include <memory> #include <optional> #include <string> -#include <vector> #include <unordered_map> #include <variant> -#include <initializer_list> -#include <memory> -#include <QAction> -#include <QString> +#include <vector> -#if defined (__clang__) -#define consumable(X) [[clang::consumable(X)]] -#define return_typestate(X) [[clang::return_typestate(X)]] -#define callable_when(X) [[clang::callable_when(X)]] -#define param_typestate(X) [[clang::param_typestate(X)]] +#if defined(__clang__) +#define consumable(X) [[clang::consumable(X)]] +#define return_typestate(X) [[clang::return_typestate(X)]] +#define callable_when(X) [[clang::callable_when(X)]] +#define param_typestate(X) [[clang::param_typestate(X)]] #else #define consumable(X) #define return_typestate(X) @@ -35,27 +35,25 @@ typedef std::variant<std::string, int, bool> conf_value_t; class consumable(unconsumed) Configuration : private std::unordered_map<std::string, conf_value_t> { + friend std::ostream &operator<<(std::ostream &out, const Configuration &obj); public: - return_typestate(unconsumed) - explicit Configuration(); + return_typestate(unconsumed) explicit Configuration(); - return_typestate(unconsumed) - explicit Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept; + return_typestate(unconsumed) explicit Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept; - explicit Configuration(Configuration &&other param_typestate(unconsumed)) = default; + explicit Configuration(Configuration && other param_typestate(unconsumed)) = default; ~Configuration() = default; - callable_when(unconsumed) - void read(std::basic_istream<char> &input); + callable_when(unconsumed) void read(std::basic_istream<char> & input); template <typename T> callable_when(unconsumed) std::optional<T> value(const char *path) const { if(use_global) return instance()->value<T>(path); - + if(this->count(path) == 0) { return std::nullopt; } @@ -64,7 +62,15 @@ public: const auto value = at(path); if constexpr(std::is_same_v<T, QString> || std::is_same_v<T, std::string>) { - auto r = std::get<std::string>(value); + auto r = [&value]() { + if(std::holds_alternative<std::string>(value)) + return std::get<std::string>(value); + else if(std::holds_alternative<int>(value)) + return std::to_string(std::get<int>(value)); + else + return std::get<bool>(value) ? std::string("true") : std::string("false"); + }(); + if(r.front() == '~') r.replace(0, 1, m_homePath); @@ -76,14 +82,14 @@ public: } else if constexpr(std::is_same_v<T, QStringList>) { return std::make_optional(QString::fromStdString(std::get<std::string>(value)).split(';')); - } else if (std::holds_alternative<T>(value)) { + } else if(std::holds_alternative<T>(value)) { return std::optional<T>(std::get<T>(value)); } else return std::nullopt; - + } // std::optional<T> value(path) const - static void move_global(std::unique_ptr<Configuration> &&conf); + static void move_global(std::unique_ptr<Configuration> && conf); private: static Configuration *instance(); @@ -93,5 +99,6 @@ private: }; void setShortcut(QAction *action, const char *name); +std::ostream &operator<<(std::ostream &out, const Configuration &obj); #endif // SMOLBOTE_CONFIGURATION_H |