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 bool has_param(const std::string& module_name, const std::string& param_name) const;
29
36 template <typename T>
37 std::optional<T> param(const std::string& module_name, const std::string& param_name) const;
38
46 template <typename T>
47 T param(const std::string& module_name, const std::string& param_name, const T& default_value) const;
48
56 template <typename T>
57 T param_cast(const std::string& module_name, const std::string& param_name) const;
58
65 template <typename T>
66 std::optional<T> param_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name) const;
67
75 template <typename T>
76 T param_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name, const T& default_value) const;
77
85 template <typename T>
86 T param_cast_nested(const std::vector<std::string>& nested_module_names, const std::string& param_name) const;
87
96 template <typename T>
97 bool override_param(const std::string& module_name, const std::string& param_name, const T& value);
98
103 void save(const std::string& path) const;
104
105protected:
106 std::any config;
107};
108
112class GlobalConfig : public Config {
113private:
114 GlobalConfig(const std::string& global_config_path) : Config(global_config_path) {}
115 virtual ~GlobalConfig() override {}
116
117public:
118 static GlobalConfig* instance(const std::string& config_path = std::string(), bool override_path = false);
119
120 static std::string get_config_path(const std::string& config_name);
121
126 void dump(const std::string& path);
127
128private:
129 static GlobalConfig* inst;
130};
131
132} // namespace glim
Configuration loader from JSON files.
Definition config.hpp:14
bool has_param(const std::string &module_name, const std::string &param_name) const
Check if a parameter exists.
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:112
void dump(const std::string &path)
Dump all involved config parameters.