Common/IniFile: Make use of std::string_view where applicable

Now that the std::map less-than comparitor is capable of being used with
heterogenous lookup, we're able to convert many of the querying
functions that took std::string references over to std::string_view.

Now these functions may be used without potentially allocating a
std::string instance unnecessarily.
This commit is contained in:
Lioncash
2019-06-16 17:28:00 -04:00
parent de7e9557dc
commit 78d1716251
2 changed files with 55 additions and 52 deletions

View File

@ -49,8 +49,8 @@ public:
public:
Section();
explicit Section(std::string name_);
bool Exists(const std::string& key) const;
bool Delete(const std::string& key);
bool Exists(std::string_view key) const;
bool Delete(std::string_view key);
void Set(const std::string& key, std::string new_value);
@ -69,12 +69,11 @@ public:
Delete(key);
}
bool Get(const std::string& key, std::string* value,
bool Get(std::string_view key, std::string* value,
const std::string& default_value = NULL_STRING) const;
template <typename T>
bool Get(const std::string& key, T* value,
const std::common_type_t<T>& default_value = {}) const
bool Get(std::string_view key, T* value, const std::common_type_t<T>& default_value = {}) const
{
std::string temp;
bool retval = Get(key, &temp);
@ -121,41 +120,40 @@ public:
bool Save(const std::string& filename);
// Returns true if key exists in section
bool Exists(const std::string& sectionName, const std::string& key) const;
bool Exists(std::string_view section_name, std::string_view key) const;
template <typename T>
bool GetIfExists(const std::string& sectionName, const std::string& key, T* value)
bool GetIfExists(std::string_view section_name, std::string_view key, T* value)
{
if (Exists(sectionName, key))
return GetOrCreateSection(sectionName)->Get(key, value);
if (Exists(section_name, key))
return GetOrCreateSection(section_name)->Get(key, value);
return false;
}
template <typename T>
bool GetIfExists(const std::string& sectionName, const std::string& key, T* value, T defaultValue)
bool GetIfExists(std::string_view section_name, std::string_view key, T* value, T default_value)
{
if (Exists(sectionName, key))
return GetOrCreateSection(sectionName)->Get(key, value, defaultValue);
else
*value = defaultValue;
if (Exists(section_name, key))
return GetOrCreateSection(section_name)->Get(key, value, default_value);
*value = default_value;
return false;
}
bool GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const;
bool GetKeys(std::string_view section_name, std::vector<std::string>* keys) const;
void SetLines(const std::string& sectionName, const std::vector<std::string>& lines);
void SetLines(const std::string& section_name, std::vector<std::string>&& lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>* lines,
const bool remove_comments = true) const;
void SetLines(std::string_view section_name, const std::vector<std::string>& lines);
void SetLines(std::string_view section_name, std::vector<std::string>&& lines);
bool GetLines(std::string_view section_name, std::vector<std::string>* lines,
bool remove_comments = true) const;
bool DeleteKey(const std::string& sectionName, const std::string& key);
bool DeleteSection(const std::string& sectionName);
bool DeleteKey(std::string_view section_name, std::string_view key);
bool DeleteSection(std::string_view section_name);
void SortSections();
Section* GetOrCreateSection(const std::string& section);
Section* GetOrCreateSection(std::string_view section_name);
// This function is related to parsing data from lines of INI files
// It's used outside of IniFile, which is why it is exposed publicly
@ -167,8 +165,8 @@ public:
private:
std::list<Section> sections;
const Section* GetSection(const std::string& section) const;
Section* GetSection(const std::string& section);
const Section* GetSection(std::string_view section_name) const;
Section* GetSection(std::string_view section_name);
static const std::string& NULL_STRING;
};