1#ifndef FRAY_STATE_MANAGER_HPP
2#define FRAY_STATE_MANAGER_HPP
8#include <initializer_list>
38 using Binary = std::vector<std::uint8_t>;
44 using Value = std::variant<std::string, std::int64_t, double, bool, Binary>;
48 using Statement = std::unique_ptr<sqlite3_stmt,
decltype(&sqlite3_finalize)>;
56 sqlite3*
_db =
nullptr;
113 const std::string& name)
115 static constexpr F_char hex[] =
"0123456789abcdef";
116 std::string result =
"fray_state_";
117 for (
unsigned char byte : name)
119 result += hex[
byte >> 4];
120 result += hex[
byte & 15];
132 const std::string& name)
139 const std::string sql =
140 "CREATE TABLE IF NOT EXISTS " + name +
141 " (key TEXT PRIMARY KEY NOT NULL, type INTEGER NOT NULL, value NOT NULL, "
142 "CHECK((type = 1 AND typeof(value) = 'text') OR "
143 "(type = 2 AND typeof(value) = 'integer') OR "
144 "(type = 3 AND typeof(value) = 'real') OR "
145 "(type = 4 AND typeof(value) = 'integer' AND value IN (0, 1)) OR "
146 "(type = 5 AND typeof(value) = 'blob')))";
147 if (sqlite3_exec(
_db, sql.c_str(),
nullptr,
nullptr,
nullptr) != SQLITE_OK)
163 const int result = sqlite3_close_v2(
_db);
164 if (result == SQLITE_OK)
176 if (sqlite3_exec(
_db,
"ROLLBACK",
nullptr,
nullptr,
nullptr) != SQLITE_OK)
186 const std::string& key)
const
188 if (key.empty() || key.find(
'\0') != std::string::npos)
207 sqlite3_stmt* raw =
nullptr;
208 const int prepared = sqlite3_prepare_v2(
_db,
209 "SELECT type FROM sqlite_master WHERE type IN ('table', 'view') AND name = ? COLLATE NOCASE",
211 Statement statement(raw, sqlite3_finalize);
212 if (prepared != SQLITE_OK ||
213 sqlite3_bind_text(statement.get(), 1, name, -1, SQLITE_STATIC) != SQLITE_OK)
218 const int result = sqlite3_step(statement.get());
219 if (result != SQLITE_ROW && result != SQLITE_DONE)
224 if (result == SQLITE_ROW)
226 const auto* type =
reinterpret_cast<const F_char*
>(sqlite3_column_text(statement.get(), 0));
227 if (type ==
nullptr || std::strcmp(type,
"table") != 0)
233 exists = result == SQLITE_ROW;
246 std::initializer_list<const F_char*> expected)
248 sqlite3_stmt* raw =
nullptr;
249 const int prepared = sqlite3_prepare_v2(
_db,
"SELECT name FROM pragma_table_xinfo(?)",
251 Statement statement(raw, sqlite3_finalize);
252 if (prepared != SQLITE_OK ||
253 sqlite3_bind_text(statement.get(), 1, table, -1, SQLITE_STATIC) != SQLITE_OK)
258 std::size_t count = 0;
260 while ((result = sqlite3_step(statement.get())) == SQLITE_ROW)
262 const auto* name =
reinterpret_cast<const F_char*
>(sqlite3_column_text(statement.get(), 0));
265 for (
const F_char* candidate : expected)
266 if (std::strcmp(name, candidate) == 0)
275 if (result != SQLITE_DONE || count != expected.size())
296 const std::string error = sqlite3_errmsg(
_db);
304 return fray::errors::STATE_NOT_FOUND;
316 const std::string& text)
318 static constexpr F_char hex[] =
"0123456789abcdef";
320 for (
unsigned char byte : text)
322 if (
byte ==
'\\' ||
byte ==
'"')
325 result +=
static_cast<F_char>(byte);
327 else if (byte < 0x20 || byte >= 0x7f)
330 result += hex[
byte >> 4];
331 result += hex[
byte & 15];
334 result +=
static_cast<F_char>(byte);
345 const std::string& key,
348 std::string formatted;
349 if (
const auto* text = std::get_if<std::string>(&value))
350 formatted =
"\"" +
escaped(*text) +
"\"";
351 else if (
const auto* integer = std::get_if<std::int64_t>(&value))
352 formatted = std::to_string(*integer);
353 else if (
const auto* real = std::get_if<double>(&value))
356 std::snprintf(buffer,
sizeof(buffer),
"%.*g", std::numeric_limits<double>::max_digits10, *real);
359 else if (
const auto*
boolean = std::get_if<bool>(&value))
360 formatted = *
boolean ?
"true" :
"false";
363 static constexpr F_char hex[] =
"0123456789abcdef";
365 for (std::uint8_t
byte : std::get<Binary>(value))
367 formatted += hex[
byte >> 4];
368 formatted += hex[
byte & 15];
383 const std::string& key,
387 std::lock_guard<std::recursive_mutex> lock(
_mutex);
391 return fray::errors::FAILED_SAVE_STATE;
394 return fray::errors::FAILED_SAVE_STATE;
396 return fray::errors::INVALID_STATE_KEY;
397 if (
const auto* real = std::get_if<double>(&value); real !=
nullptr && !std::isfinite(*real))
400 return fray::errors::FAILED_SAVE_STATE;
402 sqlite3_stmt* raw =
nullptr;
403 const std::string sql =
"INSERT INTO " +
_table +
404 " (key, type, value) VALUES (?, ?, ?) "
405 "ON CONFLICT(key) DO UPDATE SET type = excluded.type, value = excluded.value";
406 int prepared = sqlite3_prepare_v2(
_db, sql.c_str(), -1, &raw,
nullptr);
407 if (prepared != SQLITE_OK)
409 const std::string error = sqlite3_errmsg(
_db);
410 sqlite3_finalize(raw);
414 return fray::errors::FAILED_SAVE_STATE;
418 return fray::errors::FAILED_SAVE_STATE;
423 return fray::errors::FAILED_SAVE_STATE;
426 prepared = sqlite3_prepare_v2(
_db, sql.c_str(), -1, &raw,
nullptr);
428 Statement statement(raw, sqlite3_finalize);
429 if (prepared != SQLITE_OK)
432 return fray::errors::FAILED_SAVE_STATE;
434 int bound = sqlite3_bind_text64(statement.get(), 1, key.data(), key.size(), SQLITE_TRANSIENT, SQLITE_UTF8);
435 if (bound == SQLITE_OK)
436 bound = sqlite3_bind_int(statement.get(), 2,
static_cast<int>(value.index()) + 1);
437 if (bound == SQLITE_OK)
439 if (
const auto* text = std::get_if<std::string>(&value))
440 bound = sqlite3_bind_text64(statement.get(), 3, text->data(), text->size(), SQLITE_TRANSIENT, SQLITE_UTF8);
441 else if (
const auto* integer = std::get_if<std::int64_t>(&value))
442 bound = sqlite3_bind_int64(statement.get(), 3, *integer);
443 else if (
const auto* real = std::get_if<double>(&value))
444 bound = sqlite3_bind_double(statement.get(), 3, *real);
445 else if (
const auto*
boolean = std::get_if<bool>(&value))
446 bound = sqlite3_bind_int(statement.get(), 3, *
boolean ? 1 : 0);
449 const auto& binary = std::get<Binary>(value);
450 bound = binary.empty()
451 ? sqlite3_bind_zeroblob(statement.get(), 3, 0)
452 : sqlite3_bind_blob64(statement.get(), 3, binary.data(), binary.size(), SQLITE_TRANSIENT);
455 if (bound != SQLITE_OK)
458 return fray::errors::FAILED_SAVE_STATE;
460 if (sqlite3_step(statement.get()) != SQLITE_DONE)
462 const std::string error = sqlite3_errmsg(
_db);
466 return fray::errors::FAILED_SAVE_STATE;
468 return write(key, value,
false);
470 return fray::errors::FAILED_SAVE_STATE;
474 return fray::errors::NONE;
487 const std::string& key,
491 std::lock_guard<std::recursive_mutex> lock(
_mutex);
495 return fray::errors::FAILED_LOAD_STATE;
498 return fray::errors::FAILED_LOAD_STATE;
500 return fray::errors::INVALID_STATE_KEY;
501 sqlite3_stmt* raw =
nullptr;
502 const std::string sql =
"SELECT type, value FROM " +
_table +
" WHERE key = ?";
503 const int prepared = sqlite3_prepare_v2(
_db, sql.c_str(), -1, &raw,
nullptr);
504 Statement statement(raw, sqlite3_finalize);
505 if (prepared != SQLITE_OK)
507 return statementFailure(statement,
"load", fray::errors::FAILED_LOAD_STATE);
509 if (sqlite3_bind_text64(statement.get(), 1, key.data(), key.size(), SQLITE_TRANSIENT, SQLITE_UTF8) != SQLITE_OK)
512 return fray::errors::FAILED_LOAD_STATE;
514 const int result = sqlite3_step(statement.get());
515 if (result == SQLITE_DONE)
518 return fray::errors::STATE_NOT_FOUND;
520 if (result != SQLITE_ROW)
522 return statementFailure(statement,
"load", fray::errors::FAILED_LOAD_STATE);
524 if (sqlite3_column_type(statement.get(), 0) != SQLITE_INTEGER)
527 return fray::errors::FAILED_LOAD_STATE;
529 const sqlite3_int64 storedType = sqlite3_column_int64(statement.get(), 0);
530 if (storedType < 1 || storedType > 5)
532 if (storedType != expectedType)
535 return fray::errors::STATE_TYPE_MISMATCH;
537 if constexpr (std::is_same_v<T, std::string>)
539 if (sqlite3_column_type(statement.get(), 1) != SQLITE_TEXT)
541 const auto* text =
reinterpret_cast<const char*
>(sqlite3_column_text(statement.get(), 1));
544 output.assign(text, sqlite3_column_bytes(statement.get(), 1));
546 else if constexpr (std::is_same_v<T, std::int64_t>)
548 if (sqlite3_column_type(statement.get(), 1) != SQLITE_INTEGER)
550 output = sqlite3_column_int64(statement.get(), 1);
552 else if constexpr (std::is_same_v<T, double>)
554 if (sqlite3_column_type(statement.get(), 1) != SQLITE_FLOAT ||
555 !std::isfinite(sqlite3_column_double(statement.get(), 1)))
557 output = sqlite3_column_double(statement.get(), 1);
559 else if constexpr (std::is_same_v<T, bool>)
561 if (sqlite3_column_type(statement.get(), 1) != SQLITE_INTEGER)
563 const auto number = sqlite3_column_int64(statement.get(), 1);
564 if (number != 0 && number != 1)
566 output = number != 0;
570 if (sqlite3_column_type(statement.get(), 1) != SQLITE_BLOB)
572 const int size = sqlite3_column_bytes(statement.get(), 1);
573 const auto* bytes =
static_cast<const std::uint8_t*
>(sqlite3_column_blob(statement.get(), 1));
574 if (size > 0 && bytes ==
nullptr)
579 output.assign(bytes, bytes + size);
581 return fray::errors::NONE;
591 return fray::errors::FAILED_LOAD_STATE;
618 const int result = sqlite3_close_v2(
_db);
619 if (result != SQLITE_OK)
636 const std::string& table =
"Fray",
638 bool requireExistingTable =
false)
640 std::lock_guard<std::recursive_mutex> lock(
_mutex);
644 return fray::errors::FAILED_OPEN_STORE;
646 if (
_db !=
nullptr || table.empty() || table.find(
'\0') != std::string::npos)
649 return fray::errors::FAILED_OPEN_STORE;
651 std::filesystem::path path;
652 if (
files.filePath(
files.databaseFileName(), path) != fray::errors::NONE)
653 return fray::errors::FAILED_OPEN_STORE;
654 std::error_code pathError;
655 const auto status = std::filesystem::symlink_status(path, pathError);
656 if ((pathError && pathError != std::errc::no_such_file_or_directory) ||
657 std::filesystem::is_symlink(status))
660 return fray::errors::FAILED_OPEN_STORE;
662 const std::string filename = path.string();
663 const int flags = SQLITE_OPEN_READWRITE | (
files.requiresExistingDatabase() ? 0 : SQLITE_OPEN_CREATE);
664 const int result = sqlite3_open_v2(filename.c_str(), &
_db, flags,
nullptr);
665 if (result != SQLITE_OK)
668 _db !=
nullptr ? sqlite3_errmsg(
_db) : sqlite3_errstr(result));
670 return fray::errors::FAILED_OPEN_STORE;
672 if (sqlite3_exec(
_db,
"BEGIN IMMEDIATE",
nullptr,
nullptr,
nullptr) != SQLITE_OK)
676 return fray::errors::FAILED_OPEN_STORE;
678 const std::string requestedTable =
tableName(table);
680 if ((requireExistingTable &&
681 (!
tableExists(requestedTable.c_str(), exists) || !exists ||
683 (!requireExistingTable && !
createTable(requestedTable)))
685 if (requireExistingTable && !exists)
689 return fray::errors::FAILED_OPEN_STORE;
691 if (sqlite3_exec(
_db,
"COMMIT",
nullptr,
nullptr,
nullptr) != SQLITE_OK)
696 return fray::errors::FAILED_OPEN_STORE;
704 return fray::errors::NONE;
724 std::lock_guard<std::recursive_mutex> lock(
_mutex);
725 return _db !=
nullptr;
734 template<
typename Function>
738 std::lock_guard<std::recursive_mutex> lock(
_mutex);
742 return fray::errors::FAILED_HISTORY;
744 return function(
_db);
759 std::lock_guard<std::recursive_mutex> lock(
_mutex);
763 return fray::errors::FAILED_CLOSE_STORE;
765 const int result = sqlite3_close_v2(
_db);
766 if (result != SQLITE_OK)
769 return fray::errors::FAILED_CLOSE_STORE;
774 return fray::errors::NONE;
785 const std::string& key,
786 const std::string& value) {
return write(key,
Value(value)); }
795 const std::string& key,
798 if (value ==
nullptr)
801 return fray::errors::FAILED_SAVE_STATE;
803 return set(key, std::string(value));
813 const std::string& key,
814 double value) {
return write(key,
Value(value)); }
823 const std::string& key,
824 bool value) {
return write(key,
Value(value)); }
833 const std::string& key,
843 template<
typename T, std::enable_if_t<std::is_
integral_v<T> && !std::is_same_v<T,
bool>,
int> = 0>
845 const std::string& key,
848 if constexpr (std::is_unsigned_v<T>)
850 if (
static_cast<std::uintmax_t
>(value) >
851 static_cast<std::uintmax_t
>(std::numeric_limits<std::int64_t>::max()))
854 return fray::errors::STATE_VALUE_OUT_OF_RANGE;
857 return write(key,
Value(
static_cast<std::int64_t
>(value)));
868 const std::string& key,
869 std::string& value) {
return read(key, value, 1); }
878 const std::string& key,
879 double& value) {
return read(key, value, 3); }
888 const std::string& key,
889 bool& value) {
return read(key, value, 4); }
898 const std::string& key,
899 Binary& value) {
return read(key, value, 5); }
909 const std::string& key,
912 std::lock_guard<std::recursive_mutex> lock(
_mutex);
916 return fray::errors::FAILED_LOAD_STATE;
919 return fray::errors::FAILED_LOAD_STATE;
921 return fray::errors::INVALID_STATE_KEY;
922 sqlite3_stmt* raw =
nullptr;
923 const std::string sql =
"SELECT 1 FROM " +
_table +
" WHERE key = ?";
924 const int prepared = sqlite3_prepare_v2(
_db, sql.c_str(), -1, &raw,
nullptr);
925 Statement statement(raw, sqlite3_finalize);
926 if (prepared != SQLITE_OK)
927 return statementFailure(statement,
"inspect", fray::errors::FAILED_LOAD_STATE);
928 if (sqlite3_bind_text64(statement.get(), 1, key.data(), key.size(), SQLITE_TRANSIENT, SQLITE_UTF8) != SQLITE_OK)
931 return fray::errors::FAILED_LOAD_STATE;
933 const int result = sqlite3_step(statement.get());
934 if (result != SQLITE_ROW && result != SQLITE_DONE)
935 return statementFailure(statement,
"inspect", fray::errors::FAILED_LOAD_STATE);
936 exists = result == SQLITE_ROW;
937 return fray::errors::NONE;
948 const std::string& key,
952 const F_err result =
read(key, stored, 3);
953 if (result != fray::errors::NONE)
955 if (std::abs(stored) > std::numeric_limits<float>::max() ||
956 (stored != 0 && std::abs(stored) < std::numeric_limits<float>::denorm_min()))
959 return fray::errors::STATE_VALUE_OUT_OF_RANGE;
961 value =
static_cast<float>(stored);
962 return fray::errors::NONE;
972 template<
typename T, std::enable_if_t<std::is_
integral_v<T> && !std::is_same_v<T,
bool>,
int> = 0>
974 const std::string& key,
978 const F_err result =
read(key, stored, 2);
979 if (result != fray::errors::NONE)
982 if constexpr (std::is_signed_v<T>)
984 if constexpr (std::numeric_limits<T>::digits < std::numeric_limits<std::int64_t>::digits)
985 inRange = stored >=
static_cast<std::int64_t
>(std::numeric_limits<T>::min()) &&
986 stored <= static_cast<std::int64_t>(std::numeric_limits<T>::max());
989 inRange = stored >= 0 &&
990 static_cast<std::uint64_t
>(stored) <= std::numeric_limits<T>::max();
994 return fray::errors::STATE_VALUE_OUT_OF_RANGE;
996 value =
static_cast<T
>(stored);
997 return fray::errors::NONE;
1007 const std::string& key)
1009 std::lock_guard<std::recursive_mutex> lock(
_mutex);
1013 return fray::errors::FAILED_REMOVE_STATE;
1016 return fray::errors::FAILED_REMOVE_STATE;
1018 return fray::errors::INVALID_STATE_KEY;
1019 sqlite3_stmt* raw =
nullptr;
1020 const std::string sql =
"DELETE FROM " +
_table +
" WHERE key = ?";
1021 const int prepared = sqlite3_prepare_v2(
_db, sql.c_str(), -1, &raw,
nullptr);
1022 Statement statement(raw, sqlite3_finalize);
1023 if (prepared != SQLITE_OK)
1025 return statementFailure(statement,
"remove", fray::errors::FAILED_REMOVE_STATE);
1027 if (sqlite3_bind_text64(statement.get(), 1, key.data(), key.size(), SQLITE_TRANSIENT, SQLITE_UTF8) != SQLITE_OK)
1030 return fray::errors::FAILED_REMOVE_STATE;
1032 if (sqlite3_step(statement.get()) != SQLITE_DONE)
1034 return statementFailure(statement,
"remove", fray::errors::FAILED_REMOVE_STATE);
1036 if (sqlite3_changes(
_db) == 0)
1039 return fray::errors::STATE_NOT_FOUND;
1042 return fray::errors::NONE;
1052 std::lock_guard<std::recursive_mutex> lock(
_mutex);
1056 return fray::errors::FAILED_REMOVE_STATE;
1059 return fray::errors::FAILED_REMOVE_STATE;
1062 return fray::errors::FAILED_REMOVE_STATE;
1066 return fray::errors::STATE_NOT_FOUND;
1068 const std::string sql =
"DROP TABLE " +
_table;
1069 if (sqlite3_exec(
_db, sql.c_str(),
nullptr,
nullptr,
nullptr) != SQLITE_OK)
1072 return fray::errors::FAILED_REMOVE_STATE;
1074 return fray::errors::NONE;
1084 const std::string& name)
1086 std::lock_guard<std::recursive_mutex> lock(
_mutex);
1087 if (
_db ==
nullptr || name.empty() || name.find(
'\0') != std::string::npos)
1090 return fray::errors::FAILED_RENAME_STATE_TABLE;
1093 return fray::errors::FAILED_RENAME_STATE_TABLE;
1096 return fray::errors::FAILED_RENAME_STATE_TABLE;
1100 return fray::errors::STATE_NOT_FOUND;
1102 const std::string renamed =
tableName(name);
1104 return fray::errors::NONE;
1106 return fray::errors::FAILED_RENAME_STATE_TABLE;
1110 return fray::errors::FAILED_RENAME_STATE_TABLE;
1112 const std::string sql =
"ALTER TABLE " +
_table +
" RENAME TO " + renamed;
1113 if (sqlite3_exec(
_db, sql.c_str(),
nullptr,
nullptr,
nullptr) != SQLITE_OK)
1116 return fray::errors::FAILED_RENAME_STATE_TABLE;
1119 return fray::errors::NONE;
Owns one working directory, its database name and a nonblocking file lock. Project workspaces additio...
F_err write(const std::string &key, const Value &value, F_bool retry=true)
Insert or replace a typed value in the state table. Locks _mutex, may recreate a missing table once,...
bool isOpen() const
Check whether a database connection is open. Locks _mutex.
std::unique_ptr< sqlite3_stmt, decltype(&sqlite3_finalize)> Statement
SQLite statement owner that finalizes its statement on destruction.
StateManager(const StateManager &)=delete
Disallow copying the owned connection.
F_err set(const std::string &key, const std::string &value)
Save a string state value. The underlying write locks _mutex and logs successful writes.
F_err open(fray::files::FileManager &files, const std::string &table="Fray", const std::string &applicationId="", bool requireExistingTable=false)
Open and initialize a SQLite state table in the file manager's directory. Locks _mutex,...
F_bool tableExists(const F_char *name, F_bool &exists)
Inspect whether an SQLite table exists, rejecting a matching view. Requires an open database and _mut...
static std::string escaped(const std::string &text)
Escape bytes for diagnostic log output.
F_err removeTable()
Drop the current state table without closing the store. Locks _mutex; a later write can recreate the ...
F_err remove(const std::string &key)
Remove a state key. Locks _mutex.
F_err renameTable(const std::string &name)
Rename the current state table to an encoded logical name. Locks _mutex and updates the active table ...
std::recursive_mutex & mutex()
Return the store mutex, held by the owner across close and reopen during file rename.
F_bool projectAvailable()
Check that the borrowed file manager still exists. Logs on failure; callers accessing store state mus...
std::weak_ptr< int > _filesLifetime
Weak lifetime token guarding access to the borrowed file manager.
F_err get(const std::string &key, T &value)
Read an integer state value into a checked destination type. The underlying read locks _mutex; value ...
std::string _table
Encoded name of the active state table.
F_void rollback()
Roll back the current SQLite transaction and log any failure. Requires an open database and _mutex he...
F_err close()
Close the database and detach the project. Locks _mutex; logs errors.
F_err invalidStoredValue()
Log an invalid stored value.
~StateManager()
Close the connection and detach from the file manager if still alive. The owner must stop concurrent ...
static std::string tableName(const std::string &name)
Encode a logical table name as a safe SQLite identifier.
F_err set(const std::string &key, bool value)
Save a boolean state value. The underlying write locks _mutex and logs successful writes.
F_err set(const std::string &key, const F_char *value)
Save a NUL-terminated string; null is rejected state value. The underlying write locks _mutex and log...
F_err get(const std::string &key, double &value)
Read a double state value. The underlying read locks _mutex; value is unchanged on failure.
static F_void logSet(const std::string &key, const Value &value)
Log a successfully saved key and its formatted value.
F_err get(const std::string &key, std::string &value)
Read a string state value. The underlying read locks _mutex; value is unchanged on failure.
F_err set(const std::string &key, const Binary &value)
Save a binary blob state value. The underlying write locks _mutex and logs successful writes.
F_void releaseProject()
Detach the live file manager and clear the associated project metadata. Call while holding _mutex,...
const std::string & projectName() const
Return the project name captured at open; its contents change on open or close. Does not acquire _mut...
std::recursive_mutex _mutex
Serializes database and mutable store operations; recursive for nested calls.
sqlite3 * _db
Owned SQLite connection, or null when closed.
std::vector< std::uint8_t > Binary
Byte sequence stored as a SQLite blob.
fray::files::FileManager * _files
Borrowed attached project file manager, valid only while its lifetime token is alive.
std::variant< std::string, std::int64_t, double, bool, Binary > Value
Supported state value; variant order defines persistent type tags and must remain stable.
std::string _applicationId
Application identifier captured when the store opened.
std::string _projectName
Project name captured when the store opened.
F_err read(const std::string &key, T &output, int expectedType)
Read and validate a typed state value. Locks _mutex; output is changed only on a successful read.
F_err get(const std::string &key, Binary &value)
Read a binary blob state value. The underlying read locks _mutex; value is unchanged on failure.
StateManager()=default
Construct a closed state manager.
F_err get(const std::string &key, float &value)
Read a float state value. The underlying read locks _mutex; value is unchanged on failure.
F_bool validKey(const std::string &key) const
Validate a state key and log invalid input.
F_err set(const std::string &key, double value)
Save a finite double state value. The underlying write locks _mutex and logs successful writes.
F_err contains(const std::string &key, F_bool &exists)
Check whether a state key exists. Locks _mutex.
F_err withDatabase(Function &&function)
Run a callback against the open database for supporting tables. Holds _mutex throughout the callback ...
F_err set(const std::string &key, T value)
Save an integral value as signed 64-bit state. The underlying write locks _mutex and logs successful ...
F_void closeAfterOpenFailure()
Close the SQLite connection after initialization fails. Requires _mutex held; logs a close failure.
F_bool createTable(const std::string &name)
Create a typed state table or validate an existing table's columns. Requires an open database and _mu...
F_err get(const std::string &key, bool &value)
Read a boolean state value. The underlying read locks _mutex; value is unchanged on failure.
const std::string & applicationId() const
Return the application identifier captured at open; its contents change on open or close....
StateManager & operator=(const StateManager &)=delete
Disallow copy assignment of the owned connection.
F_bool hasExpectedColumns(const F_char *table, std::initializer_list< const F_char * > expected)
Check that a table has exactly the named columns. Requires an open database and _mutex held; column t...
F_err statementFailure(Statement &statement, const F_char *operation, F_err failure)
Classify and log a failed state statement after finalizing it. Requires an open database and _mutex h...
F_long F_err
Error return type used across the Engine; the value is a fray::errors::code (0 / NONE means success).
F_void engineInfo(const F_char *fmt,...)
Log info for Engine.
F_void engineError(const F_char *fmt,...)
Log error for Engine.
void F_void
Alias for void.
bool F_bool
Alias for bool.
char F_char
Alias for char.