From 7fae0f499fefaf7025076d7d8c76ed7d5a4c30f8 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 24 Apr 2024 01:17:08 +1000 Subject: [PATCH] SettingsWrapper: Add SmallString overloads --- common/SettingsWrapper.cpp | 16 ++++++++++++++++ common/SettingsWrapper.h | 26 ++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/common/SettingsWrapper.cpp b/common/SettingsWrapper.cpp index 880e4d18af..1c81430c63 100644 --- a/common/SettingsWrapper.cpp +++ b/common/SettingsWrapper.cpp @@ -64,6 +64,12 @@ void SettingsLoadWrapper::Entry(const char* section, const char* var, std::strin value = default_value; } +void SettingsLoadWrapper::Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value /* = std::string_view() */) +{ + if (!m_si.GetStringValue(section, var, &value) && value.data() != default_value.data()) + value = default_value; +} + void SettingsLoadWrapper::_EnumEntry(const char* section, const char* var, int& value, const char* const* enumArray, int defvalue) { const int cnt = _calcEnumLength(enumArray); @@ -137,6 +143,11 @@ void SettingsSaveWrapper::Entry(const char* section, const char* var, std::strin m_si.SetStringValue(section, var, value.c_str()); } +void SettingsSaveWrapper::Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value /* = std::string_view() */) +{ + m_si.SetStringValue(section, var, value.c_str()); +} + bool SettingsSaveWrapper::EntryBitBool(const char* section, const char* var, bool value, const bool defvalue /*= false*/) { m_si.SetBoolValue(section, var, value); @@ -196,6 +207,11 @@ void SettingsClearWrapper::Entry(const char* section, const char* var, std::stri m_si.DeleteValue(section, var); } +void SettingsClearWrapper::Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value /* = std::string_view() */) +{ + m_si.DeleteValue(section, var); +} + bool SettingsClearWrapper::EntryBitBool(const char* section, const char* var, bool value, const bool defvalue /*= false*/) { m_si.DeleteValue(section, var); diff --git a/common/SettingsWrapper.h b/common/SettingsWrapper.h index 23068744d1..e32cf9fd90 100644 --- a/common/SettingsWrapper.h +++ b/common/SettingsWrapper.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once @@ -6,6 +6,9 @@ #include "SettingsInterface.h" #include "common/EnumOps.h" +#include "common/SmallString.h" + +#include // Helper class which loads or saves depending on the derived class. class SettingsWrapper @@ -21,6 +24,7 @@ public: virtual void Entry(const char* section, const char* var, bool& value, const bool defvalue = false) = 0; virtual void Entry(const char* section, const char* var, float& value, const float defvalue = 0.0) = 0; virtual void Entry(const char* section, const char* var, std::string& value, const std::string& default_value = std::string()) = 0; + virtual void Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value = std::string_view()) = 0; // This special form of Entry is provided for bitfields, which cannot be passed by reference. virtual bool EntryBitBool(const char* section, const char* var, bool value, const bool defvalue = false) = 0; @@ -38,6 +42,16 @@ public: value = (T)tstore; } + template + void EnumEntry(const char* section, const char* var, T& value, std::optional (*parse_function)(const char*), + const char*(name_function)(T value), T default_value) + { + TinyString str_value(name_function(value)); + Entry(section, var, str_value, name_function(default_value)); + if (std::optional parsed_value = parse_function(str_value); parsed_value.has_value()) + value = parsed_value.value(); + } + protected: virtual void _EnumEntry(const char* section, const char* var, int& value, const char* const* enumArray, int defvalue) = 0; @@ -56,8 +70,9 @@ public: void Entry(const char* section, const char* var, uint& value, const uint defvalue = 0) override; void Entry(const char* section, const char* var, bool& value, const bool defvalue = false) override; void Entry(const char* section, const char* var, float& value, const float defvalue = 0.0) override; - void Entry(const char* section, const char* var, std::string& value, const std::string& default_value = std::string()) override; + void Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value = std::string_view()) override; + bool EntryBitBool(const char* section, const char* var, bool value, const bool defvalue = false) override; int EntryBitfield(const char* section, const char* var, int value, const int defvalue = 0) override; @@ -77,8 +92,9 @@ public: void Entry(const char* section, const char* var, uint& value, const uint defvalue = 0) override; void Entry(const char* section, const char* var, bool& value, const bool defvalue = false) override; void Entry(const char* section, const char* var, float& value, const float defvalue = 0.0) override; - void Entry(const char* section, const char* var, std::string& value, const std::string& default_value = std::string()) override; + void Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value = std::string_view()) override; + bool EntryBitBool(const char* section, const char* var, bool value, const bool defvalue = false) override; int EntryBitfield(const char* section, const char* var, int value, const int defvalue = 0) override; @@ -98,8 +114,9 @@ public: void Entry(const char* section, const char* var, uint& value, const uint defvalue = 0) override; void Entry(const char* section, const char* var, bool& value, const bool defvalue = false) override; void Entry(const char* section, const char* var, float& value, const float defvalue = 0.0) override; - void Entry(const char* section, const char* var, std::string& value, const std::string& default_value = std::string()) override; + void Entry(const char* section, const char* var, SmallStringBase& value, std::string_view default_value = std::string_view()) override; + bool EntryBitBool(const char* section, const char* var, bool value, const bool defvalue = false) override; int EntryBitfield(const char* section, const char* var, int value, const int defvalue = 0) override; @@ -115,5 +132,6 @@ protected: #define SettingsWrapBitfieldEx(varname, textname) varname = wrap.EntryBitfield(CURRENT_SETTINGS_SECTION, textname, varname, varname) #define SettingsWrapBitBoolEx(varname, textname) varname = wrap.EntryBitBool(CURRENT_SETTINGS_SECTION, textname, !!varname, varname) #define SettingsWrapEnumEx(varname, textname, names) wrap.EnumEntry(CURRENT_SETTINGS_SECTION, textname, varname, names, varname) +#define SettingsWrapParsedEnum(varname, textname, parse_func, name_func) wrap.EnumEntry(CURRENT_SETTINGS_SECTION, textname, varname, parse_func, name_func, varname) #define SettingsWrapIntEnumEx(varname, textname) varname = static_cast(wrap.EntryBitfield(CURRENT_SETTINGS_SECTION, textname, static_cast(varname), static_cast(varname)))