IniFile: Use templates for Get()

Gets rid of duplicated code.
This commit is contained in:
Léo Lam 2018-06-03 14:39:38 +02:00
parent cba32b12e7
commit 7e79bf97ab
2 changed files with 11 additions and 91 deletions

View File

@ -110,90 +110,6 @@ bool IniFile::Section::Get(const std::string& key, std::vector<std::string>* out
return true; return true;
} }
bool IniFile::Section::Get(const std::string& key, int* value, int defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Get(const std::string& key, s64* value, s64 default_value) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Get(const std::string& key, u64* value, u64 default_value) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
bool IniFile::Section::Get(const std::string& key, bool* value, bool defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Get(const std::string& key, float* value, float defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Get(const std::string& key, double* value, double defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Exists(const std::string& key) const bool IniFile::Section::Exists(const std::string& key) const
{ {
return values.find(key) != values.end(); return values.find(key) != values.end();

View File

@ -55,13 +55,17 @@ public:
bool Get(const std::string& key, std::string* value, bool Get(const std::string& key, std::string* value,
const std::string& defaultValue = NULL_STRING) const; const std::string& defaultValue = NULL_STRING) const;
bool Get(const std::string& key, int* value, int defaultValue = 0) const; template <typename T>
bool Get(const std::string& key, s64* value, s64 default_value = 0) const; bool Get(const std::string& key, T* value,
bool Get(const std::string& key, u32* value, u32 defaultValue = 0) const; const std::common_type_t<T>& default_value = {}) const
bool Get(const std::string& key, u64* value, u64 default_value = 0) const; {
bool Get(const std::string& key, bool* value, bool defaultValue = false) const; std::string temp;
bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const; bool retval = Get(key, &temp);
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
bool Get(const std::string& key, std::vector<std::string>* values) const; bool Get(const std::string& key, std::vector<std::string>* values) const;
void SetLines(const std::vector<std::string>& lines); void SetLines(const std::vector<std::string>& lines);