SettingsInterface: Add SmallString overloads

Backport of 631fca3042
This commit is contained in:
Stenzek 2024-04-12 23:39:43 +10:00 committed by Connor McLaughlin
parent 9fdd609add
commit 35525d5304
9 changed files with 115 additions and 4 deletions

View File

@ -124,6 +124,20 @@ bool MemorySettingsInterface::GetStringValue(const char* section, const char* ke
return true;
}
bool MemorySettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const
{
const auto sit = m_sections.find(section);
if (sit == m_sections.end())
return false;
const auto iter = sit->second.find(key);
if (iter == sit->second.end())
return false;
value->assign(iter->second);
return true;
}
void MemorySettingsInterface::SetIntValue(const char* section, const char* key, s32 value)
{
SetValue(section, key, std::to_string(value));

View File

@ -22,6 +22,7 @@ public:
bool GetDoubleValue(const char* section, const char* key, double* value) const override;
bool GetBoolValue(const char* section, const char* key, bool* value) const override;
bool GetStringValue(const char* section, const char* key, std::string* value) const override;
bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override;
void SetIntValue(const char* section, const char* key, s32 value) override;
void SetUIntValue(const char* section, const char* key, u32 value) override;

View File

@ -4,6 +4,7 @@
#pragma once
#include "Pcsx2Defs.h"
#include "SmallString.h"
#include <string>
#include <optional>
@ -25,6 +26,7 @@ public:
virtual bool GetDoubleValue(const char* section, const char* key, double* value) const = 0;
virtual bool GetBoolValue(const char* section, const char* key, bool* value) const = 0;
virtual bool GetStringValue(const char* section, const char* key, std::string* value) const = 0;
virtual bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const = 0;
virtual void SetIntValue(const char* section, const char* key, int value) = 0;
virtual void SetUIntValue(const char* section, const char* key, uint value) = 0;
@ -83,6 +85,22 @@ public:
return value;
}
__fi SmallString GetSmallStringValue(const char* section, const char* key, const char* default_value = "") const
{
SmallString value;
if (!GetStringValue(section, key, &value))
value.assign(default_value);
return value;
}
__fi SmallString GetTinyStringValue(const char* section, const char* key, const char* default_value = "") const
{
TinyString value;
if (!GetStringValue(section, key, &value))
value.assign(default_value);
return value;
}
__fi std::optional<int> GetOptionalIntValue(const char* section, const char* key, std::optional<int> default_value = std::nullopt) const
{
int ret;
@ -116,7 +134,29 @@ public:
__fi std::optional<std::string> GetOptionalStringValue(const char* section, const char* key, std::optional<const char*> default_value = std::nullopt) const
{
std::string ret;
return GetStringValue(section, key, &ret) ? std::optional<std::string>(ret) : default_value;
return GetStringValue(section, key, &ret) ? std::optional<std::string>(ret) :
(default_value.has_value() ? std::optional<std::string>(default_value.value()) :
std::optional<std::string>());
}
__fi std::optional<SmallString> GetOptionalSmallStringValue(const char* section, const char* key,
std::optional<const char*> default_value = std::nullopt) const
{
SmallString ret;
return GetStringValue(section, key, &ret) ?
std::optional<SmallString>(ret) :
(default_value.has_value() ? std::optional<SmallString>(default_value.value()) :
std::optional<SmallString>());
}
__fi std::optional<TinyString> GetOptionalTinyStringValue(const char* section, const char* key,
std::optional<const char*> default_value = std::nullopt) const
{
TinyString ret;
return GetStringValue(section, key, &ret) ?
std::optional<TinyString>(ret) :
(default_value.has_value() ? std::optional<TinyString>(default_value.value()) :
std::optional<TinyString>());
}
__fi void SetOptionalIntValue(const char* section, const char* key, const std::optional<int>& value)

View File

@ -185,6 +185,20 @@ std::string Host::GetBaseStringSettingValue(const char* section, const char* key
->GetStringValue(section, key, default_value);
}
SmallString Host::GetBaseSmallStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{
std::unique_lock lock(s_settings_mutex);
return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)
->GetSmallStringValue(section, key, default_value);
}
TinyString Host::GetBaseTinyStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{
std::unique_lock lock(s_settings_mutex);
return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)
->GetTinyStringValue(section, key, default_value);
}
bool Host::GetBaseBoolSettingValue(const char* section, const char* key, bool default_value /*= false*/)
{
std::unique_lock lock(s_settings_mutex);
@ -288,6 +302,18 @@ void Host::RemoveBaseSettingValue(const char* section, const char* key)
s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->DeleteValue(section, key);
}
SmallString Host::GetSmallStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{
std::unique_lock lock(s_settings_mutex);
return s_layered_settings_interface.GetSmallStringValue(section, key, default_value);
}
TinyString Host::GetTinyStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{
std::unique_lock lock(s_settings_mutex);
return s_layered_settings_interface.GetTinyStringValue(section, key, default_value);
}
std::string Host::GetStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{
std::unique_lock lock(s_settings_mutex);

View File

@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: LGPL-3.0+
#pragma once
#include "common/Pcsx2Defs.h"
#include "common/SmallString.h"
#include "fmt/format.h"
@ -92,6 +93,8 @@ namespace Host
/// Base setting retrieval, bypasses layers.
std::string GetBaseStringSettingValue(const char* section, const char* key, const char* default_value = "");
SmallString GetBaseSmallStringSettingValue(const char* section, const char* key, const char* default_value = "");
TinyString GetBaseTinyStringSettingValue(const char* section, const char* key, const char* default_value = "");
bool GetBaseBoolSettingValue(const char* section, const char* key, bool default_value = false);
int GetBaseIntSettingValue(const char* section, const char* key, int default_value = 0);
uint GetBaseUIntSettingValue(const char* section, const char* key, uint default_value = 0);
@ -115,6 +118,8 @@ namespace Host
/// Settings access, thread-safe.
std::string GetStringSettingValue(const char* section, const char* key, const char* default_value = "");
SmallString GetSmallStringSettingValue(const char* section, const char* key, const char* default_value = "");
TinyString GetTinyStringSettingValue(const char* section, const char* key, const char* default_value = "");
bool GetBoolSettingValue(const char* section, const char* key, bool default_value = false);
int GetIntSettingValue(const char* section, const char* key, int default_value = 0);
uint GetUIntSettingValue(const char* section, const char* key, uint default_value = 0);

View File

@ -196,6 +196,16 @@ bool INISettingsInterface::GetStringValue(const char* section, const char* key,
return true;
}
bool INISettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const
{
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
value->assign(str_value);
return true;
}
void INISettingsInterface::SetIntValue(const char* section, const char* key, int value)
{
m_dirty = true;
@ -314,8 +324,7 @@ std::vector<std::pair<std::string, std::string>> INISettingsInterface::GetKeyVal
entries.emplace_back(key.pItem, value);
}
}
std::sort(entries.begin(), entries.end(), [](const KVEntry& a, const KVEntry& b)
{
std::sort(entries.begin(), entries.end(), [](const KVEntry& a, const KVEntry& b) {
return a.second.nOrder < b.second.nOrder;
});
for (const KVEntry& entry : entries)

View File

@ -30,6 +30,7 @@ public:
bool GetDoubleValue(const char* section, const char* key, double* value) const override;
bool GetBoolValue(const char* section, const char* key, bool* value) const override;
bool GetStringValue(const char* section, const char* key, std::string* value) const override;
bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override;
void SetIntValue(const char* section, const char* key, int value) override;
void SetUIntValue(const char* section, const char* key, uint value) override;

View File

@ -106,6 +106,20 @@ bool LayeredSettingsInterface::GetStringValue(const char* section, const char* k
return false;
}
bool LayeredSettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetStringValue(section, key, value))
return true;
}
}
return false;
}
void LayeredSettingsInterface::SetIntValue(const char* section, const char* key, int value)
{
pxFailRel("Attempt to call SetIntValue() on layered settings interface");

View File

@ -35,6 +35,7 @@ public:
bool GetDoubleValue(const char* section, const char* key, double* value) const override;
bool GetBoolValue(const char* section, const char* key, bool* value) const override;
bool GetStringValue(const char* section, const char* key, std::string* value) const override;
bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override;
void SetIntValue(const char* section, const char* key, int value) override;
void SetUIntValue(const char* section, const char* key, uint value) override;