wip callback registry

This commit is contained in:
thecozies 2024-09-02 10:47:59 -05:00 committed by Mr-Wiseguy
parent 47c533ced9
commit d6ac7f3f74
2 changed files with 78 additions and 0 deletions

View file

@ -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<std::string>(val)) {
return &std::get<std::string>(val);
}
if (std::holds_alternative<int>(val)) {
return &std::get<int>(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<config_store_value>(key);
if (const auto& func = *std::get_if<recomp_callback_internal>(&cb)) {
func(key, val);
} else if (const auto& func = *std::get_if<recomp_callback_external>(&cb)) {
func(
key.c_str(),
get_pointer_from_conf_value(val),
(ConfigStoreValueType)val.index()
);
}
};
}

View file

@ -0,0 +1,33 @@
#ifndef __RECOMP_CALLBACK_REGISTRY_H__
#define __RECOMP_CALLBACK_REGISTRY_H__
#include <string>
#include <vector>
#include <string>
#include <unordered_map>
#include <variant>
#include <map>
#include "json/json.hpp"
#include "ConfigStore.hpp"
namespace recomp::config {
using recomp_callback_internal = std::function<void(
const std::string& key,
config_store_value& value
)>;
using recomp_callback_external = std::function<void(
const char *key,
const void *value,
ConfigStoreValueType val_type
)>;
using recomp_callback = std::variant<recomp_callback_internal, recomp_callback_external>;
using callback_registry_map = std::unordered_map<std::string, recomp_callback>;
extern callback_registry_map callback_registry;
void register_callback(const std::string& config_group, const std::string& key, recomp_callback& callback);
}
#endif