guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
local_storage.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <filesystem>
26#include <mutex>
27#include <optional>
28#include <string>
29
30#include <utility/logging/loggable.hpp>
31#include <utility/logging/default_logger.hpp>
32
33#include "storage.hpp"
34
35struct sqlite3;
36struct sqlite3_stmt;
37
38namespace guillaume
39{
40
72 public Storage,
73 public utility::logging::Loggable<LocalStorage,
74 utility::logging::DefaultLogger>
75 {
76 private:
77 std::filesystem::path
78 _storageFilePath;
79 sqlite3 *_database;
80 mutable std::mutex _mutex;
81 bool _persistent {
82 true
83 };
85 sqlite3_stmt *_setItemStmt {
86 nullptr
87 };
88 sqlite3_stmt *_getItemStmt {
89 nullptr
90 };
91 sqlite3_stmt *_removeItemStmt {
92 nullptr
93 };
94
104 sqlite3_stmt *prepareStatement(sqlite3_stmt *&stmt,
105 const char *sql) const;
106
111 void finalizeStatements(void);
112
118 void initializeSchema(void);
119
127 bool executeStatement(const std::string &sql) const noexcept(false);
128
129 public:
130 using Storage::setItem;
131
144 explicit LocalStorage(const std::filesystem::path &storageFilePath =
145 defaultStoragePath()) noexcept(false);
146
150 ~LocalStorage(void) override;
151
162 void setItem(const std::string &key,
163 const std::string &value) noexcept(false) override;
164
172 std::optional<std::string>
173 getItem(const std::string &key) noexcept(false) override;
174
181 void removeItem(const std::string &key) noexcept(false) override;
182
187 void clear(void) noexcept(false) override;
188
196 static std::filesystem::path defaultStoragePath(void) noexcept(false);
197
206 bool isPersistent(void) const noexcept;
207 };
208
209} // namespace guillaume
static std::filesystem::path defaultStoragePath(void) noexcept(false)
Get the path used when the constructor is given none.
bool isPersistent(void) const noexcept
Tell whether the items are written to the storage file.
void setItem(const std::string &key, const std::string &value) noexcept(false) override
Store a string under a key, replacing the item already stored under that key, if any.
void clear(void) noexcept(false) override
Delete every item of the storage file.
LocalStorage(const std::filesystem::path &storageFilePath=defaultStoragePath()) noexcept(false)
Open the storage file, creating it and its parent directories when they do not exist.
std::optional< std::string > getItem(const std::string &key) noexcept(false) override
Read the text stored under a key.
void removeItem(const std::string &key) noexcept(false) override
Delete the item stored under a key. Does nothing when there is none.
A key-value store of strings, modelled on the web Storage API (setItem, getItem, removeItem,...
Definition storage.hpp:67
virtual void setItem(const std::string &key, const std::string &value)=0
Store a string under a key, replacing the item already stored under that key, if any.
A C++20 UI framework for desktop and XR, built as an entity-component-system.