guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
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 <cerrno>
26#include <charconv>
27#include <cstdlib>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <type_traits>
32
33namespace guillaume
34{
35
66 class Storage
67 {
68 public:
72 virtual ~Storage(void) = default;
73
83 virtual void setItem(const std::string &key,
84 const std::string &value) = 0;
85
92 void setItem(const std::string &key, std::string_view value);
93
101 void setItem(const std::string &key, const char *value);
102
111 template<typename ValueType>
112 requires(std::is_same_v<std::remove_cvref_t<ValueType>, bool>)
113 void setItem(const std::string &key, ValueType value);
114
127 template<typename ValueType>
128 requires(std::is_arithmetic_v<ValueType>
129 && !std::is_same_v<ValueType, bool>)
130 void setItem(const std::string &key, ValueType value);
131
139 virtual std::optional<std::string> getItem(const std::string &key) = 0;
140
158 template<typename ValueType>
159 std::optional<ValueType> getItemAs(const std::string &key);
160
166 virtual void removeItem(const std::string &key) = 0;
167
171 virtual void clear(void) = 0;
172 };
173
174} // namespace guillaume
175
176// Include the Storage template implementation
177#include "guillaume/storage.tpp"
A key-value store of strings, modelled on the web Storage API (setItem, getItem, removeItem,...
Definition storage.hpp:67
virtual ~Storage(void)=default
Virtual destructor.
void setItem(const std::string &key, const char *value)
Store a copy of a C string under a key.
void setItem(const std::string &key, std::string_view value)
Store a copy of a string view under a key.
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.
virtual void clear(void)=0
Delete every item of the storage.
std::optional< ValueType > getItemAs(const std::string &key)
Read the item stored under a key and convert it to a type.
virtual std::optional< std::string > getItem(const std::string &key)=0
Read the text stored under a key.
void setItem(const std::string &key, ValueType value)
Store a number under a key, as its decimal text.
virtual void removeItem(const std::string &key)=0
Delete the item stored under a key. Does nothing when there is none.
void setItem(const std::string &key, ValueType value)
Store a boolean under a key, as the text "true" or "false".
A C++20 UI framework for desktop and XR, built as an entity-component-system.