Improve the overload resolution in IniFile

This commit is contained in:
Henrik Rydgård 2025-10-31 15:43:52 +01:00
parent bb3e599f4e
commit c98b7591d3
6 changed files with 35 additions and 36 deletions

View File

@ -217,35 +217,35 @@ const ParsedIniLine *Section::GetLine(std::string_view key) const {
void Section::Set(std::string_view key, uint32_t newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "0x%08x", newValue);
Set(key, (const char *)temp);
Set(key, std::string_view(temp));
}
void Section::Set(std::string_view key, uint64_t newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "0x%016" PRIx64, newValue);
Set(key, (const char *)temp);
Set(key, std::string_view(temp));
}
void Section::Set(std::string_view key, float newValue) {
_dbg_assert_(!my_isnanorinf(newValue));
char temp[128];
char temp[64];
snprintf(temp, sizeof(temp), "%f", newValue);
Set(key, (const char *)temp);
Set(key, std::string_view(temp));
}
void Section::Set(std::string_view key, double newValue) {
char temp[128];
char temp[64];
snprintf(temp, sizeof(temp), "%f", newValue);
Set(key, (const char *)temp);
Set(key, std::string_view(temp));
}
void Section::Set(std::string_view key, int newValue) {
char temp[128];
char temp[32];
snprintf(temp, sizeof(temp), "%d", newValue);
Set(key, (const char *)temp);
Set(key, std::string_view(temp));
}
void Section::Set(std::string_view key, const char* newValue) {
void Section::Set(std::string_view key, std::string_view newValue) {
ParsedIniLine *line = GetLine(key);
if (line) {
line->SetValue(newValue);
@ -255,8 +255,7 @@ void Section::Set(std::string_view key, const char* newValue) {
}
}
void Section::Set(std::string_view key, const std::string& newValue, const std::string& defaultValue)
{
void Section::Set(std::string_view key, std::string_view newValue, std::string_view defaultValue) {
if (newValue != defaultValue)
Set(key, newValue);
else
@ -313,12 +312,12 @@ void Section::Set(std::string_view key, const std::vector<std::string> &newValue
Set(key, temp.c_str());
}
bool Section::Get(std::string_view key, std::vector<std::string> *values, const std::vector<std::string> *defaultValues) const {
bool Section::Get(std::string_view key, std::vector<std::string> *values, const std::vector<std::string_view> *defaultValues) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (!retval || temp.empty()) {
if (defaultValues) {
*values = *defaultValues;
CopyStrings(values, *defaultValues);
}
return false;
}
@ -389,8 +388,8 @@ bool Section::Exists(std::string_view key) const {
return false;
}
void Section::AddComment(const std::string &comment) {
lines_.emplace_back(ParsedIniLine::CommentOnly("# " + comment));
void Section::AddComment(std::string_view comment) {
lines_.emplace_back(ParsedIniLine::CommentOnly("# " + std::string(comment)));
}
std::map<std::string, std::string> Section::ToMap() const {
@ -593,7 +592,7 @@ bool IniFile::Get(std::string_view sectionName, std::string_view key, std::strin
return section->Get(key, value, defaultValue);
}
bool IniFile::Get(std::string_view sectionName, std::string_view key, std::vector<std::string> *values, const std::vector<std::string> *defaultValues) {
bool IniFile::Get(std::string_view sectionName, std::string_view key, std::vector<std::string> *values, const std::vector<std::string_view> *defaultValues) {
Section *section = GetSection(sectionName);
if (!section)
return false;

View File

@ -65,13 +65,10 @@ public:
ParsedIniLine *GetLine(std::string_view key);
const ParsedIniLine *GetLine(std::string_view key) const;
void Set(std::string_view key, const char* newValue);
void Set(std::string_view key, const std::string& newValue, const std::string& defaultValue);
void Set(std::string_view key, std::string_view newValue);
void Set(std::string_view key, std::string_view newValue, std::string_view defaultValue);
void Set(std::string_view key, const std::string &value) {
Set(key, value.c_str());
}
bool Get(std::string_view key, std::string* value, const char* defaultValue) const;
bool Get(std::string_view key, std::string *value, const char* defaultValue) const;
void Set(std::string_view key, uint32_t newValue);
void Set(std::string_view key, uint64_t newValue);
@ -83,18 +80,13 @@ public:
void Set(std::string_view key, int newValue);
void Set(std::string_view key, bool newValue, bool defaultValue);
void Set(std::string_view key, const char *newValue) { Set(key, std::string_view{newValue}); }
void Set(std::string_view key, bool newValue) {
Set(key, newValue ? "True" : "False");
Set(key, std::string_view(newValue ? "True" : "False"));
}
void Set(std::string_view key, const std::vector<std::string>& newValues);
// Declare without a body to make it fail to compile. This is to prevent accidentally
// setting a pointer as a bool. The failure is in the linker unfortunately, but that's better
// than accidentally succeeding in a bad way.
template<class T>
void Set(std::string_view key, T *ptr);
void AddComment(const std::string &comment);
void AddComment(std::string_view comment);
bool Get(std::string_view key, int* value, int defaultValue = 0) const;
bool Get(std::string_view key, uint32_t* value, uint32_t defaultValue = 0) const;
@ -102,7 +94,7 @@ public:
bool Get(std::string_view key, bool* value, bool defaultValue = false) const;
bool Get(std::string_view key, float* value, float defaultValue = false) const;
bool Get(std::string_view key, double* value, double defaultValue = false) const;
bool Get(std::string_view key, std::vector<std::string> *values, const std::vector<std::string> *defaultValue = nullptr) const;
bool Get(std::string_view key, std::vector<std::string> *values, const std::vector<std::string_view> *defaultValue = nullptr) const;
// Return a list of all keys in this section
bool GetKeys(std::vector<std::string> &keys) const;
@ -143,7 +135,7 @@ public:
bool Get(std::string_view sectionName, std::string_view key, uint32_t* value, uint32_t defaultValue = 0);
bool Get(std::string_view sectionName, std::string_view key, uint64_t* value, uint64_t defaultValue = 0);
bool Get(std::string_view sectionName, std::string_view key, bool* value, bool defaultValue = false);
bool Get(std::string_view sectionName, std::string_view key, std::vector<std::string> *values, const std::vector<std::string> *defaultValues = nullptr);
bool Get(std::string_view sectionName, std::string_view key, std::vector<std::string> *values, const std::vector<std::string_view> *defaultValues = nullptr);
bool GetKeys(std::string_view sectionName, std::vector<std::string>& keys) const;

View File

@ -152,6 +152,14 @@ inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
va_end(args);
}
inline void CopyStrings(std::vector<std::string> *output, const std::vector<std::string_view> &input) {
output->clear();
output->reserve(input.size());
for (auto str : input) {
output->emplace_back(str);
}
}
void MakeUnique(std::vector<std::string> &vec);
size_t SplitSearch(std::string_view needle, std::string_view part1, std::string_view part2);

View File

@ -76,7 +76,7 @@ static const char * const logSectionName = "Log";
bool TryUpdateSavedPath(Path *path);
static const std::vector<std::string> defaultProAdhocServerList = {
static const std::vector<std::string_view> defaultProAdhocServerList = {
"socom.cc", "psp.gameplayer.club", // TODO: Add some saved recent history too?
};

View File

@ -234,7 +234,7 @@ bool ConfigSetting::RestoreToDefault(const char *owner, bool log) const {
case Type::TYPE_STRING_VECTOR:
{
std::vector<std::string> *ptr_vec = (std::vector<std::string> *)(owner + offset_);
*ptr_vec = *default_.v;
CopyStrings(ptr_vec, *default_.v);
break;
}
case Type::TYPE_TOUCH_POS:

View File

@ -38,7 +38,7 @@ struct ConfigSetting {
float f;
const char *s;
const char *p; // not sure how much point..
const std::vector<std::string> *v;
const std::vector<std::string_view> *v;
ConfigTouchPos touchPos;
ConfigCustomButton customButton;
};
@ -119,7 +119,7 @@ struct ConfigSetting {
default_.s = def;
}
ConfigSetting(std::string_view ini, const char *owner, std::vector<std::string> *v, const std::vector<std::string> *def, CfgFlag flags) noexcept
ConfigSetting(std::string_view ini, const char *owner, std::vector<std::string> *v, const std::vector<std::string_view> *def, CfgFlag flags) noexcept
: iniKey_(ini), type_(Type::TYPE_STRING_VECTOR), flags_(flags), offset_((const char *)v - owner) {
default_.v = def;
}