From 319b00f1fd6cc0385ffc4ac9d48411ed3fd1f42b Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sat, 25 Dec 2021 23:32:42 +0100 Subject: [PATCH] Config: Allow unregistering callbacks. --- Source/Core/Common/Config/Config.cpp | 27 ++++++++++++++++++++++----- Source/Core/Common/Config/Config.h | 4 +++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Source/Core/Common/Config/Config.cpp b/Source/Core/Common/Config/Config.cpp index 5943f3d067..67b21ea33b 100644 --- a/Source/Core/Common/Config/Config.cpp +++ b/Source/Core/Common/Config/Config.cpp @@ -5,17 +5,19 @@ #include #include -#include #include #include #include +#include +#include namespace Config { using Layers = std::map>; static Layers s_layers; -static std::list s_callbacks; +static std::vector> s_callbacks; +static size_t s_next_callback_id = 0; static u32 s_callback_guards = 0; static std::atomic s_config_version = 0; @@ -63,9 +65,24 @@ void RemoveLayer(LayerType layer) OnConfigChanged(); } -void AddConfigChangedCallback(ConfigChangedCallback func) +size_t AddConfigChangedCallback(ConfigChangedCallback func) { - s_callbacks.emplace_back(std::move(func)); + const size_t callback_id = s_next_callback_id; + ++s_next_callback_id; + s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func))); + return callback_id; +} + +void RemoveConfigChangedCallback(size_t callback_id) +{ + for (auto it = s_callbacks.begin(); it != s_callbacks.end(); ++it) + { + if (it->first == callback_id) + { + s_callbacks.erase(it); + return; + } + } } void OnConfigChanged() @@ -79,7 +96,7 @@ void OnConfigChanged() return; for (const auto& callback : s_callbacks) - callback(); + callback.second(); } u64 GetConfigVersion() diff --git a/Source/Core/Common/Config/Config.h b/Source/Core/Common/Config/Config.h index d2d3fd5c10..d956a203aa 100644 --- a/Source/Core/Common/Config/Config.h +++ b/Source/Core/Common/Config/Config.h @@ -22,7 +22,9 @@ void AddLayer(std::unique_ptr loader); std::shared_ptr GetLayer(LayerType layer); void RemoveLayer(LayerType layer); -void AddConfigChangedCallback(ConfigChangedCallback func); +// returns an ID that can be passed to RemoveConfigChangedCallback() +size_t AddConfigChangedCallback(ConfigChangedCallback func); +void RemoveConfigChangedCallback(size_t callback_id); void OnConfigChanged(); // Returns the number of times the config has changed in the current execution of the program