Skip to content

Commit

Permalink
Change Layer code not to create superfluous std::optional entries in …
Browse files Browse the repository at this point in the history
…LayerMap
  • Loading branch information
CookiePLMonster committed Aug 1, 2019
1 parent cb4eecd commit 48a4b62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 8 additions & 2 deletions Source/Core/Common/Config/Layer.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ bool Layer::Exists(const ConfigLocation& location) const
bool Layer::DeleteKey(const ConfigLocation& location) bool Layer::DeleteKey(const ConfigLocation& location)
{ {
m_is_dirty = true; m_is_dirty = true;
bool had_value = m_map[location].has_value(); bool had_value = false;
m_map[location].reset(); const auto iter = m_map.find(location);
if (iter != m_map.end() && iter->second.has_value())
{
iter->second.reset();
had_value = true;
}

return had_value; return had_value;
} }


Expand Down
18 changes: 9 additions & 9 deletions Source/Core/Common/Config/Layer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ class Layer
void DeleteAllKeys(); void DeleteAllKeys();


template <typename T> template <typename T>
T Get(const ConfigInfo<T>& config_info) T Get(const ConfigInfo<T>& config_info) const
{ {
return Get<T>(config_info.location).value_or(config_info.default_value); return Get<T>(config_info.location).value_or(config_info.default_value);
} }


template <typename T> template <typename T>
std::optional<T> Get(const ConfigLocation& location) std::optional<T> Get(const ConfigLocation& location) const
{ {
const std::optional<std::string>& str_value = m_map[location]; const auto iter = m_map.find(location);
if (!str_value) if (iter == m_map.end() || !iter->second.has_value())
return std::nullopt; return std::nullopt;
return detail::TryParse<T>(*str_value); return detail::TryParse<T>(*iter->second);
} }


template <typename T> template <typename T>
Expand All @@ -129,13 +129,13 @@ class Layer
Set(location, ValueToString(value)); Set(location, ValueToString(value));
} }


void Set(const ConfigLocation& location, const std::string& new_value) void Set(const ConfigLocation& location, std::string new_value)
{ {
std::optional<std::string>& current_value = m_map[location]; const auto iter = m_map.find(location);
if (current_value == new_value) if (iter != m_map.end() && iter->second == new_value)
return; return;
m_is_dirty = true; m_is_dirty = true;
current_value = new_value; m_map.insert_or_assign(location, std::move(new_value));
} }


Section GetSection(System system, const std::string& section); Section GetSection(System system, const std::string& section);
Expand Down

0 comments on commit 48a4b62

Please sign in to comment.