libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::Config Class Reference

Persistent JSON-backed key-value configuration store. More...

#include <Config.h>

Inheritance diagram for Ship::Config:
[legend]
Collaboration diagram for Ship::Config:
[legend]

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().
 
ParentComponentListGetParents ()
 Returns a mutable reference to the parent list.
 
const ParentComponentListGetParents () const
 Returns a const reference to the parent list.
 
ComponentListGetChildren ()
 Returns a mutable reference to the child list.
 
const ComponentListGetChildren () const
 Returns a const reference to the child list.
 
virtual std::shared_ptr< ComponentTryGetSharedComponent () noexcept
 Returns a shared_ptr to this Component when available, otherwise nullptr.
 
virtual std::shared_ptr< ComponentGetSharedComponent ()
 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< ContextGetContext () 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.
 

Detailed Description

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):

  • Window — injected at construction for configuration flows that need an initialized Window. Any code path that uses the cached Window must validate that it exists and is initialized before use.

Obtain the instance from Context::GetChildren().GetFirst<Config>().

Constructor & Destructor Documentation

◆ Config()

Ship::Config::Config ( const std::string &  path,
std::shared_ptr< Window window = nullptr 
)

Constructs a Config, loading the JSON file at path (creates it if absent).

Parameters
pathFilesystem path to the JSON configuration file.
windowOptional Window dependency for configuration flows that need it.

◆ ~Config()

Ship::Config::~Config ( )

Member Function Documentation

◆ Contains()

bool Ship::Config::Contains ( const std::string &  key)

Returns true if key exists in the config.

Parameters
keyDot-notation config key to check.

◆ Copy()

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).

Parameters
fromKeySource dot-notation key.
toKeyDestination dot-notation key.

◆ Erase()

void Ship::Config::Erase ( const std::string &  key)

Removes a single key from the config.

Parameters
keyDot-notation config key to remove.

◆ EraseBlock()

void Ship::Config::EraseBlock ( const std::string &  key)

Removes all keys that share the given prefix (dot-notation block).

Parameters
keyPrefix of the block to erase (e.g. "Window" removes all "Window.*" keys).

◆ FormatNestedKey()

static std::string Ship::Config::FormatNestedKey ( const std::string &  key)
staticprotected

Converts a dot-notation key into the slash-separated flat key format used internally.

Parameters
keyDot-notation key.
Returns
Formatted flat key string.

◆ GetArray()

template<typename T >
std::vector< T > Ship::Config::GetArray ( const std::string &  key)
protected

Reads an array value for key.

Template Parameters
TElement type (must be JSON-deserializable).
Parameters
keyDot-notation config key.
Returns
Vector of values, or an empty vector if the key is absent.

◆ GetBool()

bool Ship::Config::GetBool ( const std::string &  key,
bool  defaultValue = false 
)

Returns the boolean value for key, or defaultValue if absent.

Parameters
keyDot-notation config key.
defaultValueFallback value.

◆ GetFloat()

float Ship::Config::GetFloat ( const std::string &  key,
float  defaultValue = 0.0f 
)

Returns the float value for key, or defaultValue if absent.

Parameters
keyDot-notation config key.
defaultValueFallback value.

◆ GetInt()

int32_t Ship::Config::GetInt ( const std::string &  key,
int32_t  defaultValue = 0 
)

Returns the signed integer value for key, or defaultValue if absent.

Parameters
keyDot-notation config key.
defaultValueFallback value.

◆ GetNestedJson()

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.

◆ GetPath()

const std::string & Ship::Config::GetPath ( ) const

Returns the filesystem path to the JSON configuration file.

Returns
The path that was passed to the constructor (resolved by the caller).

◆ GetString()

std::string Ship::Config::GetString ( const std::string &  key,
const std::string &  defaultValue = "" 
)

Returns the string value for key, or defaultValue if absent.

Parameters
keyDot-notation config key.
defaultValueFallback string.

◆ GetUInt()

uint32_t Ship::Config::GetUInt ( const std::string &  key,
uint32_t  defaultValue = 0 
)

Returns the unsigned integer value for key, or defaultValue if absent.

Parameters
keyDot-notation config key.
defaultValueFallback value.

◆ Nested()

nlohmann::json Ship::Config::Nested ( const std::string &  key)
protected

Resolves a dot-notation key into its nested JSON node.

Parameters
keyDot-notation config key.
Returns
The JSON value at that node.

◆ RegisterVersionUpdater()

bool Ship::Config::RegisterVersionUpdater ( std::shared_ptr< ConfigVersionUpdater versionUpdater)

Adds a ConfigVersionUpdater instance to the list to be run later via RunVersionUpdates.

Parameters
versionUpdater
Returns
true if the insert was successful, or
false if the insert failed, i.e. if the list already has a ConfigVersionUpdater with a matching version.

◆ Reload()

void Ship::Config::Reload ( )

Discards in-memory values and reloads the config file from disk.

◆ RunVersionUpdates()

void Ship::Config::RunVersionUpdates ( )

Runs the Update function on each ConfigVersionUpdater instance if the version matches\ the current ConfigVersion value of the config object.

◆ Save()

void Ship::Config::Save ( )

Flushes in-memory values to the config file on disk.

◆ SetArray()

template<typename T >
void Ship::Config::SetArray ( const std::string &  key,
std::vector< T >  array 
)
protected

Writes an array value for key.

Template Parameters
TElement type (must be JSON-serializable).
Parameters
keyDot-notation config key.
arrayValues to store.

◆ SetBlock()

void Ship::Config::SetBlock ( const std::string &  key,
nlohmann::json  block 
)

Replaces the JSON object stored at key with block.

Parameters
keyDot-notation key pointing to a JSON object node.
blockReplacement JSON object.

◆ SetBool()

void Ship::Config::SetBool ( const std::string &  key,
bool  value 
)

Writes a boolean value for key.

Parameters
keyDot-notation config key.
valueValue to store.

◆ SetFloat()

void Ship::Config::SetFloat ( const std::string &  key,
float  value 
)

Writes a float value for key.

Parameters
keyDot-notation config key.
valueValue to store.

◆ SetInt()

void Ship::Config::SetInt ( const std::string &  key,
int32_t  value 
)

Writes a signed integer value for key.

Parameters
keyDot-notation config key.
valueValue to store.

◆ SetString()

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).

Parameters
keyDot-notation config key.
valueValue to store.

◆ SetUInt()

void Ship::Config::SetUInt ( const std::string &  key,
uint32_t  value 
)

Writes an unsigned integer value for key.

Parameters
keyDot-notation config key.
valueValue to store.

The documentation for this class was generated from the following file: