diff --git a/librecomp/CMakeLists.txt b/librecomp/CMakeLists.txt index 33c52d3..431ef5f 100644 --- a/librecomp/CMakeLists.txt +++ b/librecomp/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Define the library add_library(librecomp STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/ai.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/config_store.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/cont.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/dp.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/eep.cpp" diff --git a/librecomp/include/librecomp/config_store.hpp b/librecomp/include/librecomp/config_store.hpp new file mode 100644 index 0000000..8d5400f --- /dev/null +++ b/librecomp/include/librecomp/config_store.hpp @@ -0,0 +1,72 @@ +#ifndef __RECOMP_CONFIG_STORE_H__ +#define __RECOMP_CONFIG_STORE_H__ + +#include +#include +#include +#include +#include + +struct string_hash { + using is_transparent = void; + [[nodiscard]] size_t operator()(const char *txt) const { + return std::hash{}(txt); + } + [[nodiscard]] size_t operator()(std::string_view txt) const { + return std::hash{}(txt); + } + [[nodiscard]] size_t operator()(const std::string &txt) const { + return std::hash{}(txt); + } +}; + +namespace recomp { + typedef std::variant config_store_value; + typedef std::unordered_map> config_store_map; + + struct ConfigStore { + recomp::config_store_map map; + recomp::config_store_map default_map; + std::mutex store_mutex; + std::mutex default_store_mutex; + }; + + extern ConfigStore config_store; + + void set_config_store_value(std::string key, config_store_value value); + void set_config_store_default_value(std::string key, config_store_value value); + void set_config_store_value_and_default(std::string key, config_store_value value, config_store_value default_value); + + template + T get_config_store_default_value(std::string key) { + std::lock_guard lock{ config_store.default_store_mutex }; + auto it = config_store.default_map.find(key); + if (it != config_store.default_map.end()) { + if (std::holds_alternative(it->second)) { + return std::get(it->second); + } else { + throw std::runtime_error("Stored value is not of requested type"); + } + } else { + throw std::runtime_error("Key not found"); + } + }; + + // Get a value from the config store, if it doesn't exist then return the default value + template + T get_config_store_value(std::string key) { + std::lock_guard lock{ config_store.store_mutex }; + auto it = config_store.map.find(key); + if (it != config_store.map.end()) { + if (std::holds_alternative(it->second)) { + return std::get(it->second); + } else { + throw std::runtime_error("Stored value is not of requested type"); + } + } else { + return get_config_store_default_value(key); + } + }; +}; + +#endif diff --git a/librecomp/src/config_store.cpp b/librecomp/src/config_store.cpp new file mode 100644 index 0000000..1dad201 --- /dev/null +++ b/librecomp/src/config_store.cpp @@ -0,0 +1,22 @@ +#include "config_store.hpp" + +namespace recomp { + +ConfigStore config_store = {{}, {}}; + +void set_config_store_value(std::string key, config_store_value value) { + std::lock_guard lock{ config_store.store_mutex }; + config_store.map[key] = value; +} + +void set_config_store_default_value(std::string key, config_store_value value) { + std::lock_guard lock{ config_store.default_store_mutex }; + config_store.default_map[key] = value; +} + +void set_config_store_value_and_default(std::string key, config_store_value value, config_store_value default_value) { + set_config_store_value(key, value); + set_config_store_default_value(key, default_value); +} + +}