|
libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
|
Persistent JSON-backed key-value configuration store. More...
#include <Config.h>
Public Member Functions | |
| Config (const std::string &path, std::shared_ptr< Window > window=nullptr) | |
Constructs a Config, loading the JSON file at path (creates it if absent). | |
| ~Config () | |
| const std::string & | GetPath () const |
| Returns the filesystem path to the JSON configuration file. | |
| std::string | GetString (const std::string &key, const std::string &defaultValue="") |
Returns the string value for key, or defaultValue if absent. | |
| float | GetFloat (const std::string &key, float defaultValue=0.0f) |
Returns the float value for key, or defaultValue if absent. | |
| bool | GetBool (const std::string &key, bool defaultValue=false) |
Returns the boolean value for key, or defaultValue if absent. | |
| 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. | |
| void | SetString (const std::string &key, const std::string &value) |
Writes a string value for key (creates the key if it does not exist). | |
| void | SetFloat (const std::string &key, float value) |
Writes a float value for key. | |
| void | SetBool (const std::string &key, bool value) |
Writes a boolean value for key. | |
| void | SetInt (const std::string &key, int32_t value) |
Writes a signed integer value for key. | |
| void | SetUInt (const std::string &key, uint32_t value) |
Writes an unsigned integer value for key. | |
| void | Erase (const std::string &key) |
| Removes a single key from the config. | |
| void | EraseBlock (const std::string &key) |
| Removes all keys that share the given prefix (dot-notation block). | |
| void | SetBlock (const std::string &key, nlohmann::json block) |
Replaces the JSON object stored at key with block. | |
| void | Copy (const std::string &fromKey, const std::string &toKey) |
Copies the value at fromKey to toKey (overwriting any existing value at toKey). | |
| bool | Contains (const std::string &key) |
Returns true if key exists in the config. | |
| void | Reload () |
| Discards in-memory values and reloads the config file from disk. | |
| void | Save () |
| Flushes in-memory values to the config file on disk. | |
| nlohmann::json | GetNestedJson () |
| Returns the full config as a nested JSON object. | |
| bool | RegisterVersionUpdater (std::shared_ptr< ConfigVersionUpdater > versionUpdater) |
| Adds a ConfigVersionUpdater instance to the list to be run later via RunVersionUpdates. | |
| void | RunVersionUpdates () |
| Runs the Update function on each ConfigVersionUpdater instance if the version matches\ the current ConfigVersion value of the config object. | |
Public Member Functions inherited from Ship::Component | |
| Component (const std::string &name, std::shared_ptr< Context > context=nullptr) | |
| Constructs a Component with the given name. | |
| virtual | ~Component () |
| void | Init (const nlohmann::json &initArgs=nlohmann::json::object()) |
| Performs one-time initialization of this component. | |
| bool | IsInitialized () const |
| Returns true once Init() (or MarkInitialized()) has completed successfully. | |
| const std::string & | GetName () const |
| Returns the name of this Component. | |
| std::string | ToString () const |
| Returns a human-readable string representation (e.g. "Name (id)"). | |
| std::string | ToTreeString (int depth=0) const |
| Returns a human-readable tree representation of this component and its children. | |
| operator std::string () const | |
| Conversion operator to std::string; equivalent to ToString(). | |
| ParentComponentList & | GetParents () |
| Returns a mutable reference to the parent list. | |
| const ParentComponentList & | GetParents () const |
| Returns a const reference to the parent list. | |
| ComponentList & | GetChildren () |
| Returns a mutable reference to the child list. | |
| const ComponentList & | GetChildren () const |
| Returns a const reference to the child list. | |
| virtual std::shared_ptr< Component > | TryGetSharedComponent () noexcept |
| Returns a shared_ptr to this Component when available, otherwise nullptr. | |
| virtual std::shared_ptr< Component > | GetSharedComponent () |
| Returns a shared_ptr to this Component via the correct enable_shared_from_this base. | |
| template<typename T > | |
| bool | HasInChildren () const |
| Checks whether any descendant Component matches type T via BFS. | |
| template<typename T > | |
| std::shared_ptr< T > | GetFirstInChildren () const |
| Returns the first descendant that matches type T via BFS. | |
| template<typename T > | |
| std::shared_ptr< std::vector< std::shared_ptr< T > > > | GetInChildren () const |
| Returns all descendants that match type T via BFS. | |
| template<typename T > | |
| bool | HasInParents () const |
| Checks whether any ancestor Component matches type T via BFS. | |
| template<typename T > | |
| std::shared_ptr< T > | GetFirstInParents () const |
| Returns the first ancestor that matches type T via BFS. | |
| template<typename T > | |
| std::shared_ptr< std::vector< std::shared_ptr< T > > > | GetInParents () const |
| Returns all ancestors that match type T via BFS. | |
Public Member Functions inherited from Ship::Part | |
| Part () | |
| Constructs a Part and assigns it a unique ID. | |
| Part (std::shared_ptr< Context > context) | |
| Constructs a Part with an explicit Context reference and unique ID. | |
| virtual | ~Part ()=default |
| uint64_t | GetId () const |
| Returns the unique identifier for this Part. | |
| bool | operator== (const Part &other) const |
| Compares two Parts for equality by their unique IDs. | |
| std::shared_ptr< Context > | GetContext () const |
| Returns the Context this Part belongs to, or nullptr if unset. | |
| void | SetContext (std::shared_ptr< Context > ctx) |
| Sets the Context this Part belongs to. | |
Protected Member Functions | |
| nlohmann::json | Nested (const std::string &key) |
| Resolves a dot-notation key into its nested JSON node. | |
| template<typename T > | |
| void | SetArray (const std::string &key, std::vector< T > array) |
Writes an array value for key. | |
| template<typename T > | |
| std::vector< T > | GetArray (const std::string &key) |
Reads an array value for key. | |
Protected Member Functions inherited from Ship::Component | |
| virtual void | OnInit (const nlohmann::json &initArgs=nlohmann::json::object()) |
| Override this to implement component-specific initialization logic. | |
| void | MarkInitialized () |
| Marks this component as initialized without going through Init(). | |
| template<typename T > | |
| std::shared_ptr< T > | RequireDependency (const std::shared_ptr< T > &dependency, const std::string &dependencyName) const |
| Returns a cached dependency after validating it exists and is initialized. | |
Protected Member Functions inherited from Ship::Part | |
| virtual void | OnAdded (bool forced) |
| Called after this Part has been added to a PartList. | |
| virtual void | OnRemoved (bool forced) |
| Called after this Part has been removed from a PartList. | |
Static Protected Member Functions | |
| static std::string | FormatNestedKey (const std::string &key) |
| Converts a dot-notation key into the slash-separated flat key format used internally. | |
Persistent JSON-backed key-value configuration store.
Config provides typed accessors (Get/Set) for settings stored in a flat JSON file. Keys may use dot-notation to express nesting (e.g. "Window.Width"). The file is read on construction and can be reloaded or saved at any time.
Version migration is supported through ConfigVersionUpdater subclasses; register them with RegisterVersionUpdater() and call RunVersionUpdates() on startup.
Optional dependency (constructor-injected):
Obtain the instance from Context::GetChildren().GetFirst<Config>().
| Ship::Config::Config | ( | const std::string & | path, |
| std::shared_ptr< Window > | window = nullptr |
||
| ) |
| Ship::Config::~Config | ( | ) |
| bool Ship::Config::Contains | ( | const std::string & | key | ) |
Returns true if key exists in the config.
| key | Dot-notation config key to check. |
| void Ship::Config::Copy | ( | const std::string & | fromKey, |
| const std::string & | toKey | ||
| ) |
Copies the value at fromKey to toKey (overwriting any existing value at toKey).
| fromKey | Source dot-notation key. |
| toKey | Destination dot-notation key. |
| void Ship::Config::Erase | ( | const std::string & | key | ) |
Removes a single key from the config.
| key | Dot-notation config key to remove. |
| void Ship::Config::EraseBlock | ( | const std::string & | key | ) |
Removes all keys that share the given prefix (dot-notation block).
| key | Prefix of the block to erase (e.g. "Window" removes all "Window.*" keys). |
|
staticprotected |
Converts a dot-notation key into the slash-separated flat key format used internally.
| key | Dot-notation key. |
|
protected |
Reads an array value for key.
| T | Element type (must be JSON-deserializable). |
| key | Dot-notation config key. |
| bool Ship::Config::GetBool | ( | const std::string & | key, |
| bool | defaultValue = false |
||
| ) |
Returns the boolean value for key, or defaultValue if absent.
| key | Dot-notation config key. |
| defaultValue | Fallback value. |
| float Ship::Config::GetFloat | ( | const std::string & | key, |
| float | defaultValue = 0.0f |
||
| ) |
Returns the float value for key, or defaultValue if absent.
| key | Dot-notation config key. |
| defaultValue | Fallback value. |
| int32_t Ship::Config::GetInt | ( | const std::string & | key, |
| int32_t | defaultValue = 0 |
||
| ) |
Returns the signed integer value for key, or defaultValue if absent.
| key | Dot-notation config key. |
| defaultValue | Fallback value. |
| nlohmann::json Ship::Config::GetNestedJson | ( | ) |
Returns the full config as a nested JSON object.
Unlike the internal flattened representation, the returned object mirrors the dot-notation key hierarchy as nested JSON objects.
| const std::string & Ship::Config::GetPath | ( | ) | const |
Returns the filesystem path to the JSON configuration file.
| std::string Ship::Config::GetString | ( | const std::string & | key, |
| const std::string & | defaultValue = "" |
||
| ) |
Returns the string value for key, or defaultValue if absent.
| key | Dot-notation config key. |
| defaultValue | Fallback string. |
| uint32_t Ship::Config::GetUInt | ( | const std::string & | key, |
| uint32_t | defaultValue = 0 |
||
| ) |
Returns the unsigned integer value for key, or defaultValue if absent.
| key | Dot-notation config key. |
| defaultValue | Fallback value. |
|
protected |
Resolves a dot-notation key into its nested JSON node.
| key | Dot-notation config key. |
| bool Ship::Config::RegisterVersionUpdater | ( | std::shared_ptr< ConfigVersionUpdater > | versionUpdater | ) |
Adds a ConfigVersionUpdater instance to the list to be run later via RunVersionUpdates.
| versionUpdater |
| void Ship::Config::Reload | ( | ) |
Discards in-memory values and reloads the config file from disk.
| void Ship::Config::RunVersionUpdates | ( | ) |
Runs the Update function on each ConfigVersionUpdater instance if the version matches\ the current ConfigVersion value of the config object.
| void Ship::Config::Save | ( | ) |
Flushes in-memory values to the config file on disk.
|
protected |
Writes an array value for key.
| T | Element type (must be JSON-serializable). |
| key | Dot-notation config key. |
| array | Values to store. |
| void Ship::Config::SetBlock | ( | const std::string & | key, |
| nlohmann::json | block | ||
| ) |
Replaces the JSON object stored at key with block.
| key | Dot-notation key pointing to a JSON object node. |
| block | Replacement JSON object. |
| void Ship::Config::SetBool | ( | const std::string & | key, |
| bool | value | ||
| ) |
Writes a boolean value for key.
| key | Dot-notation config key. |
| value | Value to store. |
| void Ship::Config::SetFloat | ( | const std::string & | key, |
| float | value | ||
| ) |
Writes a float value for key.
| key | Dot-notation config key. |
| value | Value to store. |
| void Ship::Config::SetInt | ( | const std::string & | key, |
| int32_t | value | ||
| ) |
Writes a signed integer value for key.
| key | Dot-notation config key. |
| value | Value to store. |
| void Ship::Config::SetString | ( | const std::string & | key, |
| const std::string & | value | ||
| ) |
Writes a string value for key (creates the key if it does not exist).
| key | Dot-notation config key. |
| value | Value to store. |
| void Ship::Config::SetUInt | ( | const std::string & | key, |
| uint32_t | value | ||
| ) |
Writes an unsigned integer value for key.
| key | Dot-notation config key. |
| value | Value to store. |