summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Config/Layer.cpp
diff options
context:
space:
mode:
authorLéo Lam <leo@innovatetechnologi.es>2017-02-16 16:58:40 +0100
committerLéo Lam <leo@innovatetechnologi.es>2017-02-23 18:15:12 +0100
commit88a21dd2b9539292838aff32f5f29c182d4d6c39 (patch)
tree0ed2a6afe4e69db6b3902f141c7ae965077399bb /Source/Core/Common/Config/Layer.cpp
parentabe6f8766a5a1d9007584b4224ba9b74fdf645f2 (diff)
Fix things mentioned during code review
Ref: https://github.com/dolphin-emu/dolphin/pull/4917
Diffstat (limited to 'Source/Core/Common/Config/Layer.cpp')
-rw-r--r--Source/Core/Common/Config/Layer.cpp148
1 files changed, 148 insertions, 0 deletions
diff --git a/Source/Core/Common/Config/Layer.cpp b/Source/Core/Common/Config/Layer.cpp
new file mode 100644
index 0000000000..ee5d5e2ad6
--- /dev/null
+++ b/Source/Core/Common/Config/Layer.cpp
@@ -0,0 +1,148 @@
+// Copyright 2016 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <cstring>
+#include <map>
+
+#include "Common/Config/Config.h"
+#include "Common/Config/Layer.h"
+#include "Common/Config/Section.h"
+
+namespace Config
+{
+ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer)
+{
+}
+
+ConfigLayerLoader::~ConfigLayerLoader() = default;
+
+LayerType ConfigLayerLoader::GetLayer() const
+{
+ return m_layer;
+}
+
+Layer::Layer(LayerType type) : m_layer(type)
+{
+}
+
+Layer::Layer(std::unique_ptr<ConfigLayerLoader> loader)
+ : m_layer(loader->GetLayer()), m_loader(std::move(loader))
+{
+ Load();
+}
+
+Layer::~Layer()
+{
+ Save();
+}
+
+bool Layer::Exists(System system, const std::string& section_name, const std::string& key)
+{
+ Section* section = GetSection(system, section_name);
+ if (!section)
+ return false;
+ return section->Exists(key);
+}
+
+bool Layer::DeleteKey(System system, const std::string& section_name, const std::string& key)
+{
+ Section* section = GetSection(system, section_name);
+ if (!section)
+ return false;
+ return section->Delete(key);
+}
+
+Section* Layer::GetSection(System system, const std::string& section_name)
+{
+ for (auto& section : m_sections[system])
+ if (!strcasecmp(section.m_name.c_str(), section_name.c_str()))
+ return &section;
+ return nullptr;
+}
+
+Section* Layer::GetOrCreateSection(System system, const std::string& section_name)
+{
+ Section* section = GetSection(system, section_name);
+ if (!section)
+ {
+ if (m_layer == LayerType::Meta)
+ m_sections[system].emplace_back(RecursiveSection(m_layer, system, section_name));
+ else
+ m_sections[system].emplace_back(Section(m_layer, system, section_name));
+ section = &m_sections[system].back();
+ }
+ return section;
+}
+
+void Layer::Load()
+{
+ if (m_loader)
+ m_loader->Load(this);
+ ClearDirty();
+ InvokeConfigChangedCallbacks();
+}
+
+void Layer::Save()
+{
+ if (!m_loader || !IsDirty())
+ return;
+
+ m_loader->Save(this);
+ ClearDirty();
+ InvokeConfigChangedCallbacks();
+}
+
+LayerType Layer::GetLayer() const
+{
+ return m_layer;
+}
+
+const LayerMap& Layer::GetLayerMap() const
+{
+ return m_sections;
+}
+
+ConfigLayerLoader* Layer::GetLoader() const
+{
+ return m_loader.get();
+}
+
+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(), [](auto& system) {
+ std::for_each(system.second.begin(), system.second.end(),
+ [](auto& section) { section.ClearDirty(); });
+ });
+}
+
+RecursiveLayer::RecursiveLayer() : Layer(LayerType::Meta)
+{
+}
+
+Section* RecursiveLayer::GetSection(System system, const std::string& section_name)
+{
+ // Always queries backwards recursively, so it doesn't matter if it exists or not on this layer
+ return GetOrCreateSection(system, section_name);
+}
+
+Section* RecursiveLayer::GetOrCreateSection(System system, const std::string& section_name)
+{
+ Section* section = Layer::GetSection(system, section_name);
+ if (!section)
+ {
+ m_sections[system].emplace_back(RecursiveSection(m_layer, system, section_name));
+ section = &m_sections[system].back();
+ }
+ return section;
+}
+}