feat(reflection): Bring in new libconfig
This version of fourdst (v0.9.14) brings in the version libconfig which includes reflection based config filed (v2.0.2). This is a breaking change as any code which used versions of libconfig < v2.0.0 will no longer function when linked against this version of fourdst
This commit is contained in:
@@ -21,6 +21,6 @@ PYBIND11_MODULE(_phys, m) {
|
||||
auto constMod = m.def_submodule("constants", "Constants-module bindings");
|
||||
register_const_bindings(constMod);
|
||||
|
||||
auto configMod = m.def_submodule("config", "Configuration-module bindings");
|
||||
register_config_bindings(configMod);
|
||||
}
|
||||
const auto configMod = m.def_submodule("config", "Configuration-module bindings");
|
||||
register_config_enums(configMod);
|
||||
}
|
||||
@@ -1,55 +1,17 @@
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/stl.h> // Needed for vectors, maps, sets, strings
|
||||
#include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly
|
||||
|
||||
#include <string>
|
||||
#include "bindings.h"
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
#include "fourdst/config/config.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
void register_config_enums(const pybind11::module_& m) {
|
||||
using namespace fourdst::config;
|
||||
pybind11::enum_<ConfigState>(m, "ConfigState")
|
||||
.value("DEFAULT", ConfigState::DEFAULT)
|
||||
.value("LOADED_FROM_FILE", ConfigState::LOADED_FROM_FILE)
|
||||
.export_values();
|
||||
|
||||
// Helper function template for binding Config::get
|
||||
template <typename T>
|
||||
void def_config_get(py::module &m) {
|
||||
m.def("get",
|
||||
[](const std::string &key, T defaultValue) {
|
||||
return fourdst::config::Config::getInstance().get<T>(key, defaultValue);
|
||||
},
|
||||
py::arg("key"), py::arg("defaultValue"),
|
||||
"Get configuration value (type inferred from default)");
|
||||
}
|
||||
|
||||
void register_config_bindings(pybind11::module &config_submodule) {
|
||||
def_config_get<int>(config_submodule);
|
||||
def_config_get<double>(config_submodule);
|
||||
def_config_get<std::string>(config_submodule);
|
||||
def_config_get<bool>(config_submodule);
|
||||
|
||||
config_submodule.def("loadConfig",
|
||||
[](const std::string& configFilePath) {
|
||||
return fourdst::config::Config::getInstance().loadConfig(configFilePath);
|
||||
},
|
||||
py::arg("configFilePath"),
|
||||
"Load configuration from a YAML file.");
|
||||
|
||||
config_submodule.def("has",
|
||||
[](const std::string &key) {
|
||||
return fourdst::config::Config::getInstance().has(key);
|
||||
},
|
||||
py::arg("key"),
|
||||
"Check if a key exists in the configuration.");
|
||||
|
||||
config_submodule.def("keys",
|
||||
[]() {
|
||||
return py::cast(fourdst::config::Config::getInstance().keys());
|
||||
},
|
||||
"Get a list of all configuration keys.");
|
||||
|
||||
config_submodule.def("__repr__",
|
||||
[]() {
|
||||
std::ostringstream oss;
|
||||
oss << fourdst::config::Config::getInstance(); // Use the existing operator<<
|
||||
return std::string("<fourdsse_bindings.config module accessing C++ Singleton>\n") + oss.str();
|
||||
});
|
||||
pybind11::enum_<RootNameLoadPolicy>(m, "RootNameLoadPolicy")
|
||||
.value("FROM_FILE", RootNameLoadPolicy::FROM_FILE)
|
||||
.value("KEEP_CURRENT", RootNameLoadPolicy::KEEP_CURRENT)
|
||||
.export_values();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
#include "fourdst/config/config.h"
|
||||
|
||||
void register_config_bindings(pybind11::module &config_submodule);
|
||||
#include <string>
|
||||
#include <format>
|
||||
|
||||
void register_config_enums(const pybind11::module_& m);
|
||||
|
||||
template <typename ConfigType>
|
||||
pybind11::class_<ConfigType> bind_config_specialization(pybind11::module_& m , const std::string& name) {
|
||||
return pybind11::class_<ConfigType>(m, name.c_str())
|
||||
.def(pybind11::init<>())
|
||||
.def("load", &ConfigType::load, pybind11::arg("file_path"), "Load configuration from a file.")
|
||||
.def("save", &ConfigType::save, pybind11::arg("file_path") = "config_output.toml", "Save configuration to a file.")
|
||||
.def("save_schema", &ConfigType::save_schema, pybind11::arg("directory") = ".", "Save the configuration schema to a directory.")
|
||||
.def("get_state", &ConfigType::get_state, "Get the current state of the configuration.")
|
||||
.def("describe_state", &ConfigType::describe_state, "Get the current state of the configuration.")
|
||||
.def("__repr__",
|
||||
[](const ConfigType &cfg) -> std::string {
|
||||
return std::format("{}", cfg);
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
subdir('composition')
|
||||
subdir('constants')
|
||||
subdir('config')
|
||||
|
||||
Reference in New Issue
Block a user