GLIM
Loading...
Searching...
No Matches
config.hpp
1#pragma once
2
3#include <any>
4#include <vector>
5#include <string>
6#include <memory>
7#include <optional>
8
9namespace glim {
10
14class Config {
15public:
19 Config(const std::string& config_filename);
20 virtual ~Config();
21
28 template <typename T>
29 std::optional<T> param(const std::string& module_name, const std::string& param_name) const;
30
38 template <typename T>
39 T param(const std::string& module_name, const std::string& param_name, const T& default_value) const;
40
48 template <typename T>
49 T param_cast(const std::string& module_name, const std::string& param_name) const;
50
57 template <typename T>
58 std::optional<T> param_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name) const;
59
67 template <typename T>
68 T param_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name, const T& default_value) const;
69
77 template <typename T>
78 T param_cast_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name) const;
79
88 template <typename T>
89 bool override_param(const std::string& module_name, const std::string& param_name, const T& value);
90
95 void save(const std::string& path) const;
96
97protected:
98 std::any config;
99};
100
104class GlobalConfig : public Config {
105private:
106 GlobalConfig(const std::string& global_config_path) : Config(global_config_path) {}
107 virtual ~GlobalConfig() override {}
108
109public:
110 static GlobalConfig* instance(const std::string& config_path = std::string(), bool override_path = false);
111
112 static std::string get_config_path(const std::string& config_name);
113
118 void dump(const std::string& path);
119
120private:
121 static GlobalConfig* inst;
122};
123
124} // namespace glim
Configuration loader from JSON files.
Definition config.hpp:14
void save(const std::string &path) const
Save config parameters as a JSON file.
T param_cast(const std::string &module_name, const std::string &param_name) const
Get a parameter.
Definition config_impl.hpp:161
Config(const std::string &config_filename)
Configuration JSON filename.
std::optional< T > param(const std::string &module_name, const std::string &param_name) const
Get a parameter.
Definition config_impl.hpp:131
std::optional< T > param_nested(const std::vector< std::string > &nested_module_names, const std::string &param_name) const
Get a parameter from a nested module.
Definition config_impl.hpp:182
bool override_param(const std::string &module_name, const std::string &param_name, const T &value)
Override a parameter value.
Definition config_impl.hpp:173
T param_cast_nested(const std::vector< std::string > &nested_module_names, const std::string &param_name) const
Get a parameter.
Definition config_impl.hpp:224
Global configuration class to bootstrap the root path of the configuration files.
Definition config.hpp:104
void dump(const std::string &path)
Dump all involved config parameters.