6#include <nlohmann/json.hpp>
69 Config(
const std::string& path, std::shared_ptr<Window> window =
nullptr);
83 std::string
GetString(
const std::string& key,
const std::string& defaultValue =
"");
90 float GetFloat(
const std::string& key,
float defaultValue = 0.0f);
97 bool GetBool(
const std::string& key,
bool defaultValue =
false);
104 int32_t
GetInt(
const std::string& key, int32_t defaultValue = 0);
111 uint32_t
GetUInt(
const std::string& key, uint32_t defaultValue = 0);
118 void SetString(
const std::string& key,
const std::string& value);
125 void SetFloat(
const std::string& key,
float value);
132 void SetBool(
const std::string& key,
bool value);
139 void SetInt(
const std::string& key, int32_t value);
146 void SetUInt(
const std::string& key, uint32_t value);
165 void SetBlock(
const std::string& key, nlohmann::json block);
172 void Copy(
const std::string& fromKey,
const std::string& toKey);
217 nlohmann::json
Nested(
const std::string& key);
232 template <
typename T>
void SetArray(
const std::string& key, std::vector<T> array);
240 template <
typename T> std::vector<T>
GetArray(
const std::string& key);
243 nlohmann::json mFlattenedJson;
244 nlohmann::json mNestedJson;
247 std::map<uint32_t, std::shared_ptr<ConfigVersionUpdater>> mVersionUpdaters;
248 std::shared_ptr<Window> mWindow;
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