Config: Allow unregistering callbacks.

This commit is contained in:
Admiral H. Curtiss 2021-12-25 23:32:42 +01:00
parent cae4b545bd
commit 319b00f1fd
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
2 changed files with 25 additions and 6 deletions

View File

@ -5,17 +5,19 @@
#include <algorithm>
#include <atomic>
#include <list>
#include <map>
#include <mutex>
#include <shared_mutex>
#include <utility>
#include <vector>
namespace Config
{
using Layers = std::map<LayerType, std::shared_ptr<Layer>>;
static Layers s_layers;
static std::list<ConfigChangedCallback> s_callbacks;
static std::vector<std::pair<size_t, ConfigChangedCallback>> s_callbacks;
static size_t s_next_callback_id = 0;
static u32 s_callback_guards = 0;
static std::atomic<u64> 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()

View File

@ -22,7 +22,9 @@ void AddLayer(std::unique_ptr<ConfigLayerLoader> loader);
std::shared_ptr<Layer> 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