Key-value storage kept in a file: the place for data that must be found again at the next start (preferences, a saved session).
More...
|
| | LocalStorage (const std::filesystem::path &storageFilePath=defaultStoragePath()) noexcept(false) |
| | Open the storage file, creating it and its parent directories when they do not exist.
|
|
| ~LocalStorage (void) override |
| | Close the database. The items stay in the 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.
|
| 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.
|
| void | clear (void) noexcept(false) override |
| | Delete every item of the storage file.
|
| bool | isPersistent (void) const noexcept |
| | Tell whether the items are written to the storage file.
|
| void | setItem (const std::string &key, std::string_view value) |
| | Store a copy of a string view under a key.
|
| void | setItem (const std::string &key, const char *value) |
| | Store a copy of a C string under a key.
|
template<typename ValueType>
requires (std::is_same_v<std::remove_cvref_t<ValueType>, bool>) |
| void | setItem (const std::string &key, ValueType value) |
| | Store a boolean under a key, as the text "true" or "false".
|
|
virtual | ~Storage (void)=default |
| | Virtual destructor.
|
| void | setItem (const std::string &key, std::string_view value) |
| | Store a copy of a string view under a key.
|
| void | setItem (const std::string &key, const char *value) |
| | Store a copy of a C string under a key.
|
template<typename ValueType>
requires (std::is_same_v<std::remove_cvref_t<ValueType>, bool>) |
| void | setItem (const std::string &key, ValueType value) |
| | Store a boolean under a key, as the text "true" or "false".
|
template<typename ValueType>
requires (std::is_arithmetic_v<ValueType> && !std::is_same_v<ValueType, bool>) |
| void | setItem (const std::string &key, ValueType value) |
| | Store a number under a key, as its decimal text.
|
| template<typename ValueType> |
| std::optional< ValueType > | getItemAs (const std::string &key) |
| | Read the item stored under a key and convert it to a type.
|
Key-value storage kept in a file: the place for data that must be found again at the next start (preferences, a saved session).
The items live in an SQLite database file, by default .guillaume-local-storage.db in the current working directory. Two LocalStorage objects opened on the same path see the same items. The typed overloads of Storage (numbers, booleans, getItemAs) are available.
storage.setItem("theme", "dark");
const auto theme = storage.getItem(
"theme");
Key-value storage kept in a file: the place for data that must be found again at the next start (pref...
Design tokens: colors, type scale, shapes, elevation, motion and spacing. Read them through guillaume...
Things to watch out for:
- When the file cannot be opened or created, the storage logs an error and works in memory instead: nothing is saved. Check isPersistent() when that matters.
- Every operation takes a lock, so one object can be used from several threads. Another process holding the file is waited for, up to five seconds.
- The class owns a database connection and is not meant to be copied.
- See also
- Storage
-
SessionStorage
Definition at line 71 of file local_storage.hpp.