|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 // advconfig_impl_legacy.h : legacy (foobar2000 v1.x compatible) implementation of advconfig objects
|
|
|
4
|
|
|
5 #include "cfg_var_legacy.h"
|
|
|
6 using namespace cfg_var_legacy;
|
|
|
7
|
|
|
8 namespace fb2k {
|
|
|
9 pfc::string8 advconfig_autoName(const GUID&);
|
|
|
10 }
|
|
|
11
|
|
|
12 #if FOOBAR2000_SUPPORT_CFG_VAR_DOWNGRADE
|
|
|
13 #define ADVCONFIG_DOWNGRADE { this->get_static_instance().downgrade_set_name(configStoreName); }
|
|
|
14 #define ADVCONFIG_DOWNGRADE_AUTO { this->get_static_instance().downgrade_set_name( fb2k::advconfig_autoName(p_guid) ); }
|
|
|
15 #else
|
|
|
16 #define ADVCONFIG_DOWNGRADE {(void)configStoreName;}
|
|
|
17 #define ADVCONFIG_DOWNGRADE_AUTO
|
|
|
18 #endif
|
|
|
19
|
|
|
20 //! Standard implementation of advconfig_entry_checkbox. \n
|
|
|
21 class advconfig_entry_checkbox_impl : public advconfig_entry_checkbox_v2 {
|
|
|
22 public:
|
|
|
23 advconfig_entry_checkbox_impl(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, bool isRadio = false, uint32_t flags = 0)
|
|
|
24 : m_name(p_name), m_initialstate(p_initialstate), m_state(p_guid, p_initialstate), m_parent(p_parent), m_priority(p_priority), m_isRadio(isRadio), m_flags(flags) {}
|
|
|
25
|
|
|
26 void get_name(pfc::string_base& p_out) override { p_out = m_name; }
|
|
|
27 GUID get_guid() override { return m_state.get_guid(); }
|
|
|
28 GUID get_parent() override { return m_parent; }
|
|
|
29 void reset() override { m_state = m_initialstate; }
|
|
|
30 bool get_state() override { return m_state; }
|
|
|
31 void set_state(bool p_state) override { m_state = p_state; }
|
|
|
32 bool is_radio() override { return m_isRadio; }
|
|
|
33 double get_sort_priority() override { return m_priority; }
|
|
|
34 bool get_default_state() override { return m_initialstate; }
|
|
|
35 t_uint32 get_preferences_flags() override { return m_flags; }
|
|
|
36
|
|
|
37 bool get_state_() const { return m_state; }
|
|
|
38 bool get_default_state_() const { return m_initialstate; }
|
|
|
39 #if FOOBAR2000_SUPPORT_CFG_VAR_DOWNGRADE
|
|
|
40 void downgrade_set_name(const char * arg) { m_state.downgrade_set_name(arg); }
|
|
|
41 #endif
|
|
|
42 private:
|
|
|
43 const pfc::string8 m_name;
|
|
|
44 const bool m_initialstate;
|
|
|
45 const bool m_isRadio;
|
|
|
46 const uint32_t m_flags;
|
|
|
47 cfg_bool m_state;
|
|
|
48 const GUID m_parent;
|
|
|
49 const double m_priority;
|
|
|
50 };
|
|
|
51
|
|
|
52 //! Service factory helper around standard advconfig_branch implementation. Use this class to register your own Advanced Preferences branches. \n
|
|
|
53 //! Usage: static advconfig_branch_factory mybranch(name, branchID, parentBranchID, priority);
|
|
|
54 class advconfig_branch_factory : public service_factory_single_t<advconfig_branch_impl> {
|
|
|
55 public:
|
|
|
56 advconfig_branch_factory(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority)
|
|
|
57 : service_factory_single_t<advconfig_branch_impl>(p_name, p_guid, p_parent, p_priority) {}
|
|
|
58 };
|
|
|
59
|
|
|
60 class advconfig_checkbox_factory_common : public service_factory_single_t<advconfig_entry_checkbox_impl> {
|
|
|
61 public:
|
|
|
62 template<typename ... args_t> advconfig_checkbox_factory_common(args_t && ... args) : service_factory_single_t<advconfig_entry_checkbox_impl>(std::forward<args_t>(args) ...) {}
|
|
|
63
|
|
|
64 bool get() const { return this->get_static_instance().get_state_(); }
|
|
|
65 void set(bool val) { this->get_static_instance().set_state(val); }
|
|
|
66 operator bool() const { return get(); }
|
|
|
67 bool operator=(bool val) { set(val); return val; }
|
|
|
68 };
|
|
|
69
|
|
|
70 class advconfig_checkbox_factory : public advconfig_checkbox_factory_common {
|
|
|
71 public:
|
|
|
72 advconfig_checkbox_factory(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, uint32_t flags = 0)
|
|
|
73 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, false /* not radio */, flags) {
|
|
|
74 ADVCONFIG_DOWNGRADE_AUTO;
|
|
|
75 }
|
|
|
76 advconfig_checkbox_factory(const char* p_name, const char * configStoreName, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, uint32_t flags = 0)
|
|
|
77 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, false /* not radio */, flags) {
|
|
|
78 ADVCONFIG_DOWNGRADE;
|
|
|
79 }
|
|
|
80
|
|
|
81 };
|
|
|
82
|
|
|
83 class advconfig_radio_factory : public advconfig_checkbox_factory_common {
|
|
|
84 public:
|
|
|
85 advconfig_radio_factory(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, uint32_t flags = 0)
|
|
|
86 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, true /* radio */, flags) {
|
|
|
87 ADVCONFIG_DOWNGRADE_AUTO;
|
|
|
88 }
|
|
|
89 advconfig_radio_factory(const char* p_name, const char * configStoreName, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, uint32_t flags = 0)
|
|
|
90 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, true /* radio */, flags) {
|
|
|
91 ADVCONFIG_DOWNGRADE;
|
|
|
92 }
|
|
|
93 };
|
|
|
94
|
|
|
95 // OBSOLETE, use advconfig_checkbox_factory / advconfig_radio_factory
|
|
|
96 template<bool p_is_radio, uint32_t prefFlags = 0>
|
|
|
97 class advconfig_checkbox_factory_t : public advconfig_checkbox_factory_common {
|
|
|
98 public:
|
|
|
99 advconfig_checkbox_factory_t(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate)
|
|
|
100 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, p_is_radio, prefFlags) {
|
|
|
101 ADVCONFIG_DOWNGRADE_AUTO;
|
|
|
102 }
|
|
|
103 advconfig_checkbox_factory_t(const char* p_name, const char * configStoreName, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate)
|
|
|
104 : advconfig_checkbox_factory_common(p_name, p_guid, p_parent, p_priority, p_initialstate, p_is_radio, prefFlags) {
|
|
|
105 ADVCONFIG_DOWNGRADE;
|
|
|
106 }
|
|
|
107 };
|
|
|
108
|
|
|
109 //! Special advconfig_entry_string implementation - implements integer entries. Use advconfig_integer_factory to register your own integer entries in Advanced Preferences instead of using this class directly.
|
|
|
110 template<typename int_t_>
|
|
|
111 class advconfig_entry_integer_impl_ : public advconfig_entry_string_v2 {
|
|
|
112 public:
|
|
|
113 typedef int_t_ int_t;
|
|
|
114 advconfig_entry_integer_impl_(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, int_t p_initialstate, int_t p_min, int_t p_max, t_uint32 p_prefFlags)
|
|
|
115 : m_name(p_name), m_parent(p_parent), m_priority(p_priority), m_initval(p_initialstate), m_min(p_min), m_max(p_max), m_state(p_guid, p_initialstate), m_prefFlags(p_prefFlags) {
|
|
|
116 PFC_ASSERT(p_min < p_max);
|
|
|
117 }
|
|
|
118 void get_name(pfc::string_base& p_out) { p_out = m_name; }
|
|
|
119 GUID get_guid() { return m_state.get_guid(); }
|
|
|
120 GUID get_parent() { return m_parent; }
|
|
|
121 void reset() { m_state = m_initval; }
|
|
|
122 double get_sort_priority() { return m_priority; }
|
|
|
123 void get_state(pfc::string_base& p_out) { format(p_out, m_state.get_value()); }
|
|
|
124 void set_state(const char* p_string, t_size p_length) { set_state_int(myATOI(p_string, p_length)); }
|
|
|
125 t_uint32 get_flags() { return advconfig_entry_string::flag_is_integer | (is_signed() ? flag_is_signed : 0); }
|
|
|
126
|
|
|
127 int_t get_state_int() const { return m_state; }
|
|
|
128 void set_state_int(int_t val) { m_state = pfc::clip_t<int_t>(val, m_min, m_max); }
|
|
|
129
|
|
|
130 void get_default_state(pfc::string_base& out) {
|
|
|
131 format(out, m_initval);
|
|
|
132 }
|
|
|
133 void validate(pfc::string_base& val) {
|
|
|
134 format(val, pfc::clip_t<int_t>(myATOI(val, SIZE_MAX), m_min, m_max));
|
|
|
135 }
|
|
|
136 t_uint32 get_preferences_flags() { return m_prefFlags; }
|
|
|
137 #if FOOBAR2000_SUPPORT_CFG_VAR_DOWNGRADE
|
|
|
138 void downgrade_set_name(const char * arg) { m_state.downgrade_set_name(arg); }
|
|
|
139 #endif
|
|
|
140 private:
|
|
|
141 static void format(pfc::string_base& out, int_t v) {
|
|
|
142 if (is_signed()) out = pfc::format_int(v).get_ptr();
|
|
|
143 else out = pfc::format_uint(v).get_ptr();
|
|
|
144 }
|
|
|
145 static int_t myATOI(const char* s, size_t l) {
|
|
|
146 if (is_signed()) return pfc::atoi64_ex(s, l);
|
|
|
147 else return pfc::atoui64_ex(s, l);
|
|
|
148 }
|
|
|
149 static bool is_signed() {
|
|
|
150 return ((int_t)-1) < ((int_t)0);
|
|
|
151 }
|
|
|
152 cfg_int_t<int_t> m_state;
|
|
|
153 const double m_priority;
|
|
|
154 const int_t m_initval, m_min, m_max;
|
|
|
155 const GUID m_parent;
|
|
|
156 const pfc::string8 m_name;
|
|
|
157 const t_uint32 m_prefFlags;
|
|
|
158 };
|
|
|
159
|
|
|
160 typedef advconfig_entry_integer_impl_<uint64_t> advconfig_entry_integer_impl;
|
|
|
161
|
|
|
162 //! Service factory helper around integer-specialized advconfig_entry_string implementation. Use this class to register your own integer entries in Advanced Preferences. \n
|
|
|
163 //! Usage: static advconfig_integer_factory myint(name, itemID, parentID, priority, initialValue, minValue, maxValue);
|
|
|
164 template<typename int_t_>
|
|
|
165 class advconfig_integer_factory_ : public service_factory_single_t<advconfig_entry_integer_impl_<int_t_> > {
|
|
|
166 public:
|
|
|
167 typedef int_t_ int_t;
|
|
|
168 advconfig_integer_factory_(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, t_uint64 p_initialstate, t_uint64 p_min, t_uint64 p_max, t_uint32 p_prefFlags = 0)
|
|
|
169 : service_factory_single_t<advconfig_entry_integer_impl_<int_t_> >(p_name, p_guid, p_parent, p_priority, p_initialstate, p_min, p_max, p_prefFlags) {
|
|
|
170 ADVCONFIG_DOWNGRADE_AUTO;
|
|
|
171 }
|
|
|
172
|
|
|
173 advconfig_integer_factory_(const char* p_name, const char * configStoreName, const GUID& p_guid, const GUID& p_parent, double p_priority, t_uint64 p_initialstate, t_uint64 p_min, t_uint64 p_max, t_uint32 p_prefFlags = 0)
|
|
|
174 : service_factory_single_t<advconfig_entry_integer_impl_<int_t_> >(p_name, p_guid, p_parent, p_priority, p_initialstate, p_min, p_max, p_prefFlags) {
|
|
|
175 ADVCONFIG_DOWNGRADE;
|
|
|
176 }
|
|
|
177
|
|
|
178 int_t get() const { return this->get_static_instance().get_state_int(); }
|
|
|
179 void set(int_t val) { this->get_static_instance().set_state_int(val); }
|
|
|
180
|
|
|
181 operator int_t() const { return get(); }
|
|
|
182 int_t operator=(int_t val) { set(val); return val; }
|
|
|
183 };
|
|
|
184
|
|
|
185 typedef advconfig_integer_factory_<uint64_t> advconfig_integer_factory;
|
|
|
186 typedef advconfig_integer_factory_<int64_t> advconfig_signed_integer_factory;
|
|
|
187
|
|
|
188
|
|
|
189 //! Standard advconfig_entry_string implementation
|
|
|
190 class advconfig_entry_string_impl : public advconfig_entry_string_v2 {
|
|
|
191 public:
|
|
|
192 advconfig_entry_string_impl(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, const char* p_initialstate, t_uint32 p_prefFlags)
|
|
|
193 : m_name(p_name), m_parent(p_parent), m_priority(p_priority), m_initialstate(p_initialstate), m_state(p_guid, p_initialstate), m_prefFlags(p_prefFlags) {}
|
|
|
194 void get_name(pfc::string_base& p_out) { p_out = m_name; }
|
|
|
195 GUID get_guid() { return m_state.get_guid(); }
|
|
|
196 GUID get_parent() { return m_parent; }
|
|
|
197 void reset() {
|
|
|
198 inWriteSync(m_sync);
|
|
|
199 m_state = m_initialstate;
|
|
|
200 }
|
|
|
201 double get_sort_priority() { return m_priority; }
|
|
|
202 void get_state(pfc::string_base& p_out) {
|
|
|
203 inReadSync(m_sync);
|
|
|
204 p_out = m_state;
|
|
|
205 }
|
|
|
206 void set_state(const char* p_string, t_size p_length = SIZE_MAX) {
|
|
|
207 inWriteSync(m_sync);
|
|
|
208 m_state.set_string(p_string, p_length);
|
|
|
209 }
|
|
|
210 t_uint32 get_flags() { return 0; }
|
|
|
211 void get_default_state(pfc::string_base& out) { out = m_initialstate; }
|
|
|
212 t_uint32 get_preferences_flags() { return m_prefFlags; }
|
|
|
213 #if FOOBAR2000_SUPPORT_CFG_VAR_DOWNGRADE
|
|
|
214 void downgrade_set_name(const char * arg) { m_state.downgrade_set_name(arg); }
|
|
|
215 #endif
|
|
|
216 private:
|
|
|
217 const pfc::string8 m_initialstate, m_name;
|
|
|
218 cfg_string m_state;
|
|
|
219 pfc::readWriteLock m_sync;
|
|
|
220 const double m_priority;
|
|
|
221 const GUID m_parent;
|
|
|
222 const t_uint32 m_prefFlags;
|
|
|
223 };
|
|
|
224
|
|
|
225 //! Service factory helper around standard advconfig_entry_string implementation. Use this class to register your own string entries in Advanced Preferences. \n
|
|
|
226 //! Usage: static advconfig_string_factory mystring(name, itemID, branchID, priority, initialValue);
|
|
|
227 class advconfig_string_factory : public service_factory_single_t<advconfig_entry_string_impl> {
|
|
|
228 public:
|
|
|
229 advconfig_string_factory(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, const char* p_initialstate, t_uint32 p_prefFlags = 0)
|
|
|
230 : service_factory_single_t<advconfig_entry_string_impl>(p_name, p_guid, p_parent, p_priority, p_initialstate, p_prefFlags) {
|
|
|
231 ADVCONFIG_DOWNGRADE_AUTO;
|
|
|
232 }
|
|
|
233 advconfig_string_factory(const char* p_name, const char * configStoreName, const GUID& p_guid, const GUID& p_parent, double p_priority, const char* p_initialstate, t_uint32 p_prefFlags = 0)
|
|
|
234 : service_factory_single_t<advconfig_entry_string_impl>(p_name, p_guid, p_parent, p_priority, p_initialstate, p_prefFlags) {
|
|
|
235 ADVCONFIG_DOWNGRADE;
|
|
|
236 }
|
|
|
237
|
|
|
238 void get(pfc::string_base& out) { get_static_instance().get_state(out); }
|
|
|
239 void set(const char* in) { get_static_instance().set_state(in); }
|
|
|
240 };
|
|
|
241
|
|
|
242 // No more separate _MT versions, readWriteLock overhead is irrelevant
|
|
|
243 typedef advconfig_entry_string_impl advconfig_entry_string_impl_MT;
|
|
|
244 typedef advconfig_string_factory advconfig_string_factory_MT;
|
|
|
245
|
|
|
246
|
|
|
247 /*
|
|
|
248 Advanced Preferences variable declaration examples
|
|
|
249
|
|
|
250 static advconfig_string_factory mystring("name goes here",myguid,parentguid,0,"asdf");
|
|
|
251 to retrieve state: pfc::string8 val; mystring.get(val);
|
|
|
252
|
|
|
253 static advconfig_checkbox_factory mycheckbox("name goes here",myguid,parentguid,0,false);
|
|
|
254 to retrieve state: mycheckbox.get();
|
|
|
255
|
|
|
256 static advconfig_integer_factory myint("name goes here",myguid,parentguid,0,initialValue,minimumValue,maximumValue);
|
|
|
257 to retrieve state: myint.get();
|
|
|
258 */
|
|
|
259
|
|
|
260
|
|
|
261 #undef ADVCONFIG_DOWNGRADE
|
|
|
262 #undef ADVCONFIG_DOWNGRADE_AUTO
|