MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
mostly_harmless::data::DatabaseState Class Referencefinal

Represents a connection to a sqlite database. More...

#include <mostlyharmless_DatabaseState.h>

Public Member Functions

 DatabaseState (const DatabaseState &)=delete
 
 DatabaseState (DatabaseState &&other) noexcept
 
DatabaseStateoperator= (const DatabaseState &)=delete
 
DatabaseStateoperator= (DatabaseState &&other) noexcept
 
 ~DatabaseState () noexcept
 
template<DatabaseStorageType T>
auto set (std::string_view name, const T &toSet) -> void
 
template<DatabaseStorageType T>
auto get (std::string_view name) -> std::optional< T >
 
auto duplicate () const -> std::optional< DatabaseState >
 

Static Public Member Functions

static auto tryCreate (const std::filesystem::path &location, const std::vector< std::pair< std::string, DatabaseValueVariant > > &initialValues) -> std::optional< DatabaseState >
 

Detailed Description

Represents a connection to a sqlite database.

The rationale behind this class' existence is to provide a unified "standard" layout for a plugin's global state - things like settings, statistics or anything else you might need to share between all instances. It doesn't directly support any kind of IPC notifiers, and is assumed that if a user wants synchronisation between instances using this class, they'll either poll the values they need on a timer, or set up their own filewatching mechanism.

The database schema itself is fairly generic, a single DATA table, with NAME, TEXT_VALUE, BOOL_VALUE, INT_VALUE, FLOAT_VALUE and `DOUBLE_VALUE fields. get and set are templated, and will get or set the field associated with the given template type - so it's important to ensure you retrieve the correct value type for a field you've previously set. The fields default to "", false, 0, 0.0f and 0.0 respectively. The connection is set to WAL mode, to allow concurrent access.

Constructor & Destructor Documentation

◆ DatabaseState() [1/2]

mostly_harmless::data::DatabaseState::DatabaseState ( const DatabaseState & )
delete

Non Copyable, as the database connection pointer will be closed on destruction...

◆ DatabaseState() [2/2]

mostly_harmless::data::DatabaseState::DatabaseState ( DatabaseState && other)
inlinenoexcept

Moveable, nulls other's connection pointer

Parameters
otherThe moved-from DatabaseState instance

◆ ~DatabaseState()

mostly_harmless::data::DatabaseState::~DatabaseState ( )
inlinenoexcept

The internal database handle is closed if not null.

Member Function Documentation

◆ duplicate()

auto mostly_harmless::data::DatabaseState::duplicate ( ) const -> std::optional<DatabaseState>
inlinenodiscard

Tries to create a copy of the current database connection. This is useful as database connections are only safe to use on a single thread - so for polling for changes on a background thread for example, a new connection must be used.

Will not work with an in-memory database created with filename = ":memory:".

Returns
A copy of the current connection if succeeded, nullopt otherwise.

◆ get()

template<DatabaseStorageType T>
auto mostly_harmless::data::DatabaseState::get ( std::string_view name) -> std::optional<T>
inlinenodiscard

Attempts to retrieve the value of a named field in the database.

Template Parameters
TA std::string, bool, any int, float, or double.
Parameters
nameThe name of the database field to retrieve
Returns
The value of the retrieved database field if found, nullopt otherwise.

◆ operator=() [1/2]

DatabaseState & mostly_harmless::data::DatabaseState::operator= ( const DatabaseState & )
delete

Non Copyable, as the database connection pointer will be closed on destruction...

◆ operator=() [2/2]

DatabaseState & mostly_harmless::data::DatabaseState::operator= ( DatabaseState && other)
inlinenoexcept

Moveable, nulls other's connection pointer

Parameters
otherThe moved-from DatabaseState instance
Returns
*this

◆ set()

template<DatabaseStorageType T>
auto mostly_harmless::data::DatabaseState::set ( std::string_view name,
const T & toSet ) -> void
inline

Sets the value of a database entry with a matching name. If not found, will first create the entry.

Template Parameters
TA std::string, bool, any int, float, or double
Parameters
nameThe name of the field to set.
toSetThe value to set.

◆ tryCreate()

static auto mostly_harmless::data::DatabaseState::tryCreate ( const std::filesystem::path & location,
const std::vector< std::pair< std::string, DatabaseValueVariant > > & initialValues ) -> std::optional<DatabaseState>
inlinestaticnodiscard

Attempts to create a new DatabaseState instance. On construction, will attempt to open the database passed to location, and establish a connection to it if it exists. If it doesn't exist, creates the database, and a table to store user data in.

Parameters
locationA path to the database to create or open.
initialValuesA vector containing the initial values to add to the database if it didn't exist. If the database DID exist, but any of the keys in the vector aren't present, they'll be added with the values specified and existing items will be skipped.
Returns
A DatabaseState instance on success, nullopt otherwise.

The documentation for this class was generated from the following file: