MostlyHarmless 0.0.1
Loading...
Searching...
No Matches
mostly_harmless::data Namespace Reference

Contains classes and functions related to data management. More...

Classes

class  DatabasePropertyWatcher
 A convenience class for responding to changes in a databaseState, potentially fired from other processes. Watches a given property's field corresponding to type T for changes, and fires a callback on change. Note that callback will run on a background timer thread, so if you need thread safety, be sure to utilise ISharedState::callOnMessageThread from your callback lambda. More...

Typedefs

using DatabaseValueVariant = std::variant<std::string, bool, int, float, double>
 A std::variant containing all types satisfying the DatabaseStorageType concept.

Functions

 DatabaseState (const DatabaseState &)=delete
 DatabaseState (DatabaseState &&other) noexcept
DatabaseState & operator= (const DatabaseState &)=delete
DatabaseState & operator= (DatabaseState &&other) noexcept
static auto tryCreate (const std::filesystem::path &location, const std::vector< std::pair< std::string, DatabaseValueVariant > > &initialValues) -> std::optional< DatabaseState >
 ~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 >

Detailed Description

Contains classes and functions related to data management.

Typedef Documentation

◆ DatabaseValueVariant

using mostly_harmless::data::DatabaseValueVariant = std::variant<std::string, bool, int, float, double>

A std::variant containing all types satisfying the DatabaseStorageType concept.

Function Documentation

◆ DatabaseState() [1/2]

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

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

◆ DatabaseState() [2/2]

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

Moveable, nulls other's connection pointer

Parameters
otherThe moved-from DatabaseState instance

◆ duplicate()

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

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::get ( std::string_view name) -> std::optional< T >
nodiscard

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::operator= ( const DatabaseState & )
delete

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

◆ operator=() [2/2]

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

Moveable, nulls other's connection pointer

Parameters
otherThe moved-from DatabaseState instance
Returns
*this

◆ set()

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

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()

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

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.

◆ ~DatabaseState()

mostly_harmless::data::~DatabaseState ( )
noexcept

The internal database handle is closed if not null.