|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 // advconfig_impl.h : mainline (foobar2000 v2.0) implementation of advconfig objects
|
|
|
4
|
|
|
5 #include "advconfig.h"
|
|
|
6
|
|
|
7 //! Standard implementation of advconfig_branch. \n
|
|
|
8 //! Usage: no need to use this class directly - use advconfig_branch_factory instead.
|
|
|
9 class advconfig_branch_impl : public advconfig_branch {
|
|
|
10 public:
|
|
|
11 advconfig_branch_impl(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority) : m_name(p_name), m_guid(p_guid), m_parent(p_parent), m_priority(p_priority) {}
|
|
|
12 void get_name(pfc::string_base& p_out) override { p_out = m_name; }
|
|
|
13 GUID get_guid() override { return m_guid; }
|
|
|
14 GUID get_parent() override { return m_parent; }
|
|
|
15 void reset() override {}
|
|
|
16 double get_sort_priority() override { return m_priority; }
|
|
|
17 private:
|
|
|
18 pfc::string8 m_name;
|
|
|
19 GUID m_guid, m_parent;
|
|
|
20 const double m_priority;
|
|
|
21 };
|
|
|
22
|
|
|
23
|
|
|
24 #if FOOBAR2000_TARGET_VERSION < 81
|
|
|
25 #include "advconfig_impl_legacy.h"
|
|
|
26 #else
|
|
|
27
|
|
|
28 #include "configStore.h"
|
|
|
29 #include "configCache.h"
|
|
|
30 #include "cfg_var_legacy.h" // cfg_var_reader
|
|
|
31
|
|
|
32 namespace fb2k {
|
|
|
33 pfc::string8 advconfig_autoName(const GUID& id);
|
|
|
34 pfc::string8 advconfig_autoName(const GUID& id, const char * specified);
|
|
|
35 }
|
|
|
36
|
|
|
37
|
|
|
38 //! Standard implementation of advconfig_entry_checkbox. \n
|
|
|
39 class advconfig_entry_checkbox_impl : public advconfig_entry_checkbox_v2, private cfg_var_legacy::cfg_var_reader {
|
|
|
40 public:
|
|
|
41 advconfig_entry_checkbox_impl(const char* p_name, const char * varName, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate, bool isRadio = false, uint32_t flags = 0)
|
|
|
42 : cfg_var_reader(p_guid), m_name(p_name), m_varName(varName), m_guid(p_guid), m_initialstate(p_initialstate), m_parent(p_parent), m_priority(p_priority), m_isRadio(isRadio), m_flags(flags) {}
|
|
|
43
|
|
|
44 void get_name(pfc::string_base& p_out) override { p_out = m_name; }
|
|
|
45 GUID get_guid() override { return m_guid; }
|
|
|
46 GUID get_parent() override { return m_parent; }
|
|
|
47 void reset() override;
|
|
|
48 bool get_state() override { return get_state_(); }
|
|
|
49 void set_state(bool p_state) override;
|
|
|
50 bool is_radio() override { return m_isRadio; }
|
|
|
51 double get_sort_priority() override { return m_priority; }
|
|
|
52 bool get_default_state() override { return m_initialstate; }
|
|
|
53 t_uint32 get_preferences_flags() override { return m_flags; }
|
|
|
54
|
|
|
55 bool get_state_() const;
|
|
|
56 bool get_default_state_() const { return m_initialstate; }
|
|
|
57
|
|
|
58 const char* varName() const { return m_varName; }
|
|
|
59 private:
|
|
|
60 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
|
|
|
61 // cfg_var_reader
|
|
|
62 void set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) override;
|
|
|
63 #endif
|
|
|
64
|
|
|
65 const pfc::string8 m_name, m_varName;
|
|
|
66 const GUID m_guid;
|
|
|
67 const bool m_initialstate;
|
|
|
68 const bool m_isRadio;
|
|
|
69 const uint32_t m_flags;
|
|
|
70
|
|
|
71 const GUID m_parent;
|
|
|
72 const double m_priority;
|
|
|
73 };
|
|
|
74
|
|
|
75 //! Service factory helper around standard advconfig_branch implementation. Use this class to register your own Advanced Preferences branches. \n
|
|
|
76 //! Usage: static advconfig_branch_factory mybranch(name, branchID, parentBranchID, priority);
|
|
|
77 class advconfig_branch_factory : public service_factory_single_t<advconfig_branch_impl> {
|
|
|
78 public:
|
|
|
79 advconfig_branch_factory(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority)
|
|
|
80 : service_factory_single_t<advconfig_branch_impl>(p_name, p_guid, p_parent, p_priority) {}
|
|
|
81 };
|
|
|
82
|
|
|
83 template<bool is_radio>
|
|
|
84 class advconfig_checkbox_factory_ : public service_factory_single_t<advconfig_entry_checkbox_impl> {
|
|
|
85 public:
|
|
|
86 advconfig_checkbox_factory_(const char* name, const GUID& guid, const GUID& parent, double priority, bool initialstate, uint32_t flags = 0) : service_factory_single_t<advconfig_entry_checkbox_impl>(name, fb2k::advconfig_autoName(guid), guid, parent, priority, initialstate, is_radio, flags) {}
|
|
|
87 advconfig_checkbox_factory_(const char* name, const char* varName, const GUID& guid, const GUID& parent, double priority, bool initial, uint32_t flags = 0) : service_factory_single_t<advconfig_entry_checkbox_impl>(name, varName, guid, parent, priority, initial, is_radio, flags) {}
|
|
|
88
|
|
|
89 bool get() const { return this->get_static_instance().get_state_(); }
|
|
|
90 void set(bool val) { this->get_static_instance().set_state(val); }
|
|
|
91 operator bool() const { return get(); }
|
|
|
92 bool operator=(bool val) { set(val); return val; }
|
|
|
93 const char* varName() const { return this->get_static_instance().varName(); }
|
|
|
94 };
|
|
|
95
|
|
|
96 typedef advconfig_checkbox_factory_<false> advconfig_checkbox_factory;
|
|
|
97 typedef advconfig_checkbox_factory_<true> advconfig_radio_factory;
|
|
|
98
|
|
|
99 #if 0
|
|
|
100 // OBSOLETE, use advconfig_checkbox_factory / advconfig_radio_factory
|
|
|
101 template<bool p_is_radio, uint32_t prefFlags = 0>
|
|
|
102 class advconfig_checkbox_factory_t : public advconfig_checkbox_factory_<p_is_radio> {
|
|
|
103 public:
|
|
|
104 advconfig_checkbox_factory_t(const char* p_name, const GUID& p_guid, const GUID& p_parent, double p_priority, bool p_initialstate) : advconfig_checkbox_factory_<p_is_radio>(p_name, fb2k::advconfig_autoName(p_guid), p_guid, p_parent, p_priority, p_initialstate, prefFlags) {}
|
|
|
105 };
|
|
|
106 #endif
|
|
|
107
|
|
|
108 struct advconfig_entry_string_desc { const char* name; const char* varName; GUID guid, parent; double priority = 0; const char* initial = ""; uint32_t prefsFlags = 0; uint32_t flags = 0; };
|
|
|
109
|
|
|
110 //! Standard advconfig_entry_string implementation. Use advconfig_string_factory to register your own string entries in Advanced Preferences instead of using this class directly.
|
|
|
111 class advconfig_entry_string_impl : public advconfig_entry_string_v2, private cfg_var_legacy::cfg_var_reader {
|
|
|
112 public:
|
|
|
113 advconfig_entry_string_impl(advconfig_entry_string_desc const& arg) : cfg_var_reader(arg.guid), m_name(arg.name), m_varName(fb2k::advconfig_autoName(arg.guid, arg.varName)), m_guid(arg.guid), m_parent(arg.parent), m_priority(arg.priority), m_initialstate(arg.initial), m_prefFlags(arg.prefsFlags), m_flags(arg.flags) {}
|
|
|
114 advconfig_entry_string_impl(const char* p_name, const char * p_varName, const GUID& p_guid, const GUID& p_parent, double p_priority, const char* p_initialstate, t_uint32 p_prefFlags)
|
|
|
115 : cfg_var_reader(p_guid), m_name(p_name), m_varName(p_varName), m_guid(p_guid), m_parent(p_parent), m_initialstate(p_initialstate), m_priority(p_priority), m_prefFlags(p_prefFlags) {}
|
|
|
116 void get_name(pfc::string_base& p_out) override { p_out = m_name; }
|
|
|
117 GUID get_guid() override { return m_guid; }
|
|
|
118 GUID get_parent() override { return m_parent; }
|
|
|
119 void reset() override;
|
|
|
120 double get_sort_priority() override { return m_priority; }
|
|
|
121 void get_state(pfc::string_base& p_out) override;
|
|
|
122 void set_state(const char* p_string, t_size p_length = SIZE_MAX) override;
|
|
|
123 t_uint32 get_flags() override { return m_flags; }
|
|
|
124 void get_default_state(pfc::string_base& out) override{ out = m_initialstate; }
|
|
|
125 t_uint32 get_preferences_flags() override { return m_prefFlags; }
|
|
|
126 private:
|
|
|
127 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
|
|
|
128 // cfg_var_reader
|
|
|
129 void set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) override;
|
|
|
130 #endif
|
|
|
131
|
|
|
132 const pfc::string8 m_initialstate, m_name, m_varName;
|
|
|
133 const GUID m_guid, m_parent;
|
|
|
134
|
|
|
135 const double m_priority;
|
|
|
136 const t_uint32 m_prefFlags;
|
|
|
137 const uint32_t m_flags = 0;
|
|
|
138 };
|
|
|
139
|
|
|
140 //! Service factory helper around standard advconfig_entry_string implementation. Use this class to register your own string entries in Advanced Preferences. \n
|
|
|
141 //! Usage: static advconfig_string_factory mystring(name, itemID, branchID, priority, initialValue);
|
|
|
142 class advconfig_string_factory : public service_factory_single_t<advconfig_entry_string_impl> {
|
|
|
143 public:
|
|
|
144 advconfig_string_factory(const char* p_name, const char * varName, const GUID& p_guid, const GUID& p_parent, double p_priority, const char* p_initialstate, t_uint32 p_prefFlags = 0) : service_factory_single_t<advconfig_entry_string_impl>(p_name, varName, p_guid, p_parent, p_priority, p_initialstate, p_prefFlags) {}
|
|
|
145 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) : service_factory_single_t<advconfig_entry_string_impl>(p_name, fb2k::advconfig_autoName(p_guid), p_guid, p_parent, p_priority, p_initialstate, p_prefFlags) {}
|
|
|
146 advconfig_string_factory(advconfig_entry_string_desc const& arg) : service_factory_single_t<advconfig_entry_string_impl>(arg) {}
|
|
|
147
|
|
|
148 void get(pfc::string_base& out) { get_static_instance().get_state(out); }
|
|
|
149 pfc::string8 get() { pfc::string8 temp; get(temp); return temp; }
|
|
|
150 void set(const char* in) { get_static_instance().set_state(in); }
|
|
|
151 };
|
|
|
152
|
|
|
153 // Thread hacks no longer needed
|
|
|
154 typedef advconfig_string_factory advconfig_string_factory_MT;
|
|
|
155
|
|
|
156 //! 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.
|
|
|
157 template<typename int_t_>
|
|
|
158 class advconfig_entry_integer_impl_ : public advconfig_entry_string_v2, private cfg_var_legacy::cfg_var_reader {
|
|
|
159 public:
|
|
|
160 typedef int_t_ int_t;
|
|
|
161 advconfig_entry_integer_impl_(const char* p_name, const char * p_varName, 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)
|
|
|
162 : cfg_var_reader(p_guid), m_name(p_name), m_varName(p_varName), m_guid(p_guid), m_parent(p_parent), m_priority(p_priority), m_initval(p_initialstate), m_min(p_min), m_max(p_max), m_prefFlags(p_prefFlags) {
|
|
|
163 PFC_ASSERT(p_min < p_max);
|
|
|
164 }
|
|
|
165 void get_name(pfc::string_base& p_out) override { p_out = m_name; }
|
|
|
166 GUID get_guid() override { return m_guid; }
|
|
|
167 GUID get_parent() override { return m_parent; }
|
|
|
168 void reset() override {fb2k::configStore::get()->deleteConfigBool(m_varName);}
|
|
|
169 double get_sort_priority() override { return m_priority; }
|
|
|
170 void get_state(pfc::string_base& p_out) override { p_out = pfc::format_int( get_state_int() ); }
|
|
|
171 void set_state(const char* p_string, t_size p_length) override {set_state_int(pfc::atoi64_ex(p_string, p_length));}
|
|
|
172 t_uint32 get_flags() override { return advconfig_entry_string::flag_is_integer | (is_signed() ? flag_is_signed : 0); }
|
|
|
173
|
|
|
174 int_t get_state_int() const {return fb2k::configStore::get()->getConfigInt(m_varName, m_initval);}
|
|
|
175 void set_state_int(int_t val) { val = pfc::clip_t<int_t>(val, m_min, m_max); fb2k::configStore::get()->setConfigInt(m_varName, val); }
|
|
|
176
|
|
|
177 void get_default_state(pfc::string_base& out) override {
|
|
|
178 format(out, m_initval);
|
|
|
179 }
|
|
|
180 void validate(pfc::string_base& val) override {
|
|
|
181 format(val, pfc::clip_t<int_t>(pfc::atoi64_ex(val, SIZE_MAX), m_min, m_max));
|
|
|
182 }
|
|
|
183 t_uint32 get_preferences_flags() override { return m_prefFlags; }
|
|
|
184 private:
|
|
|
185 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
|
|
|
186 // cfg_var_reader
|
|
|
187 void set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) override {
|
|
|
188 switch (p_sizehint) {
|
|
|
189 case 4:
|
|
|
190 { int32_t v; p_stream->read_lendian_t(v, p_abort); this->set_state_int(v); } break;
|
|
|
191 case 8:
|
|
|
192 { int64_t v; p_stream->read_lendian_t(v, p_abort); this->set_state_int(v); } break;
|
|
|
193 }
|
|
|
194 }
|
|
|
195
|
|
|
196 #endif
|
|
|
197
|
|
|
198 void format(pfc::string_base& out, int_t v) const {
|
|
|
199 if (is_signed()) out = pfc::format_int(v);
|
|
|
200 else out = pfc::format_uint(v);
|
|
|
201 }
|
|
|
202 bool is_signed() const {
|
|
|
203 return m_min < 0;
|
|
|
204 }
|
|
|
205
|
|
|
206 const double m_priority;
|
|
|
207 const int_t m_initval, m_min, m_max;
|
|
|
208 const GUID m_guid, m_parent;
|
|
|
209 const pfc::string8 m_name, m_varName;
|
|
|
210 const t_uint32 m_prefFlags;
|
|
|
211 };
|
|
|
212
|
|
|
213 typedef advconfig_entry_integer_impl_<uint64_t> advconfig_entry_integer_impl;
|
|
|
214
|
|
|
215 //! Service factory helper around integer-specialized advconfig_entry_string implementation. Use this class to register your own integer entries in Advanced Preferences. \n
|
|
|
216 //! Usage: static advconfig_integer_factory myint(name, itemID, parentID, priority, initialValue, minValue, maxValue);
|
|
|
217 template<typename int_t_>
|
|
|
218 class advconfig_integer_factory_ : public service_factory_single_t<advconfig_entry_integer_impl_<int_t_>> {
|
|
|
219 public:
|
|
|
220 typedef int_t_ int_t;
|
|
|
221 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) : service_factory_single_t<advconfig_entry_integer_impl>(p_name, fb2k::advconfig_autoName(p_guid), p_guid, p_parent, p_priority, p_initialstate, p_min, p_max, p_prefFlags) {}
|
|
|
222 advconfig_integer_factory_(const char* p_name, const char * p_varName, 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 = 0) : service_factory_single_t<advconfig_entry_integer_impl_<int_t_> >(p_name, p_varName, p_guid, p_parent, p_priority, p_initialstate, p_min, p_max, p_prefFlags) {}
|
|
|
223
|
|
|
224 int_t get() const { return this->get_static_instance().get_state_int(); }
|
|
|
225 void set(int_t val) { this->get_static_instance().set_state_int(val); }
|
|
|
226
|
|
|
227 operator int_t() const { return get(); }
|
|
|
228 int_t operator=(int_t val) { set(val); return val; }
|
|
|
229 };
|
|
|
230
|
|
|
231 typedef advconfig_integer_factory_<uint64_t> advconfig_integer_factory;
|
|
|
232 typedef advconfig_integer_factory_<int64_t> advconfig_signed_integer_factory;
|
|
|
233
|
|
|
234
|
|
|
235 class advconfig_integer_factory_cached : public advconfig_integer_factory {
|
|
|
236 public:
|
|
|
237 using advconfig_integer_factory::int_t;
|
|
|
238 advconfig_integer_factory_cached(const char* p_name, const char* p_varName, 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 = 0)
|
|
|
239 : advconfig_integer_factory(p_name, p_varName, p_guid, p_parent, p_priority, p_initialstate, p_min, p_max, p_prefFlags),
|
|
|
240 m_cache(p_varName, p_initialstate) {}
|
|
|
241
|
|
|
242 int_t get() { return m_cache.get(); }
|
|
|
243 void set(int_t v) { m_cache.set(v); }
|
|
|
244 operator int_t() { return get(); }
|
|
|
245 private:
|
|
|
246 fb2k::configIntCache m_cache;
|
|
|
247 };
|
|
|
248
|
|
|
249 template<bool is_radio>
|
|
|
250 class advconfig_checkbox_factory_cached_ : public advconfig_checkbox_factory_<is_radio> {
|
|
|
251 public:
|
|
|
252 advconfig_checkbox_factory_cached_(const char* name, const char* varName, const GUID& guid, const GUID& parent, double priority, bool initial, uint32_t flags = 0)
|
|
|
253 : advconfig_checkbox_factory_<is_radio>(name, varName, guid, parent, priority, initial, flags),
|
|
|
254 m_cache(varName, initial) {}
|
|
|
255
|
|
|
256 bool get() { return m_cache.get(); }
|
|
|
257 void set(bool v) { m_cache.set(v); }
|
|
|
258 operator bool() { return get(); }
|
|
|
259 private:
|
|
|
260 fb2k::configBoolCache m_cache;
|
|
|
261 };
|
|
|
262
|
|
|
263 typedef advconfig_checkbox_factory_cached_<false> advconfig_checkbox_factory_cached;
|
|
|
264 typedef advconfig_checkbox_factory_cached_<true> advconfig_radio_factory_cached;
|
|
|
265
|
|
|
266 /*
|
|
|
267 Advanced Preferences variable declaration examples
|
|
|
268
|
|
|
269 static advconfig_string_factory mystring("name goes here","configStore var name goes here", myguid,parentguid,0,"asdf");
|
|
|
270 to retrieve state: pfc::string8 val; mystring.get(val);
|
|
|
271
|
|
|
272 static advconfig_checkbox_factory mycheckbox("name goes here","configStore var name goes here", myguid,parentguid,0,false);
|
|
|
273 to retrieve state: mycheckbox.get();
|
|
|
274
|
|
|
275 static advconfig_integer_factory myint("name goes here","configStore var name goes herE", myguid,parentguid,0,initialValue,minimumValue,maximumValue);
|
|
|
276 to retrieve state: myint.get();
|
|
|
277 */
|
|
|
278 #endif
|