libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Config.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <string>
5#include <map>
6#include <nlohmann/json.hpp>
8
9namespace Ship {
10class Config;
11class Window;
12
24 protected:
25 uint32_t mVersion;
26
27 public:
28 ConfigVersionUpdater(uint32_t toVersion);
35 virtual void Update(Config* conf) = 0;
36
42 uint32_t GetVersion();
43};
44
62class Config : public Component {
63 public:
69 Config(const std::string& path, std::shared_ptr<Window> window = nullptr);
71
76 const std::string& GetPath() const;
77
83 std::string GetString(const std::string& key, const std::string& defaultValue = "");
84
90 float GetFloat(const std::string& key, float defaultValue = 0.0f);
91
97 bool GetBool(const std::string& key, bool defaultValue = false);
98
104 int32_t GetInt(const std::string& key, int32_t defaultValue = 0);
105
111 uint32_t GetUInt(const std::string& key, uint32_t defaultValue = 0);
112
118 void SetString(const std::string& key, const std::string& value);
119
125 void SetFloat(const std::string& key, float value);
126
132 void SetBool(const std::string& key, bool value);
133
139 void SetInt(const std::string& key, int32_t value);
140
146 void SetUInt(const std::string& key, uint32_t value);
147
152 void Erase(const std::string& key);
153
158 void EraseBlock(const std::string& key);
159
165 void SetBlock(const std::string& key, nlohmann::json block);
166
172 void Copy(const std::string& fromKey, const std::string& toKey);
173
178 bool Contains(const std::string& key);
179
181 void Reload();
182
184 void Save();
185
192 nlohmann::json GetNestedJson();
193
202 bool RegisterVersionUpdater(std::shared_ptr<ConfigVersionUpdater> versionUpdater);
203
210
211 protected:
217 nlohmann::json Nested(const std::string& key);
218
224 static std::string FormatNestedKey(const std::string& key);
225
232 template <typename T> void SetArray(const std::string& key, std::vector<T> array);
233
240 template <typename T> std::vector<T> GetArray(const std::string& key);
241
242 private:
243 nlohmann::json mFlattenedJson;
244 nlohmann::json mNestedJson;
245 std::string mPath;
246 bool mIsNewInstance;
247 std::map<uint32_t, std::shared_ptr<ConfigVersionUpdater>> mVersionUpdaters;
248 std::shared_ptr<Window> mWindow;
249};
250} // namespace Ship
A named Part with a parent/child hierarchy and optional thread safety.
Definition Component.h:34
Abstract class representing a Config Version Updater, intended to express how to upgrade a Configurat...
Definition Config.h:23
uint32_t GetVersion()
Get the value of mVersion.
uint32_t mVersion
Definition Config.h:25
virtual void Update(Config *conf)=0
Performs actions on a Config object via the provided pointer to update it to the next version....
ConfigVersionUpdater(uint32_t toVersion)
Persistent JSON-backed key-value configuration store.
Definition Config.h:62
void SetBlock(const std::string &key, nlohmann::json block)
Replaces the JSON object stored at key with block.
const std::string & GetPath() const
Returns the filesystem path to the JSON configuration file.
void Erase(const std::string &key)
Removes a single key from the config.
void SetString(const std::string &key, const std::string &value)
Writes a string value for key (creates the key if it does not exist).
nlohmann::json GetNestedJson()
Returns the full config as a nested JSON object.
void SetArray(const std::string &key, std::vector< T > array)
Writes an array value for key.
bool Contains(const std::string &key)
Returns true if key exists in the config.
void RunVersionUpdates()
Runs the Update function on each ConfigVersionUpdater instance if the version matches\ the current Co...
int32_t GetInt(const std::string &key, int32_t defaultValue=0)
Returns the signed integer value for key, or defaultValue if absent.
uint32_t GetUInt(const std::string &key, uint32_t defaultValue=0)
Returns the unsigned integer value for key, or defaultValue if absent.
bool RegisterVersionUpdater(std::shared_ptr< ConfigVersionUpdater > versionUpdater)
Adds a ConfigVersionUpdater instance to the list to be run later via RunVersionUpdates.
void Reload()
Discards in-memory values and reloads the config file from disk.
void SetBool(const std::string &key, bool value)
Writes a boolean value for key.
float GetFloat(const std::string &key, float defaultValue=0.0f)
Returns the float value for key, or defaultValue if absent.
void EraseBlock(const std::string &key)
Removes all keys that share the given prefix (dot-notation block).
std::vector< T > GetArray(const std::string &key)
Reads an array value for key.
std::string GetString(const std::string &key, const std::string &defaultValue="")
Returns the string value for key, or defaultValue if absent.
void Copy(const std::string &fromKey, const std::string &toKey)
Copies the value at fromKey to toKey (overwriting any existing value at toKey).
void SetInt(const std::string &key, int32_t value)
Writes a signed integer value for key.
static std::string FormatNestedKey(const std::string &key)
Converts a dot-notation key into the slash-separated flat key format used internally.
Config(const std::string &path, std::shared_ptr< Window > window=nullptr)
Constructs a Config, loading the JSON file at path (creates it if absent).
void Save()
Flushes in-memory values to the config file on disk.
nlohmann::json Nested(const std::string &key)
Resolves a dot-notation key into its nested JSON node.
bool GetBool(const std::string &key, bool defaultValue=false)
Returns the boolean value for key, or defaultValue if absent.
void SetFloat(const std::string &key, float value)
Writes a float value for key.
void SetUInt(const std::string &key, uint32_t value)
Writes an unsigned integer value for key.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14