diff -r 571e85c0d811 -r a6d937f8c0ac src/settings.h --- a/src/settings.h Tue Jun 14 20:35:11 2022 +0300 +++ b/src/settings.h Tue Jun 14 20:44:07 2022 +0300 @@ -3,52 +3,55 @@ #include #include "settingdefs.h" -// settingdefs - get name and default value by setting -static const struct Settingdef -{ - const char* name; - QVariant defaultValue; -} settingdefs[] = { -#define SETTING(NAME, DEFVALUE) {#NAME, QVariant::fromValue(DEFVALUE)}, -#include "settingdefs.h" -#undef SETTING -}; - // SettingType - get type of setting by enumerator -template -struct SettingType{}; +template +struct SettingInfo{}; #define SETTING(NAME, DEFVALUE) \ template<> \ - struct SettingType \ + struct SettingInfo \ { \ using type = decltype((DEFVALUE)); \ + static constexpr char name[] = #NAME; \ + static const auto& defaultValue() { \ + static const auto value = DEFVALUE; \ + return value; \ + }; \ }; #include "settingdefs.h" #undef SETTING -template -using SettingType_t = typename SettingType::type; +template +using SettingType_t = typename SettingInfo::type; static_assert(std::is_same_v, bool>); +template +const char* settingName = SettingInfo::name; + +template +inline auto settingDefaultValue() +{ + return SettingInfo::defaultValue(); +} + // get() - get setting by enumerator template inline SettingType_t setting() { - const Settingdef& def = settingdefs[static_cast(X)]; - return QSettings{}.value(def.name, def.defaultValue).value>(); + static const QVariant defvariant = QVariant::fromValue(settingDefaultValue()); + const QVariant var = QSettings{}.value(settingName, defvariant); + return var.value>(); } // setSetting() - set value of setting template inline void setSetting(const SettingType_t& value) { - const Settingdef& def = settingdefs[static_cast(X)]; - if (value == def.defaultValue.value>()) { - QSettings{}.remove(def.name); + if (value == settingDefaultValue()) { + QSettings{}.remove(settingName); } else { - QSettings{}.setValue(def.name, QVariant::fromValue(value)); + QSettings{}.setValue(settingName, QVariant::fromValue(value)); } }