Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
SimpleStorage Class Reference

Simple key–value storage system for persisting basic data types. More...

#include <simple_storage.h>

Classes

struct  overloaded
 

Public Types

using StorageKey = std::string
 
using StorageValue = std::variant< int, float, std::string >
 

Public Member Functions

template<typename T >
get_value_or_default (const std::string &key, T default_value)
 
std::optional< StorageValuetry_get (const StorageKey &key_name)
 Attempt to retrieve a stored value.
 
void set_value (const StorageKey &key_name, const StorageValue &value)
 Store or overwrite a value.
 
void delete_all ()
 Remove all stored entries.
 
void delete_key (const StorageKey &key_name)
 Remove a specific stored entry.
 
void save ()
 Persist current storage to disk or another backend.
 

Static Public Member Functions

static SimpleStorageinstance ()
 

Protected Member Functions

 SimpleStorage ()=default
 
void load ()
 Load stored data into memory.
 

Protected Attributes

std::map< StorageKey, StorageValuesession_storage_
 In-memory map of all loaded storage entries.
 

Friends

class Engine
 Engine is the sole caller of load().
 

Detailed Description

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.

Member Typedef Documentation

◆ StorageKey

using SimpleStorage::StorageKey = std::string

◆ StorageValue

using SimpleStorage::StorageValue = std::variant<int, float, std::string>

Constructor & Destructor Documentation

◆ SimpleStorage()

SimpleStorage::SimpleStorage ( )
protecteddefault

Member Function Documentation

◆ 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,
default_value 
)
inline
Template Parameters
Texpected_type
Parameters
keystorage key=
default_valuethe default value
Returns
the value contained inside the variant

◆ instance()

static SimpleStorage & SimpleStorage::instance ( )
static

◆ 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()

void SimpleStorage::set_value ( const StorageKey key_name,
const StorageValue 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()

std::optional< StorageValue > SimpleStorage::try_get ( const StorageKey key_name)

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.

Friends And Related Symbol Documentation

◆ Engine

friend class Engine
friend

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.

Member Data Documentation

◆ session_storage_

std::map<StorageKey, StorageValue> SimpleStorage::session_storage_
protected

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: