Skip to content

Commit

Permalink
SysConf: Make use of std::string_view
Browse files Browse the repository at this point in the history
We can allow strings to be used with the SysConf interface in
potentially non-allocating manners.
  • Loading branch information
lioncash committed Dec 30, 2020
1 parent 3b2e312 commit 74224c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions Source/Core/Core/SysConf.cpp
Expand Up @@ -227,29 +227,29 @@ void SysConf::AddEntry(Entry&& entry)
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(),
[&key](const auto& entry) { return entry.name == key; });
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(),
[&key](const auto& entry) { return entry.name == key; });
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))
return entry;
AddEntry({type, key});
AddEntry({type, std::string(key)});
return GetEntry(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(),
[&key](const auto& entry) { return entry.name == key; }),
Expand Down
13 changes: 7 additions & 6 deletions Source/Core/Core/SysConf.h
Expand Up @@ -9,6 +9,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "Common/Assert.h"
Expand Down Expand Up @@ -75,20 +76,20 @@ class SysConf final
};

void AddEntry(Entry&& entry);
Entry* GetEntry(const std::string& key);
const Entry* GetEntry(const std::string& key) const;
Entry* GetOrAddEntry(const std::string& key, Entry::Type type);
void RemoveEntry(const std::string& key);
Entry* GetEntry(std::string_view key);
const Entry* GetEntry(std::string_view key) const;
Entry* GetOrAddEntry(std::string_view key, Entry::Type type);
void RemoveEntry(std::string_view key);

// Intended for use with the non array types.
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);
return entry ? entry->GetData(default_value) : default_value;
}
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);
}
Expand Down

0 comments on commit 74224c9

Please sign in to comment.