Merge pull request #9393 from lioncash/sysconf

SysConf: Make use of std::string_view
This commit is contained in:
Léo Lam 2020-12-30 01:22:39 +01:00 committed by GitHub
commit 806a4f3a9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 20 deletions

View File

@ -211,45 +211,45 @@ bool SysConf::Save() const
return result == IOS::HLE::FS::ResultCode::Success; return result == IOS::HLE::FS::ResultCode::Success;
} }
SysConf::Entry::Entry(Type type_, const std::string& name_) : type(type_), name(name_) SysConf::Entry::Entry(Type type_, std::string name_) : type(type_), name(std::move(name_))
{ {
if (type != Type::SmallArray && type != Type::BigArray) if (type != Type::SmallArray && type != Type::BigArray)
bytes.resize(GetNonArrayEntrySize(type)); bytes.resize(GetNonArrayEntrySize(type));
} }
SysConf::Entry::Entry(Type type_, const std::string& name_, std::vector<u8> bytes_) SysConf::Entry::Entry(Type type_, std::string name_, std::vector<u8> bytes_)
: type(type_), name(name_), bytes(std::move(bytes_)) : type(type_), name(std::move(name_)), bytes(std::move(bytes_))
{ {
} }
void SysConf::AddEntry(Entry&& entry) SysConf::Entry& SysConf::AddEntry(Entry&& entry)
{ {
m_entries.emplace_back(std::move(entry)); return m_entries.emplace_back(std::move(entry));
} }
SysConf::Entry* SysConf::GetEntry(const std::string& key) SysConf::Entry* SysConf::GetEntry(std::string_view key)
{ {
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
[&key](const auto& entry) { return entry.name == key; }); [&key](const auto& entry) { return entry.name == key; });
return iterator != m_entries.end() ? &*iterator : nullptr; return iterator != m_entries.end() ? &*iterator : nullptr;
} }
const SysConf::Entry* SysConf::GetEntry(const std::string& key) const const SysConf::Entry* SysConf::GetEntry(std::string_view key) const
{ {
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
[&key](const auto& entry) { return entry.name == key; }); [&key](const auto& entry) { return entry.name == key; });
return iterator != m_entries.end() ? &*iterator : nullptr; return iterator != m_entries.end() ? &*iterator : nullptr;
} }
SysConf::Entry* SysConf::GetOrAddEntry(const std::string& key, Entry::Type type) SysConf::Entry* SysConf::GetOrAddEntry(std::string_view key, Entry::Type type)
{ {
if (Entry* entry = GetEntry(key)) if (Entry* entry = GetEntry(key))
return entry; return entry;
AddEntry({type, key});
return GetEntry(key); return &AddEntry({type, std::string(key)});
} }
void SysConf::RemoveEntry(const std::string& key) void SysConf::RemoveEntry(std::string_view key)
{ {
m_entries.erase(std::remove_if(m_entries.begin(), m_entries.end(), m_entries.erase(std::remove_if(m_entries.begin(), m_entries.end(),
[&key](const auto& entry) { return entry.name == key; }), [&key](const auto& entry) { return entry.name == key; }),

View File

@ -9,6 +9,7 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "Common/Assert.h" #include "Common/Assert.h"
@ -46,8 +47,8 @@ public:
ByteBool = 7, ByteBool = 7,
}; };
Entry(Type type_, const std::string& name_); Entry(Type type_, std::string name_);
Entry(Type type_, const std::string& name_, std::vector<u8> bytes_); Entry(Type type_, std::string name_, std::vector<u8> bytes_);
// Intended for use with the non array types. // Intended for use with the non array types.
template <typename T> template <typename T>
@ -74,21 +75,21 @@ public:
std::vector<u8> bytes; std::vector<u8> bytes;
}; };
void AddEntry(Entry&& entry); Entry& AddEntry(Entry&& entry);
Entry* GetEntry(const std::string& key); Entry* GetEntry(std::string_view key);
const Entry* GetEntry(const std::string& key) const; const Entry* GetEntry(std::string_view key) const;
Entry* GetOrAddEntry(const std::string& key, Entry::Type type); Entry* GetOrAddEntry(std::string_view key, Entry::Type type);
void RemoveEntry(const std::string& key); void RemoveEntry(std::string_view key);
// Intended for use with the non array types. // Intended for use with the non array types.
template <typename T> template <typename T>
T GetData(const std::string& key, T default_value) const T GetData(std::string_view key, T default_value) const
{ {
const Entry* entry = GetEntry(key); const Entry* entry = GetEntry(key);
return entry ? entry->GetData(default_value) : default_value; return entry ? entry->GetData(default_value) : default_value;
} }
template <typename T> template <typename T>
void SetData(const std::string& key, Entry::Type type, T value) void SetData(std::string_view key, Entry::Type type, T value)
{ {
GetOrAddEntry(key, type)->SetData(value); GetOrAddEntry(key, type)->SetData(value);
} }