config: use string_view, added method to get value pointer

This commit is contained in:
Hyper 2024-11-15 13:43:14 +00:00
parent 2024dc46ce
commit cd1f9742f5
4 changed files with 34 additions and 27 deletions

View file

@ -18,7 +18,7 @@ void Config::Load()
{
def->ReadValue(toml);
#if _DEBUG
printf("%s\n", def->GetDefinition().c_str());
printf("%s (0x%llx)\n", def->GetDefinition().c_str(), def->GetValue());
#endif
}
}

View file

@ -5,7 +5,7 @@
class Config
{
public:
inline static std::vector<ConfigDefBase*> Definitions{};
inline static std::vector<IConfigDef*> Definitions{};
CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English);
CONFIG_DEFINE("System", bool, Hints, true);

View file

@ -30,24 +30,23 @@
#define WINDOWPOS_CENTRED 0x2FFF0000
class ConfigDefBase
class IConfigDef
{
public:
virtual ~ConfigDefBase() = default;
virtual ~IConfigDef() = default;
virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0;
virtual void MakeDefault() = 0;
virtual std::string GetSection() const = 0;
virtual std::string GetName() const = 0;
virtual bool IsMenuOption() const = 0;
virtual std::string_view GetSection() const = 0;
virtual std::string_view GetName() const = 0;
virtual void* GetValue() = 0;
virtual std::string GetDefinition(bool withSection = false) const = 0;
virtual std::string ToString() const = 0;
};
template<typename T, bool isMenuOption = true>
class ConfigDef : public ConfigDefBase
class ConfigDef : public IConfigDef
{
protected:
bool m_isMenuOption{ isMenuOption };
public:
std::string Section{};
std::string Name{};
@ -123,16 +122,26 @@ public:
Value = DefaultValue;
}
std::string GetSection() const override
bool IsMenuOption() const override
{
return isMenuOption;
}
std::string_view GetSection() const override
{
return Section;
}
std::string GetName() const override
std::string_view GetName() const override
{
return Name;
}
void* GetValue() override
{
return &Value;
}
std::string GetDefinition(bool withSection = false) const override
{
std::string result;
@ -147,27 +156,25 @@ public:
std::string ToString() const override
{
if constexpr (std::is_same<T, std::string>::value)
std::string result = "\"N/A\"";
if constexpr (std::is_same_v<T, std::string>)
{
return std::format("\"{}\"", Value);
result = std::format("\"{}\"", Value);
}
else if constexpr (std::is_enum_v<T>)
{
auto it = EnumTemplateReverse.find(Value);
if (it != EnumTemplateReverse.end())
{
return std::format("\"{}\"", it->second);
}
else
{
return "\"N/A\"";
}
result = std::format("\"{}\"", it->second);
}
else
{
return std::format("{}", Value);
result = std::format("{}", Value);
}
return result;
}
ConfigDef& operator=(const ConfigDef& other)

View file

@ -311,7 +311,7 @@ static void DrawConfigOptions()
for (auto& config : Config::Definitions)
{
if (_stricmp(config->GetSection().c_str(), CATEGORIES[g_categoryIndex]) != 0)
if (_stricmp(config->GetSection().data(), CATEGORIES[g_categoryIndex]) != 0)
continue;
// Left side
@ -322,9 +322,9 @@ static void DrawConfigOptions()
drawList->AddRectFilledMultiColor(min, max, COLOR0, COLOR0, COLOR1, COLOR1);
float size = Scale(26.0f);
auto textSize = g_seuratFont->CalcTextSizeA(size, FLT_MAX, 0.0f, config->GetName().c_str());
auto textSize = g_seuratFont->CalcTextSizeA(size, FLT_MAX, 0.0f, config->GetName().data());
drawList->AddText(g_seuratFont, size, { min.x + gridSize, min.y + (optionHeight - textSize.y) / 2.0f }, IM_COL32_WHITE, config->GetName().c_str());
drawList->AddText(g_seuratFont, size, { min.x + gridSize, min.y + (optionHeight - textSize.y) / 2.0f }, IM_COL32_WHITE, config->GetName().data());
// Right side
min = { max.x + (clipRectMax.x - max.x - valueWidth) / 2.0f, min.y + (optionHeight - valueHeight) / 2.0f };
@ -342,7 +342,7 @@ static void DrawConfigOptions()
std::transform(valueText.begin(), valueText.end(), valueText.begin(), toupper);
size = Scale(20.0f);
textSize = g_newRodinFont->CalcTextSizeA(size, FLT_MAX, 0.0f, valueText.c_str());
textSize = g_newRodinFont->CalcTextSizeA(size, FLT_MAX, 0.0f, valueText.data());
min.x += ((max.x - min.x) - textSize.x) / 2.0f;
min.y += ((max.y - min.y) - textSize.y) / 2.0f;
@ -359,7 +359,7 @@ static void DrawConfigOptions()
size,
min,
IM_COL32_WHITE,
valueText.c_str(),
valueText.data(),
Scale(2),
IM_COL32_BLACK);