summaryrefslogtreecommitdiffstats
path: root/src/common/settings_setting.h
blob: 55280fec4fc28773763a115f24f478046886d5f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <limits>
#include <map>
#include <optional>
#include <stdexcept>
#include <string>
#include <typeindex>
#include <typeinfo>
#include "common/common_types.h"
#include "common/settings_common.h"
#include "common/settings_enums.h"

namespace Settings {

/** The Setting class is a simple resource manager. It defines a label and default value
 * alongside the actual value of the setting for simpler and less-error prone use with frontend
 * configurations. Specifying a default value and label is required. A minimum and maximum range
 * can be specified for sanitization.
 */
template <typename Type, bool ranged = false>
class Setting : public BasicSetting {
protected:
    Setting() = default;

public:
    /**
     * Sets a default value, label, and setting value.
     *
     * @param linkage Setting registry
     * @param default_val Initial value of the setting, and default value of the setting
     * @param name Label for the setting
     * @param category_ Category of the setting AKA INI group
     * @param specialization_ Suggestion for how frontend implemetations represent this in a config
     * @param save_ Suggests that this should or should not be saved to a frontend config file
     * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
     * @param other_setting_ A second Setting to associate to this one in metadata
     */
    explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name,
                     Category category_, u32 specialization_ = Specialization::Default,
                     bool save_ = true, bool runtime_modifiable_ = false,
                     BasicSetting* other_setting_ = nullptr)
        requires(!ranged)
        : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
                       other_setting_),
          value{default_val}, default_value{default_val} {}
    virtual ~Setting() = default;

    /**
     * Sets a default value, minimum value, maximum value, and label.
     *
     * @param linkage Setting registry
     * @param default_val Initial value of the setting, and default value of the setting
     * @param min_val Sets the minimum allowed value of the setting
     * @param max_val Sets the maximum allowed value of the setting
     * @param name Label for the setting
     * @param category_ Category of the setting AKA INI group
     * @param specialization_ Suggestion for how frontend implemetations represent this in a config
     * @param save_ Suggests that this should or should not be saved to a frontend config file
     * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
     * @param other_setting_ A second Setting to associate to this one in metadata
     */
    explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val,
                     const Type& max_val, const std::string& name, Category category_,
                     u32 specialization_ = Specialization::Default, bool save_ = true,
                     bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr)
        requires(ranged)
        : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
                       other_setting_),
          value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {}

    /**
     *  Returns a reference to the setting's value.
     *
     * @returns A reference to the setting
     */
    [[nodiscard]] virtual const Type& GetValue() const {
        return value;
    }

    /**
     * Sets the setting to the given value.
     *
     * @param val The desired value
     */
    virtual void SetValue(const Type& val) {
        Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
        std::swap(value, temp);
    }

    /**
     * Returns the value that this setting was created with.
     *
     * @returns A reference to the default value
     */
    [[nodiscard]] const Type& GetDefault() const {
        return default_value;
    }

    [[nodiscard]] constexpr bool IsEnum() const override {
        return std::is_enum_v<Type>;
    }

protected:
    [[nodiscard]] std::string ToString(const Type& value_) const {
        if constexpr (std::is_same_v<Type, std::string>) {
            return value_;
        } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
            return value_.has_value() ? std::to_string(*value_) : "none";
        } else if constexpr (std::is_same_v<Type, bool>) {
            return value_ ? "true" : "false";
        } else if constexpr (std::is_same_v<Type, AudioEngine>) {
            // Compatibility with old AudioEngine setting being a string
            return CanonicalizeEnum(value_);
        } else {
            return std::to_string(static_cast<u64>(value_));
        }
    }

public:
    /**
     * Converts the value of the setting to a std::string. Respects the global state if the setting
     * has one.
     *
     * @returns The current setting as a std::string
     */
    [[nodiscard]] std::string ToString() const override {
        return ToString(this->GetValue());
    }

    /**
     * Returns the default value of the setting as a std::string.
     *
     * @returns The default value as a string.
     */
    [[nodiscard]] std::string DefaultToString() const override {
        return ToString(default_value);
    }

    /**
     * Assigns a value to the setting.
     *
     * @param val The desired setting value
     *
     * @returns A reference to the setting
     */
    virtual const Type& operator=(const Type& val) {
        Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
        std::swap(value, temp);
        return value;
    }

    /**
     * Returns a reference to the setting.
     *
     * @returns A reference to the setting
     */
    explicit virtual operator const Type&() const {
        return value;
    }

    /**
     * Converts the given value to the Setting's type of value. Uses SetValue to enter the setting,
     * thus respecting its constraints.
     *
     * @param input The desired value
     */
    void LoadString(const std::string& input) override final {
        if (input.empty()) {
            this->SetValue(this->GetDefault());
            return;
        }
        try {
            if constexpr (std::is_same_v<Type, std::string>) {
                this->SetValue(input);
            } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
                this->SetValue(static_cast<u32>(std::stoul(input)));
            } else if constexpr (std::is_same_v<Type, bool>) {
                this->SetValue(input == "true");
            } else if constexpr (std::is_same_v<Type, AudioEngine>) {
                this->SetValue(ToEnum<Type>(input));
            } else {
                this->SetValue(static_cast<Type>(std::stoll(input)));
            }
        } catch (std::invalid_argument&) {
            this->SetValue(this->GetDefault());
        }
    }

    [[nodiscard]] std::string constexpr Canonicalize() const override final {
        if constexpr (std::is_enum_v<Type>) {
            return CanonicalizeEnum(this->GetValue());
        } else {
            return ToString(this->GetValue());
        }
    }

    /**
     * Gives us another way to identify the setting without having to go through a string.
     *
     * @returns the type_index of the setting's type
     */
    [[nodiscard]] std::type_index TypeId() const override final {
        return std::type_index(typeid(Type));
    }

    [[nodiscard]] constexpr u32 EnumIndex() const override final {
        if constexpr (std::is_enum_v<Type>) {
            return EnumMetadata<Type>::Index();
        } else {
            return std::numeric_limits<u32>::max();
        }
    }

    [[nodiscard]] std::string MinVal() const override final {
        return this->ToString(minimum);
    }
    [[nodiscard]] std::string MaxVal() const override final {
        return this->ToString(maximum);
    }

    [[nodiscard]] constexpr bool Ranged() const override {
        return ranged;
    }

protected:
    Type value{};               ///< The setting
    const Type default_value{}; ///< The default value
    const Type maximum{};       ///< Maximum allowed value of the setting
    const Type minimum{};       ///< Minimum allowed value of the setting
};

/**
 * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a
 * custom setting to switch to when a guest application specifically requires it. The effect is that
 * other components of the emulator can access the setting's intended value without any need for the
 * component to ask whether the custom or global setting is needed at the moment.
 *
 * By default, the global setting is used.
 */
template <typename Type, bool ranged = false>
class SwitchableSetting : virtual public Setting<Type, ranged> {
public:
    /**
     * Sets a default value, label, and setting value.
     *
     * @param linkage Setting registry
     * @param default_val Initial value of the setting, and default value of the setting
     * @param name Label for the setting
     * @param category_ Category of the setting AKA INI group
     * @param specialization_ Suggestion for how frontend implemetations represent this in a config
     * @param save_ Suggests that this should or should not be saved to a frontend config file
     * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
     * @param other_setting_ A second Setting to associate to this one in metadata
     */
    explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name,
                               Category category_, u32 specialization_ = Specialization::Default,
                               bool save_ = true, bool runtime_modifiable_ = false,
                               BasicSetting* other_setting_ = nullptr)
        requires(!ranged)
        : Setting<Type, false>{
              linkage, default_val,         name,          category_, specialization_,
              save_,   runtime_modifiable_, other_setting_} {
        linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
    }
    virtual ~SwitchableSetting() = default;

    /**
     * Sets a default value, minimum value, maximum value, and label.
     *
     * @param linkage Setting registry
     * @param default_val Initial value of the setting, and default value of the setting
     * @param min_val Sets the minimum allowed value of the setting
     * @param max_val Sets the maximum allowed value of the setting
     * @param name Label for the setting
     * @param category_ Category of the setting AKA INI group
     * @param specialization_ Suggestion for how frontend implemetations represent this in a config
     * @param save_ Suggests that this should or should not be saved to a frontend config file
     * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
     * @param other_setting_ A second Setting to associate to this one in metadata
     */
    explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val,
                               const Type& max_val, const std::string& name, Category category_,
                               u32 specialization_ = Specialization::Default, bool save_ = true,
                               bool runtime_modifiable_ = false,
                               BasicSetting* other_setting_ = nullptr)
        requires(ranged)
        : Setting<Type, true>{linkage,         default_val, min_val,
                              max_val,         name,        category_,
                              specialization_, save_,       runtime_modifiable_,
                              other_setting_} {
        linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
    }

    /**
     * Tells this setting to represent either the global or custom setting when other member
     * functions are used.
     *
     * @param to_global Whether to use the global or custom setting.
     */
    void SetGlobal(bool to_global) override final {
        use_global = to_global;
    }

    /**
     * Returns whether this setting is using the global setting or not.
     *
     * @returns The global state
     */
    [[nodiscard]] bool UsingGlobal() const override final {
        return use_global;
    }

    /**
     * Returns either the global or custom setting depending on the values of this setting's global
     * state or if the global value was specifically requested.
     *
     * @param need_global Request global value regardless of setting's state; defaults to false
     *
     * @returns The required value of the setting
     */
    [[nodiscard]] const Type& GetValue() const override final {
        if (use_global) {
            return this->value;
        }
        return custom;
    }
    [[nodiscard]] const Type& GetValue(bool need_global) const {
        if (use_global || need_global) {
            return this->value;
        }
        return custom;
    }

    /**
     * Sets the current setting value depending on the global state.
     *
     * @param val The new value
     */
    void SetValue(const Type& val) override final {
        Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
        if (use_global) {
            std::swap(this->value, temp);
        } else {
            std::swap(custom, temp);
        }
    }

    [[nodiscard]] constexpr bool Switchable() const override final {
        return true;
    }

    [[nodiscard]] std::string ToStringGlobal() const override final {
        return this->ToString(this->value);
    }

    /**
     * Assigns the current setting value depending on the global state.
     *
     * @param val The new value
     *
     * @returns A reference to the current setting value
     */
    const Type& operator=(const Type& val) override final {
        Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
        if (use_global) {
            std::swap(this->value, temp);
            return this->value;
        }
        std::swap(custom, temp);
        return custom;
    }

    /**
     * Returns the current setting value depending on the global state.
     *
     * @returns A reference to the current setting value
     */
    explicit operator const Type&() const override final {
        if (use_global) {
            return this->value;
        }
        return custom;
    }

protected:
    bool use_global{true}; ///< The setting's global state
    Type custom{};         ///< The custom value of the setting
};

} // namespace Settings