From d6ac7f3f747d6ae383840371520c20e4bf5ef1d4 Mon Sep 17 00:00:00 2001 From: thecozies <79979276+thecozies@users.noreply.github.com> Date: Mon, 2 Sep 2024 10:47:59 -0500 Subject: [PATCH] wip callback registry --- librecomp/src/config/CallbackRegistry.cpp | 45 +++++++++++++++++++++++ librecomp/src/config/CallbackRegistry.hpp | 33 +++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 librecomp/src/config/CallbackRegistry.cpp create mode 100644 librecomp/src/config/CallbackRegistry.hpp diff --git a/librecomp/src/config/CallbackRegistry.cpp b/librecomp/src/config/CallbackRegistry.cpp new file mode 100644 index 0000000..d7b7585 --- /dev/null +++ b/librecomp/src/config/CallbackRegistry.cpp @@ -0,0 +1,45 @@ +#include "CallbackRegistry.hpp" + +namespace recomp::config { + +callback_registry_map callback_registry = {}; + +static recomp_callback_internal nullcb; + +void register_callback(const std::string& config_group, const std::string& key, recomp_callback& callback) { + callback_registry[config_group + "/" + key] = callback; +}; + +const void *get_pointer_from_conf_value(config_store_value& val) { + if (std::holds_alternative(val)) { + return &std::get(val); + } + if (std::holds_alternative(val)) { + return &std::get(val); + } + return nullptr; +} + +void invoke_callback(const std::string& key) { + auto find_it = callback_registry.find(key); + if (find_it == callback_registry.end()) { + printf("ERROR: Could not locate callback at '%s'.\n", key); + return; + } + + auto& cb = callback_registry.at(key); + + config_store_value& val = get_config_store_value(key); + + if (const auto& func = *std::get_if(&cb)) { + func(key, val); + } else if (const auto& func = *std::get_if(&cb)) { + func( + key.c_str(), + get_pointer_from_conf_value(val), + (ConfigStoreValueType)val.index() + ); + } +}; + +} diff --git a/librecomp/src/config/CallbackRegistry.hpp b/librecomp/src/config/CallbackRegistry.hpp new file mode 100644 index 0000000..1f1e782 --- /dev/null +++ b/librecomp/src/config/CallbackRegistry.hpp @@ -0,0 +1,33 @@ +#ifndef __RECOMP_CALLBACK_REGISTRY_H__ +#define __RECOMP_CALLBACK_REGISTRY_H__ + +#include +#include +#include +#include +#include +#include +#include "json/json.hpp" +#include "ConfigStore.hpp" + +namespace recomp::config { + using recomp_callback_internal = std::function; + + using recomp_callback_external = std::function; + + using recomp_callback = std::variant; + + using callback_registry_map = std::unordered_map; + + extern callback_registry_map callback_registry; + + void register_callback(const std::string& config_group, const std::string& key, recomp_callback& callback); +} +#endif