galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Config.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_CORE_CONFIG_HPP_
9#define GALAXY_CORE_CONFIG_HPP_
10
11#include <nlohmann/json.hpp>
12
16
17namespace galaxy
18{
22 class Config final
23 {
24 public:
28 Config() noexcept;
29
37 Config(std::string_view file) noexcept;
38
42 ~Config() noexcept;
43
51 void load(std::string_view file);
52
56 void save();
57
68 template<meta::standard_type Value>
69 void set(const std::string& key, const Value& value) noexcept;
70
83 template<meta::standard_type Value>
84 void set(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
85
94 template<meta::standard_type Value>
95 void restore(const std::string& key, const Value& value) noexcept;
96
107 template<meta::standard_type Value>
108 void restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
109
117 [[nodiscard]]
118 bool has(const std::string& key) noexcept;
119
129 [[nodiscard]]
130 bool has(const std::string& key, const std::string& section, const std::string& delim = ".");
131
141 template<meta::standard_type Value>
142 [[nodiscard]]
143 std::optional<Value> get(const std::string& key);
144
156 template<meta::standard_type Value>
157 [[nodiscard]]
158 std::optional<Value> get(const std::string& key, const std::string& section, const std::string& delim = ".");
159
165 [[nodiscard]]
166 bool empty() const;
167
173 [[nodiscard]]
174 nlohmann::json& raw() noexcept;
175
176 private:
180 Config(const Config&) = delete;
181
185 Config(Config&&) = delete;
186
190 Config& operator=(const Config&) = delete;
191
195 Config& operator=(Config&&) = delete;
196
197 private:
201 nlohmann::json m_config;
202
206 std::string m_path;
207 };
208
209 template<meta::standard_type Value>
210 inline void Config::set(const std::string& key, const Value& value) noexcept
211 {
212 m_config[key] = value;
213 }
214
215 template<meta::standard_type Value>
216 inline void Config::set(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
217 {
218 const auto sections = str::split(section, delim);
219 if (sections.empty())
220 {
221 // Single section.
222 m_config[section][key] = value;
223 }
224 else
225 {
226 // Multiple sections.
227 nlohmann::json* leaf = &m_config;
228 for (const auto& sec : sections)
229 {
230 if (!leaf->contains(sec))
231 {
232 (*leaf)[sec] = nlohmann::json::object();
233 }
234
235 leaf = &leaf->at(sec);
236 }
237
238 (*leaf)[key] = value;
239 }
240 }
241
242 template<meta::standard_type Value>
243 inline void Config::restore(const std::string& key, const Value& value) noexcept
244 {
245 if (!has(key))
246 {
247 set(key, value);
248 }
249 }
250
251 template<meta::standard_type Value>
252 inline void Config::restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
253 {
254 if (!has(key, section, delim))
255 {
256 set(key, value, section, delim);
257 }
258 }
259
260 template<meta::standard_type Value>
261 inline std::optional<Value> Config::get(const std::string& key)
262 {
263 const auto& section = m_config;
264
265 if (section.contains(key))
266 {
267 return section[key].get<Value>();
268 }
269
270 return std::nullopt;
271 }
272
273 template<meta::standard_type Value>
274 inline std::optional<Value> Config::get(const std::string& key, const std::string& section, const std::string& delim)
275 {
276 const auto sections = str::split(section, delim);
277 if (sections.empty())
278 {
279 // Single section.
280 const auto& root = m_config;
281 if (root.contains(section))
282 {
283 const auto& obj = root[section];
284 if (obj.contains(key))
285 {
286 return obj[key].get<Value>();
287 }
288 }
289
290 return std::nullopt;
291 }
292 else
293 {
294 // Multiple sections.
295 nlohmann::json* leaf = &m_config;
296 for (const auto& sec : sections)
297 {
298 if (leaf->contains(sec))
299 {
300 leaf = &leaf->at(sec);
301 }
302 }
303
304 if (leaf->contains(key))
305 {
306 return (*leaf)[key].get<Value>();
307 }
308
309 return std::nullopt;
310 }
311 }
312} // namespace galaxy
313
314#endif
Config() noexcept
Constructor.
Definition Config.cpp:14
Config(Config &&)=delete
Move constructor.
void save()
Save the config file. Must be opened first.
Definition Config.cpp:59
void load(std::string_view file)
Loads config json from disk.
Definition Config.cpp:30
std::optional< Value > get(const std::string &key)
Retrieve a root config value.
Definition Config.hpp:261
nlohmann::json m_config
Config file as JSON.
Definition Config.hpp:201
Config(const Config &)=delete
Copy constructor.
Config & operator=(Config &&)=delete
Move assignment operator.
nlohmann::json & raw() noexcept
Get raw json object.
Definition Config.cpp:111
void restore(const std::string &key, const Value &value) noexcept
Sets a new setting, only if key or value does not already exist.
Definition Config.hpp:243
bool empty() const
Is the config file blank.
Definition Config.cpp:106
std::string m_path
Filepath.
Definition Config.hpp:206
void set(const std::string &key, const Value &value) noexcept
Set a new key-value pair for the config.
Definition Config.hpp:210
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
Definition Config.cpp:68
Config & operator=(const Config &)=delete
Copy assignment operator.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.
Tag.cpp galaxy.
Definition Tag.cpp:13