settings_setting.h 14 KB

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