Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
simple_storage.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <optional>
5#include <stdexcept>
6#include <string>
7#include <variant>
8
27 public:
28 using StorageKey = std::string;
29 using StorageValue = std::variant<int, float, std::string>;
30
53 template <class... TSelector>
54 struct overloaded : TSelector... {
55 using TSelector::operator()...;
56 };
57
65 template <typename T>
66 T get_value_or_default(const std::string &key, T default_value) {
67 // So we provide the overloads for the () operator here. Every version of it
68 // takes in x and returns T or throws...
69 return std::visit(overloaded{
70 [](const T value) -> T { return value; },
71 [](auto) -> T {
72 throw std::invalid_argument{
73 "invalid value for T in settings::value_of"};
74 },
75 },
76 instance().try_get(key).value_or(default_value));
77 }
78
80
88 std::optional<StorageValue> try_get(const StorageKey &key_name);
89
97 void set_value(const StorageKey &key_name, const StorageValue &value);
98
106
113 void delete_key(const StorageKey &key_name);
114
121 void save();
122
123 protected:
131 std::map<StorageKey, StorageValue> session_storage_;
132
133 SimpleStorage() = default;
134
142 friend class Engine;
143
151 void load();
152};
Central engine class managing core services and lifecycle.
Definition engine.h:13
Simple key–value storage system for persisting basic data types.
Definition simple_storage.h:26
T get_value_or_default(const std::string &key, T default_value)
Definition simple_storage.h:66
void delete_key(const StorageKey &key_name)
Remove a specific stored entry.
std::optional< StorageValue > try_get(const StorageKey &key_name)
Attempt to retrieve a stored value.
void save()
Persist current storage to disk or another backend.
std::string StorageKey
Definition simple_storage.h:28
void set_value(const StorageKey &key_name, const StorageValue &value)
Store or overwrite a value.
std::variant< int, float, std::string > StorageValue
Definition simple_storage.h:29
void delete_all()
Remove all stored entries.
std::map< StorageKey, StorageValue > session_storage_
In-memory map of all loaded storage entries.
Definition simple_storage.h:131
SimpleStorage()=default
static SimpleStorage & instance()
void load()
Load stored data into memory.
Definition simple_storage.h:54