mirror of
https://github.com/N64Recomp/N64ModernRuntime.git
synced 2025-10-30 08:02:29 +00:00
init recomp config_store
This commit is contained in:
parent
576fed6cfc
commit
5e03b5ff0e
3 changed files with 95 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
# Define the library
|
# Define the library
|
||||||
add_library(librecomp STATIC
|
add_library(librecomp STATIC
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/ai.cpp"
|
"${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/cont.cpp"
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/dp.cpp"
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/dp.cpp"
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/eep.cpp"
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/eep.cpp"
|
||||||
|
|
|
||||||
72
librecomp/include/librecomp/config_store.hpp
Normal file
72
librecomp/include/librecomp/config_store.hpp
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
#ifndef __RECOMP_CONFIG_STORE_H__
|
||||||
|
#define __RECOMP_CONFIG_STORE_H__
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
struct string_hash {
|
||||||
|
using is_transparent = void;
|
||||||
|
[[nodiscard]] size_t operator()(const char *txt) const {
|
||||||
|
return std::hash<std::string_view>{}(txt);
|
||||||
|
}
|
||||||
|
[[nodiscard]] size_t operator()(std::string_view txt) const {
|
||||||
|
return std::hash<std::string_view>{}(txt);
|
||||||
|
}
|
||||||
|
[[nodiscard]] size_t operator()(const std::string &txt) const {
|
||||||
|
return std::hash<std::string>{}(txt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace recomp {
|
||||||
|
typedef std::variant<std::string, int> config_store_value;
|
||||||
|
typedef std::unordered_map<std::string, config_store_value, string_hash, std::equal_to<>> 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<typename T>
|
||||||
|
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<T>(it->second)) {
|
||||||
|
return std::get<T>(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<typename T>
|
||||||
|
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<T>(it->second)) {
|
||||||
|
return std::get<T>(it->second);
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Stored value is not of requested type");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return get_config_store_default_value<T>(key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
22
librecomp/src/config_store.cpp
Normal file
22
librecomp/src/config_store.cpp
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue