Simple key–value storage system for persisting basic data types.
More...
#include <simple_storage.h>
Simple key–value storage system for persisting basic data types.
SimpleStorage provides a straightforward interface for storing and retrieving key–value pairs, where values can be of type int, float, or string. It supports loading from and saving to persistent storage, allowing data to persist across application runs.
Usage:
- Use set_value() to store values associated with string keys.
- Use get_value_or_default() to retrieve values, providing a default if the key does not exist.
- Call save() to persist the current state to disk.
- Call load() during initialization to restore state from disk.
- Note
- This class is designed for simple data storage needs and may not be suitable for complex data structures or high-performance requirements.
◆ StorageKey
◆ StorageValue
◆ SimpleStorage()
| SimpleStorage::SimpleStorage |
( |
| ) |
|
|
protecteddefault |
◆ delete_all()
| void SimpleStorage::delete_all |
( |
| ) |
|
Remove all stored entries.
Clears the entire storage map, discarding all keys and values currently held in memory. Does not trigger persistence by itself.
◆ delete_key()
| void SimpleStorage::delete_key |
( |
const StorageKey & |
key_name | ) |
|
Remove a specific stored entry.
Deletes the value associated with the given key without affecting other entries. Safe to call even when the key does not exist.
◆ get_value_or_default()
template<typename T >
| T SimpleStorage::get_value_or_default |
( |
const std::string & |
key, |
|
|
T |
default_value |
|
) |
| |
|
inline |
- Template Parameters
-
- Parameters
-
| key | storage key= |
| default_value | the default value |
- Returns
- the value contained inside the variant
◆ instance()
◆ load()
| void SimpleStorage::load |
( |
| ) |
|
|
protected |
Load stored data into memory.
Restores the session_storage_ from a persistent source if one exists. Intended to be called during initialization by Engine to rebuild the in-memory state for fast access.
◆ save()
| void SimpleStorage::save |
( |
| ) |
|
Persist current storage to disk or another backend.
Saves all in-memory key–value pairs so they can be restored later. Call this to maintain state across application runs.
◆ set_value()
Store or overwrite a value.
Assigns the provided value to the specified key. Existing values at the same key are replaced. This operation updates only the in-memory session storage.
◆ try_get()
Attempt to retrieve a stored value.
Looks up the given key and returns its associated value if it exists. Returns an empty optional when the key is not present, allowing callers to detect missing entries.
◆ Engine
Engine is the sole caller of load().
Only the Engine class should invoke load() during game startup. This ensures that session_storage_ is correctly populated from persistent storage before any game logic accesses it.
◆ session_storage_
In-memory map of all loaded storage entries.
Holds key–value pairs after load() is called, enabling fast lookup and mutation without requiring disk access. Serves as the working session state for all storage operations.
The documentation for this class was generated from the following file: