summaryrefslogtreecommitdiff
path: root/src/common/settings_setting.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/settings_setting.h')
-rw-r--r--src/common/settings_setting.h413
1 files changed, 413 insertions, 0 deletions
diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h
new file mode 100644
index 000000000..f226e38d4
--- /dev/null
+++ b/src/common/settings_setting.h
@@ -0,0 +1,413 @@
1#pragma once
2
3#include <map>
4#include <optional>
5#include <string>
6#include <typeindex>
7#include <typeinfo>
8#include "common/common_types.h"
9#include "common/settings_common.h"
10#include "common/settings_enums.h"
11
12namespace Settings {
13
14/** The Setting class is a simple resource manager. It defines a label and default value
15 * alongside the actual value of the setting for simpler and less-error prone use with frontend
16 * configurations. Specifying a default value and label is required. A minimum and maximum range
17 * can be specified for sanitization.
18 */
19template <typename Type, bool ranged = false>
20class Setting : public BasicSetting {
21protected:
22 Setting() = default;
23
24 /**
25 * Only sets the setting to the given initializer, leaving the other members to their default
26 * initializers.
27 *
28 * @param global_val Initial value of the setting
29 */
30 explicit Setting(const Type& val)
31 : value{val},
32 default_value{}, maximum{}, minimum{}, label{}, category{Category::Miscellaneous}, id{} {}
33
34public:
35 /**
36 * Sets a default value, label, and setting value.
37 *
38 * @param linkage Setting registry
39 * @param default_val Initial value of the setting, and default value of the setting
40 * @param name Label for the setting
41 * @param category_ Category of the setting AKA INI group
42 */
43 explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name,
44 enum Category category_, bool save_ = true, bool runtime_modifiable_ = false)
45 requires(!ranged)
46 : value{default_val}, default_value{default_val}, label{name}, category{category_},
47 id{linkage.count}, save{save_}, runtime_modifiable{runtime_modifiable_} {
48 linkage.by_category[category].push_front(this);
49 linkage.count++;
50 }
51 virtual ~Setting() = default;
52
53 /**
54 * Sets a default value, minimum value, maximum value, and label.
55 *
56 * @param linkage Setting registry
57 * @param default_val Initial value of the setting, and default value of the setting
58 * @param min_val Sets the minimum allowed value of the setting
59 * @param max_val Sets the maximum allowed value of the setting
60 * @param name Label for the setting
61 * @param category_ Category of the setting AKA INI group
62 */
63 explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val,
64 const Type& max_val, const std::string& name, enum Category category_,
65 bool save_ = true, bool runtime_modifiable_ = false)
66 requires(ranged)
67 : value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val},
68 label{name}, category{category_}, id{linkage.count}, save{save_},
69 runtime_modifiable{runtime_modifiable_} {
70 linkage.by_category[category].push_front(this);
71 linkage.count++;
72 }
73
74 /**
75 * Returns a reference to the setting's value.
76 *
77 * @returns A reference to the setting
78 */
79 [[nodiscard]] virtual const Type& GetValue() const {
80 return value;
81 }
82
83 /**
84 * Sets the setting to the given value.
85 *
86 * @param val The desired value
87 */
88 virtual void SetValue(const Type& val) {
89 Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
90 std::swap(value, temp);
91 }
92
93 /**
94 * Returns the value that this setting was created with.
95 *
96 * @returns A reference to the default value
97 */
98 [[nodiscard]] const Type& GetDefault() const {
99 return default_value;
100 }
101
102 /**
103 * Returns the label this setting was created with.
104 *
105 * @returns A reference to the label
106 */
107 [[nodiscard]] const std::string& GetLabel() const override {
108 return label;
109 }
110
111 /**
112 * Returns the setting's category AKA INI group.
113 *
114 * @returns The setting's category
115 */
116 [[nodiscard]] enum Category Category() const override {
117 return category;
118 }
119
120 [[nodiscard]] bool RuntimeModfiable() const override {
121 return runtime_modifiable;
122 }
123
124 [[nodiscard]] constexpr bool IsEnum() const override {
125 return std::is_enum<Type>::value;
126 }
127
128 /**
129 * Returns whether the current setting is Switchable.
130 *
131 * @returns If the setting is a SwitchableSetting
132 */
133 [[nodiscard]] virtual constexpr bool Switchable() const override {
134 return false;
135 }
136
137protected:
138 std::string ToString(const Type& value_) const {
139 if constexpr (std::is_same<Type, std::string>()) {
140 return value_;
141 } else if constexpr (std::is_same<Type, std::optional<u32>>()) {
142 return value_.has_value() ? std::to_string(*value_) : "none";
143 } else if constexpr (std::is_same<Type, bool>()) {
144 return value_ ? "true" : "false";
145 } else if (std::is_same<Type, AudioEngine>()) {
146 return CanonicalizeEnum(value_);
147 } else {
148 return std::to_string(static_cast<u64>(value_));
149 }
150 }
151
152public:
153 /**
154 * Converts the value of the setting to a std::string. Respects the global state if the setting
155 * has one.
156 *
157 * @returns The current setting as a std::string
158 */
159 std::string ToString() const override {
160 return ToString(this->GetValue());
161 }
162
163 /**
164 * Returns the default value of the setting as a std::string.
165 *
166 * @returns The default value as a string.
167 */
168 std::string DefaultToString() const override {
169 return ToString(default_value);
170 }
171
172 /**
173 * Assigns a value to the setting.
174 *
175 * @param val The desired setting value
176 *
177 * @returns A reference to the setting
178 */
179 virtual const Type& operator=(const Type& val) {
180 Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
181 std::swap(value, temp);
182 return value;
183 }
184
185 /**
186 * Returns a reference to the setting.
187 *
188 * @returns A reference to the setting
189 */
190 explicit virtual operator const Type&() const {
191 return value;
192 }
193
194 /**
195 * Converts the given value to the Setting's type of value. Uses SetValue to enter the setting,
196 * thus respecting its constraints.
197 *
198 * @param input The desired value
199 */
200 void LoadString(const std::string& input) override {
201 if (input.empty()) {
202 this->SetValue(this->GetDefault());
203 return;
204 }
205 try {
206 if constexpr (std::is_same<Type, std::string>()) {
207 this->SetValue(input);
208 } else if constexpr (std::is_same<Type, std::optional<u32>>()) {
209 this->SetValue(static_cast<u32>(std::stoul(input)));
210 } else if constexpr (std::is_same<Type, bool>()) {
211 this->SetValue(input == "true");
212 } else if constexpr (std::is_same<Type, AudioEngine>()) {
213 this->SetValue(ToEnum<Type>(input));
214 } else {
215 this->SetValue(static_cast<Type>(std::stoll(input)));
216 }
217 } catch (std::invalid_argument) {
218 this->SetValue(this->GetDefault());
219 }
220 }
221
222 [[nodiscard]] std::string constexpr Canonicalize() const override {
223 if constexpr (std::is_enum<Type>::value) {
224 return CanonicalizeEnum(this->GetValue());
225 }
226 return ToString(this->GetValue());
227 }
228
229 /**
230 * Returns the save preference of the setting i.e. when saving or reading the setting from a
231 * frontend, whether this setting should be skipped.
232 *
233 * @returns The save preference
234 */
235 virtual bool Save() const override {
236 return save;
237 }
238
239 /**
240 * Gives us another way to identify the setting without having to go through a string.
241 *
242 * @returns the type_index of the setting's type
243 */
244 virtual std::type_index TypeId() const override {
245 return std::type_index(typeid(Type));
246 }
247
248 virtual constexpr u32 Id() const override {
249 return id;
250 }
251
252 virtual std::string MinVal() const override {
253 return this->ToString(minimum);
254 }
255 virtual std::string MaxVal() const override {
256 return this->ToString(maximum);
257 }
258
259protected:
260 Type value{}; ///< The setting
261 const Type default_value{}; ///< The default value
262 const Type maximum{}; ///< Maximum allowed value of the setting
263 const Type minimum{}; ///< Minimum allowed value of the setting
264 const std::string label{}; ///< The setting's label
265 const enum Category category; ///< The setting's category AKA INI group
266 const u32 id;
267 bool save;
268 bool runtime_modifiable;
269};
270
271/**
272 * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a
273 * custom setting to switch to when a guest application specifically requires it. The effect is that
274 * other components of the emulator can access the setting's intended value without any need for the
275 * component to ask whether the custom or global setting is needed at the moment.
276 *
277 * By default, the global setting is used.
278 */
279template <typename Type, bool ranged = false>
280class SwitchableSetting : virtual public Setting<Type, ranged> {
281public:
282 /**
283 * Sets a default value, label, and setting value.
284 *
285 * @param linkage Setting registry
286 * @param default_val Initial value of the setting, and default value of the setting
287 * @param name Label for the setting
288 * @param category_ Category of the setting AKA INI group
289 */
290 explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name,
291 Category category, bool save = true, bool runtime_modifiable = false)
292 requires(!ranged)
293 : Setting<Type, false>{linkage, default_val, name, category, save, runtime_modifiable} {
294 linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
295 }
296 virtual ~SwitchableSetting() = default;
297
298 /**
299 * Sets a default value, minimum value, maximum value, and label.
300 *
301 * @param linkage Setting registry
302 * @param default_val Initial value of the setting, and default value of the setting
303 * @param min_val Sets the minimum allowed value of the setting
304 * @param max_val Sets the maximum allowed value of the setting
305 * @param name Label for the setting
306 * @param category_ Category of the setting AKA INI group
307 */
308 explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val,
309 const Type& max_val, const std::string& name, Category category,
310 bool save = true, bool runtime_modifiable = false)
311 requires(ranged)
312 : Setting<Type, true>{linkage, default_val, min_val, max_val,
313 name, category, save, runtime_modifiable} {
314 linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
315 }
316
317 /**
318 * Tells this setting to represent either the global or custom setting when other member
319 * functions are used.
320 *
321 * @param to_global Whether to use the global or custom setting.
322 */
323 void SetGlobal(bool to_global) override {
324 use_global = to_global;
325 }
326
327 /**
328 * Returns whether this setting is using the global setting or not.
329 *
330 * @returns The global state
331 */
332 [[nodiscard]] bool UsingGlobal() const override {
333 return use_global;
334 }
335
336 /**
337 * Returns either the global or custom setting depending on the values of this setting's global
338 * state or if the global value was specifically requested.
339 *
340 * @param need_global Request global value regardless of setting's state; defaults to false
341 *
342 * @returns The required value of the setting
343 */
344 [[nodiscard]] virtual const Type& GetValue() const override {
345 if (use_global) {
346 return this->value;
347 }
348 return custom;
349 }
350 [[nodiscard]] virtual const Type& GetValue(bool need_global) const {
351 if (use_global || need_global) {
352 return this->value;
353 }
354 return custom;
355 }
356
357 /**
358 * Sets the current setting value depending on the global state.
359 *
360 * @param val The new value
361 */
362 void SetValue(const Type& val) override {
363 Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
364 if (use_global) {
365 std::swap(this->value, temp);
366 } else {
367 std::swap(custom, temp);
368 }
369 }
370
371 [[nodiscard]] virtual constexpr bool Switchable() const override {
372 return true;
373 }
374
375 [[nodiscard]] virtual std::string ToStringGlobal() const override {
376 return this->ToString(this->value);
377 }
378
379 /**
380 * Assigns the current setting value depending on the global state.
381 *
382 * @param val The new value
383 *
384 * @returns A reference to the current setting value
385 */
386 const Type& operator=(const Type& val) override {
387 Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
388 if (use_global) {
389 std::swap(this->value, temp);
390 return this->value;
391 }
392 std::swap(custom, temp);
393 return custom;
394 }
395
396 /**
397 * Returns the current setting value depending on the global state.
398 *
399 * @returns A reference to the current setting value
400 */
401 virtual explicit operator const Type&() const override {
402 if (use_global) {
403 return this->value;
404 }
405 return custom;
406 }
407
408protected:
409 bool use_global{true}; ///< The setting's global state
410 Type custom{}; ///< The custom value of the setting
411};
412
413} // namespace Settings