settings_setting.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <limits>
  5. #include <map>
  6. #include <optional>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <typeindex>
  10. #include <typeinfo>
  11. #include "common/common_types.h"
  12. #include "common/settings_common.h"
  13. #include "common/settings_enums.h"
  14. namespace Settings {
  15. /** The Setting class is a simple resource manager. It defines a label and default value
  16. * alongside the actual value of the setting for simpler and less-error prone use with frontend
  17. * configurations. Specifying a default value and label is required. A minimum and maximum range
  18. * can be specified for sanitization.
  19. */
  20. template <typename Type, bool ranged = false>
  21. class Setting : public BasicSetting {
  22. protected:
  23. Setting() = default;
  24. public:
  25. /**
  26. * Sets a default value, label, and setting value.
  27. *
  28. * @param linkage Setting registry
  29. * @param default_val Initial value of the setting, and default value of the setting
  30. * @param name Label for the setting
  31. * @param category_ Category of the setting AKA INI group
  32. * @param specialization_ Suggestion for how frontend implemetations represent this in a config
  33. * @param save_ Suggests that this should or should not be saved to a frontend config file
  34. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  35. * @param other_setting_ A second Setting to associate to this one in metadata
  36. */
  37. explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name,
  38. enum Category category_, u32 specialization_ = Specialization::Default,
  39. bool save_ = true, bool runtime_modifiable_ = false,
  40. BasicSetting* other_setting_ = nullptr)
  41. requires(!ranged)
  42. : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
  43. other_setting_),
  44. value{default_val}, default_value{default_val} {}
  45. virtual ~Setting() = default;
  46. /**
  47. * Sets a default value, minimum value, maximum value, and label.
  48. *
  49. * @param linkage Setting registry
  50. * @param default_val Initial value of the setting, and default value of the setting
  51. * @param min_val Sets the minimum allowed value of the setting
  52. * @param max_val Sets the maximum allowed value of the setting
  53. * @param name Label for the setting
  54. * @param category_ Category of the setting AKA INI group
  55. * @param specialization_ Suggestion for how frontend implemetations represent this in a config
  56. * @param save_ Suggests that this should or should not be saved to a frontend config file
  57. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  58. * @param other_setting_ A second Setting to associate to this one in metadata
  59. */
  60. explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val,
  61. const Type& max_val, const std::string& name, enum Category category_,
  62. u32 specialization_ = Specialization::Default, bool save_ = true,
  63. bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr)
  64. requires(ranged)
  65. : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
  66. other_setting_),
  67. value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {}
  68. /**
  69. * Returns a reference to the setting's value.
  70. *
  71. * @returns A reference to the setting
  72. */
  73. [[nodiscard]] virtual const Type& GetValue() const {
  74. return value;
  75. }
  76. /**
  77. * Sets the setting to the given value.
  78. *
  79. * @param val The desired value
  80. */
  81. virtual void SetValue(const Type& val) {
  82. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  83. std::swap(value, temp);
  84. }
  85. /**
  86. * Returns the value that this setting was created with.
  87. *
  88. * @returns A reference to the default value
  89. */
  90. [[nodiscard]] const Type& GetDefault() const {
  91. return default_value;
  92. }
  93. [[nodiscard]] constexpr bool IsEnum() const override {
  94. return std::is_enum_v<Type>;
  95. }
  96. protected:
  97. [[nodiscard]] std::string ToString(const Type& value_) const {
  98. if constexpr (std::is_same_v<Type, std::string>) {
  99. return value_;
  100. } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
  101. return value_.has_value() ? std::to_string(*value_) : "none";
  102. } else if constexpr (std::is_same_v<Type, bool>) {
  103. return value_ ? "true" : "false";
  104. } else if constexpr (std::is_same_v<Type, AudioEngine>) {
  105. // Compatibility with old AudioEngine setting being a string
  106. return CanonicalizeEnum(value_);
  107. } else {
  108. return std::to_string(static_cast<u64>(value_));
  109. }
  110. }
  111. public:
  112. /**
  113. * Converts the value of the setting to a std::string. Respects the global state if the setting
  114. * has one.
  115. *
  116. * @returns The current setting as a std::string
  117. */
  118. [[nodiscard]] std::string ToString() const override {
  119. return ToString(this->GetValue());
  120. }
  121. /**
  122. * Returns the default value of the setting as a std::string.
  123. *
  124. * @returns The default value as a string.
  125. */
  126. [[nodiscard]] std::string DefaultToString() const override {
  127. return ToString(default_value);
  128. }
  129. /**
  130. * Assigns a value to the setting.
  131. *
  132. * @param val The desired setting value
  133. *
  134. * @returns A reference to the setting
  135. */
  136. virtual const Type& operator=(const Type& val) {
  137. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  138. std::swap(value, temp);
  139. return value;
  140. }
  141. /**
  142. * Returns a reference to the setting.
  143. *
  144. * @returns A reference to the setting
  145. */
  146. explicit virtual operator const Type&() const {
  147. return value;
  148. }
  149. /**
  150. * Converts the given value to the Setting's type of value. Uses SetValue to enter the setting,
  151. * thus respecting its constraints.
  152. *
  153. * @param input The desired value
  154. */
  155. void LoadString(const std::string& input) override final {
  156. if (input.empty()) {
  157. this->SetValue(this->GetDefault());
  158. return;
  159. }
  160. try {
  161. if constexpr (std::is_same_v<Type, std::string>) {
  162. this->SetValue(input);
  163. } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
  164. this->SetValue(static_cast<u32>(std::stoul(input)));
  165. } else if constexpr (std::is_same_v<Type, bool>) {
  166. this->SetValue(input == "true");
  167. } else if constexpr (std::is_same_v<Type, AudioEngine>) {
  168. this->SetValue(ToEnum<Type>(input));
  169. } else {
  170. this->SetValue(static_cast<Type>(std::stoll(input)));
  171. }
  172. } catch (std::invalid_argument&) {
  173. this->SetValue(this->GetDefault());
  174. }
  175. }
  176. [[nodiscard]] std::string constexpr Canonicalize() const override final {
  177. if constexpr (std::is_enum_v<Type>) {
  178. return CanonicalizeEnum(this->GetValue());
  179. } else {
  180. return ToString(this->GetValue());
  181. }
  182. }
  183. /**
  184. * Gives us another way to identify the setting without having to go through a string.
  185. *
  186. * @returns the type_index of the setting's type
  187. */
  188. [[nodiscard]] std::type_index TypeId() const override final {
  189. return std::type_index(typeid(Type));
  190. }
  191. [[nodiscard]] constexpr u32 EnumIndex() const override final {
  192. if constexpr (std::is_enum_v<Type>) {
  193. return EnumMetadata<Type>::Index();
  194. } else {
  195. return std::numeric_limits<u32>::max();
  196. }
  197. }
  198. [[nodiscard]] std::string MinVal() const override final {
  199. return this->ToString(minimum);
  200. }
  201. [[nodiscard]] std::string MaxVal() const override final {
  202. return this->ToString(maximum);
  203. }
  204. [[nodiscard]] constexpr bool Ranged() const override {
  205. return ranged;
  206. }
  207. protected:
  208. Type value{}; ///< The setting
  209. const Type default_value{}; ///< The default value
  210. const Type maximum{}; ///< Maximum allowed value of the setting
  211. const Type minimum{}; ///< Minimum allowed value of the setting
  212. };
  213. /**
  214. * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a
  215. * custom setting to switch to when a guest application specifically requires it. The effect is that
  216. * other components of the emulator can access the setting's intended value without any need for the
  217. * component to ask whether the custom or global setting is needed at the moment.
  218. *
  219. * By default, the global setting is used.
  220. */
  221. template <typename Type, bool ranged = false>
  222. class SwitchableSetting : virtual public Setting<Type, ranged> {
  223. public:
  224. /**
  225. * Sets a default value, label, and setting value.
  226. *
  227. * @param linkage Setting registry
  228. * @param default_val Initial value of the setting, and default value of the setting
  229. * @param name Label for the setting
  230. * @param category_ Category of the setting AKA INI group
  231. * @param specialization_ Suggestion for how frontend implemetations represent this in a config
  232. * @param save_ Suggests that this should or should not be saved to a frontend config file
  233. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  234. * @param other_setting_ A second Setting to associate to this one in metadata
  235. */
  236. explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name,
  237. Category category_, u32 specialization_ = Specialization::Default,
  238. bool save_ = true, bool runtime_modifiable_ = false,
  239. BasicSetting* other_setting_ = nullptr)
  240. requires(!ranged)
  241. : Setting<Type, false>{
  242. linkage, default_val, name, category_, specialization_,
  243. save_, runtime_modifiable_, other_setting_} {
  244. linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
  245. }
  246. virtual ~SwitchableSetting() = default;
  247. /**
  248. * Sets a default value, minimum value, maximum value, and label.
  249. *
  250. * @param linkage Setting registry
  251. * @param default_val Initial value of the setting, and default value of the setting
  252. * @param min_val Sets the minimum allowed value of the setting
  253. * @param max_val Sets the maximum allowed value of the setting
  254. * @param name Label for the setting
  255. * @param category_ Category of the setting AKA INI group
  256. * @param specialization_ Suggestion for how frontend implemetations represent this in a config
  257. * @param save_ Suggests that this should or should not be saved to a frontend config file
  258. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  259. * @param other_setting_ A second Setting to associate to this one in metadata
  260. */
  261. explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val,
  262. const Type& max_val, const std::string& name, Category category_,
  263. u32 specialization_ = Specialization::Default, bool save_ = true,
  264. bool runtime_modifiable_ = false,
  265. BasicSetting* other_setting_ = nullptr)
  266. requires(ranged)
  267. : Setting<Type, true>{linkage, default_val, min_val,
  268. max_val, name, category_,
  269. specialization_, save_, runtime_modifiable_,
  270. other_setting_} {
  271. linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
  272. }
  273. /**
  274. * Tells this setting to represent either the global or custom setting when other member
  275. * functions are used.
  276. *
  277. * @param to_global Whether to use the global or custom setting.
  278. */
  279. void SetGlobal(bool to_global) override final {
  280. use_global = to_global;
  281. }
  282. /**
  283. * Returns whether this setting is using the global setting or not.
  284. *
  285. * @returns The global state
  286. */
  287. [[nodiscard]] bool UsingGlobal() const override final {
  288. return use_global;
  289. }
  290. /**
  291. * Returns either the global or custom setting depending on the values of this setting's global
  292. * state or if the global value was specifically requested.
  293. *
  294. * @param need_global Request global value regardless of setting's state; defaults to false
  295. *
  296. * @returns The required value of the setting
  297. */
  298. [[nodiscard]] const Type& GetValue() const override final {
  299. if (use_global) {
  300. return this->value;
  301. }
  302. return custom;
  303. }
  304. [[nodiscard]] const Type& GetValue(bool need_global) const {
  305. if (use_global || need_global) {
  306. return this->value;
  307. }
  308. return custom;
  309. }
  310. /**
  311. * Sets the current setting value depending on the global state.
  312. *
  313. * @param val The new value
  314. */
  315. void SetValue(const Type& val) override final {
  316. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  317. if (use_global) {
  318. std::swap(this->value, temp);
  319. } else {
  320. std::swap(custom, temp);
  321. }
  322. }
  323. [[nodiscard]] constexpr bool Switchable() const override final {
  324. return true;
  325. }
  326. [[nodiscard]] std::string ToStringGlobal() const override final {
  327. return this->ToString(this->value);
  328. }
  329. /**
  330. * Assigns the current setting value depending on the global state.
  331. *
  332. * @param val The new value
  333. *
  334. * @returns A reference to the current setting value
  335. */
  336. const Type& operator=(const Type& val) override final {
  337. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  338. if (use_global) {
  339. std::swap(this->value, temp);
  340. return this->value;
  341. }
  342. std::swap(custom, temp);
  343. return custom;
  344. }
  345. /**
  346. * Returns the current setting value depending on the global state.
  347. *
  348. * @returns A reference to the current setting value
  349. */
  350. explicit operator const Type&() const override final {
  351. if (use_global) {
  352. return this->value;
  353. }
  354. return custom;
  355. }
  356. protected:
  357. bool use_global{true}; ///< The setting's global state
  358. Type custom{}; ///< The custom value of the setting
  359. };
  360. } // namespace Settings