Config: Only save settings if they have been changed

This commit is contained in:
Léo Lam 2017-02-14 18:05:54 +01:00
parent 8360e358ee
commit b51c6023ba
2 changed files with 46 additions and 5 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <array> #include <array>
#include <map> #include <map>
@ -119,16 +120,23 @@ bool Section::Delete(const std::string& key)
return false; return false;
m_values.erase(it); m_values.erase(it);
m_dirty = true;
return true; return true;
} }
void Section::Set(const std::string& key, const std::string& value) void Section::Set(const std::string& key, const std::string& value)
{ {
auto it = m_values.find(key); auto it = m_values.find(key);
if (it != m_values.end()) if (it != m_values.end() && it->second != value)
{
it->second = value; it->second = value;
else m_dirty = true;
}
else if (it == m_values.end())
{
m_values[key] = value; m_values[key] = value;
m_dirty = true;
}
} }
void Section::Set(const std::string& key, u32 newValue) void Section::Set(const std::string& key, u32 newValue)
@ -165,6 +173,12 @@ void Section::Set(const std::string& key, const std::string& newValue,
Delete(key); Delete(key);
} }
void Section::SetLines(const std::vector<std::string>& lines)
{
m_lines = lines;
m_dirty = true;
}
bool Section::Get(const std::string& key, std::string* value, bool Section::Get(const std::string& key, std::string* value,
const std::string& default_value) const const std::string& default_value) const
{ {
@ -326,13 +340,33 @@ void Layer::Load()
{ {
if (m_loader) if (m_loader)
m_loader->Load(this); m_loader->Load(this);
ClearDirty();
CallbackSystems(); CallbackSystems();
} }
void Layer::Save() void Layer::Save()
{ {
if (m_loader) if (!m_loader || !IsDirty())
return;
m_loader->Save(this); m_loader->Save(this);
ClearDirty();
}
bool Layer::IsDirty() const
{
return std::any_of(m_sections.begin(), m_sections.end(), [](const auto& system) {
return std::any_of(system.second.begin(), system.second.end(),
[](const auto& section) { return section->IsDirty(); });
});
}
void Layer::ClearDirty()
{
std::for_each(m_sections.begin(), m_sections.end(), [](const auto& system) {
std::for_each(system.second.begin(), system.second.end(),
[](const auto& section) { section->ClearDirty(); });
});
} }
Section* GetOrCreateSection(System system, const std::string& section_name) Section* GetOrCreateSection(System system, const std::string& section_name)

View File

@ -92,13 +92,17 @@ public:
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; bool Get(const std::string& key, double* value, double defaultValue = 0.0) const;
// Section chunk // Section chunk
void SetLines(const std::vector<std::string>& lines) { m_lines = lines; } void SetLines(const std::vector<std::string>& lines);
// XXX: Add to recursive layer // XXX: Add to recursive layer
virtual bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const; virtual bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
virtual bool HasLines() const { return m_lines.size() > 0; } virtual bool HasLines() const { return m_lines.size() > 0; }
const std::string& GetName() const { return m_name; } const std::string& GetName() const { return m_name; }
const SectionValueMap& GetValues() const { return m_values; } const SectionValueMap& GetValues() const { return m_values; }
bool IsDirty() const { return m_dirty; }
void ClearDirty() { m_dirty = false; }
protected: protected:
bool m_dirty = false;
LayerType m_layer; LayerType m_layer;
System m_system; System m_system;
const std::string m_name; const std::string m_name;
@ -154,6 +158,9 @@ public:
// Stay away from this routine as much as possible // Stay away from this routine as much as possible
ConfigLayerLoader* GetLoader() { return m_loader.get(); } ConfigLayerLoader* GetLoader() { return m_loader.get(); }
protected: protected:
bool IsDirty() const;
void ClearDirty();
LayerMap m_sections; LayerMap m_sections;
const LayerType m_layer; const LayerType m_layer;
std::unique_ptr<ConfigLayerLoader> m_loader; std::unique_ptr<ConfigLayerLoader> m_loader;