src/settings.h

changeset 233
5509bec02c81
parent 231
a9bf6bab5ea2
child 237
10a6298f636f
equal deleted inserted replaced
232:8efa3a33172e 233:5509bec02c81
3 #include <QSettings> 3 #include <QSettings>
4 #include <QMdiArea> 4 #include <QMdiArea>
5 #include "libraries.h" 5 #include "libraries.h"
6 #include "gl/common.h" 6 #include "gl/common.h"
7 7
8 // SettingType - get type of setting by enumerator 8 template<class>
9 template<typename>
10 struct SettingId{};
11
12 template<SettingId>
13 struct SettingInfo{}; 9 struct SettingInfo{};
14 10
15 #define SETTING(NAME, DEFVALUE) \ 11 #define SETTING(NAME, DEFVALUE) \
16 namespace SettingIdTypes { class NAME; } \ 12 namespace Setting { class NAME; } \
17 namespace Setting { constexpr SettingId<class NAME##_SettingType> NAME; } \
18 template<> \ 13 template<> \
19 struct SettingInfo<Setting::NAME> \ 14 struct SettingInfo<Setting::NAME> \
20 { \ 15 { \
21 using type = decltype((DEFVALUE)); \ 16 using type = decltype((DEFVALUE)); \
22 static constexpr char name[] = #NAME; \ 17 static constexpr char name[] = #NAME; \
49 44
50 // End of setting definitions 45 // End of setting definitions
51 // ----------------------------------------------------------------------------- 46 // -----------------------------------------------------------------------------
52 #undef SETTING 47 #undef SETTING
53 48
54 template<SettingId X> 49 template<class X>
55 using SettingType_t = typename SettingInfo<X>::type; 50 using SettingType_t = typename SettingInfo<X>::type;
56 static_assert(std::is_same_v<SettingType_t<Setting::DrawAxes>, bool>); 51 static_assert(std::is_same_v<SettingType_t<Setting::DrawAxes>, bool>);
57 52
58 template<SettingId X> 53 template<class X>
59 const char* settingName = SettingInfo<X>::name; 54 const char* settingName = SettingInfo<X>::name;
60 55
61 template<SettingId X> 56 template<class X>
62 inline auto settingDefaultValue() 57 inline auto settingDefaultValue()
63 { 58 {
64 return SettingInfo<X>::defaultValue(); 59 return SettingInfo<X>::defaultValue();
65 } 60 }
66 61
67 // get() - get setting by enumerator 62 // get() - get setting by enumerator
68 template<SettingId X> 63 template<class X>
69 inline SettingType_t<X> setting() 64 inline SettingType_t<X> setting()
70 { 65 {
71 static const QVariant defvariant = QVariant::fromValue(settingDefaultValue<X>()); 66 static const QVariant defvariant = QVariant::fromValue(settingDefaultValue<X>());
72 const QVariant var = QSettings{}.value(settingName<X>, defvariant); 67 const QVariant var = QSettings{}.value(settingName<X>, defvariant);
73 return var.value<SettingType_t<X>>(); 68 return var.value<SettingType_t<X>>();
74 } 69 }
75 70
76 // setSetting() - set value of setting 71 // setSetting() - set value of setting
77 template<SettingId X> 72 template<class X>
78 inline void setSetting(const SettingType_t<X>& value) 73 inline void setSetting(const SettingType_t<X>& value)
79 { 74 {
80 if (value == settingDefaultValue<X>()) { 75 if (value == settingDefaultValue<X>()) {
81 QSettings{}.remove(settingName<X>); 76 QSettings{}.remove(settingName<X>);
82 } 77 }

mercurial