From 88a21dd2b9539292838aff32f5f29c182d4d6c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 16 Feb 2017 16:58:40 +0100 Subject: Fix things mentioned during code review Ref: https://github.com/dolphin-emu/dolphin/pull/4917 --- Source/Core/Common/Config/Config.cpp | 140 +++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Source/Core/Common/Config/Config.cpp (limited to 'Source/Core/Common/Config/Config.cpp') diff --git a/Source/Core/Common/Config/Config.cpp b/Source/Core/Common/Config/Config.cpp new file mode 100644 index 0000000000..bcfb9a2619 --- /dev/null +++ b/Source/Core/Common/Config/Config.cpp @@ -0,0 +1,140 @@ +// Copyright 2016 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include +#include +#include + +#include "Common/Assert.h" +#include "Common/Config/Config.h" + +namespace Config +{ +static Layers s_layers; +static std::list s_callbacks; + +void InvokeConfigChangedCallbacks(); + +Section* GetOrCreateSection(System system, const std::string& section_name) +{ + return s_layers[LayerType::Meta]->GetOrCreateSection(system, section_name); +} + +Layers* GetLayers() +{ + return &s_layers; +} + +void AddLayer(std::unique_ptr layer) +{ + s_layers[layer->GetLayer()] = std::move(layer); + InvokeConfigChangedCallbacks(); +} + +void AddLayer(std::unique_ptr loader) +{ + AddLayer(std::make_unique(std::move(loader))); +} + +void AddLoadLayer(std::unique_ptr layer) +{ + layer->Load(); + AddLayer(std::move(layer)); +} + +void AddLoadLayer(std::unique_ptr loader) +{ + AddLoadLayer(std::make_unique(std::move(loader))); +} + +Layer* GetLayer(LayerType layer) +{ + if (!LayerExists(layer)) + return nullptr; + return s_layers[layer].get(); +} + +void RemoveLayer(LayerType layer) +{ + s_layers.erase(layer); + InvokeConfigChangedCallbacks(); +} +bool LayerExists(LayerType layer) +{ + return s_layers.find(layer) != s_layers.end(); +} + +void AddConfigChangedCallback(ConfigChangedCallback func) +{ + s_callbacks.emplace_back(func); +} + +void InvokeConfigChangedCallbacks() +{ + for (const auto& callback : s_callbacks) + callback(); +} + +// Explicit load and save of layers +void Load() +{ + for (auto& layer : s_layers) + layer.second->Load(); +} + +void Save() +{ + for (auto& layer : s_layers) + layer.second->Save(); +} + +void Init() +{ + // This layer always has to exist + s_layers[LayerType::Meta] = std::make_unique(); +} + +void Shutdown() +{ + s_layers.clear(); + s_callbacks.clear(); +} + +static const std::map system_to_name = { + {System::Main, "Dolphin"}, {System::GCPad, "GCPad"}, {System::WiiPad, "Wiimote"}, + {System::GCKeyboard, "GCKeyboard"}, {System::GFX, "Graphics"}, {System::Logger, "Logger"}, + {System::Debugger, "Debugger"}, {System::UI, "UI"}, +}; + +const std::string& GetSystemName(System system) +{ + return system_to_name.at(system); +} + +System GetSystemFromName(const std::string& name) +{ + const auto system = std::find_if(system_to_name.begin(), system_to_name.end(), + [&name](const auto& entry) { return entry.second == name; }); + if (system != system_to_name.end()) + return system->first; + + _assert_msg_(COMMON, false, "Programming error! Couldn't convert '%s' to system!", name.c_str()); + return System::Main; +} + +const std::string& GetLayerName(LayerType layer) +{ + static const std::map layer_to_name = { + {LayerType::Base, "Base"}, + {LayerType::GlobalGame, "Global GameINI"}, + {LayerType::LocalGame, "Local GameINI"}, + {LayerType::Netplay, "Netplay"}, + {LayerType::Movie, "Movie"}, + {LayerType::CommandLine, "Command Line"}, + {LayerType::CurrentRun, "Current Run"}, + {LayerType::Meta, "Top"}, + }; + return layer_to_name.at(layer); +} +} -- cgit v1.2.3