CuteHMI - Shared Database (CuteHMI.SharedDatabase.0)
cutehmi::shareddatabase::DatabaseWorker Class Reference

Database worker. More...

#include <cutehmi/shareddatabase/DatabaseWorker.hpp>

Inheritance diagram for cutehmi::shareddatabase::DatabaseWorker:
QObject

Signals

void started ()
 Work started. More...
 
void ready ()
 Results are ready. More...
 
void refused (const QString &reason)
 Worker went on a strike action. More...
 

Public Member Functions

 DatabaseWorker (const QString &connectionName, std::function< void(QSqlDatabase &db)> task=nullptr)
 Constructor. More...
 
void setTask (std::function< void(QSqlDatabase &db)> task)
 Set task. More...
 
virtual void job (QSqlDatabase &db)
 Worker's job. More...
 
void wait () const
 Wait for the worker. More...
 
bool isReady () const
 Check if worker is ready. More...
 
bool isWorking () const
 Check if worker is working. More...
 
void work ()
 Do the work. More...
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual ~QObject ()
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Protected Member Functions

QThreaddbThread () const
 Get database thread. More...
 
QSqlDatabasedb ()
 Get database connection. More...
 
const QSqlDatabasedb () const
 Get database connection. More...
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

Database worker.

Convenient worker class that runs tasks in the same thread as where database connection lives.

Constructor & Destructor Documentation

◆ DatabaseWorker()

cutehmi::shareddatabase::DatabaseWorker::DatabaseWorker ( const QString connectionName,
std::function< void(QSqlDatabase &db)>  task = nullptr 
)
explicit

Constructor.

This constructor employs the worker in the same thread as where database connection denoted by connectionName lives.

Parameters
connectionNamedatabase connection name.
tasktask function. See job().

Member Function Documentation

◆ db() [1/2]

const QSqlDatabase * cutehmi::shareddatabase::DatabaseWorker::db ( )
protected

Get database connection.

Returns
database connection.
Warning
database connection lives inside database thread. This means that this function can not be called from a thread other than database thread, i.e. it should not be used outside the job() scope.

◆ db() [2/2]

const QSqlDatabase* cutehmi::shareddatabase::DatabaseWorker::db ( ) const
protected

Get database connection.

Returns
database connection.
Warning
database connection lives inside database thread. This means that this function can not be called from a thread other than database thread, i.e. it should not be used outside the job() scope.

◆ dbThread()

QThread * cutehmi::shareddatabase::DatabaseWorker::dbThread ( ) const
protected

Get database thread.

Returns
database thread.

◆ isReady()

bool cutehmi::shareddatabase::DatabaseWorker::isReady ( ) const

Check if worker is ready.

Returns
true when worker has completed the job, false otherwise.
Remarks
This method is thread-safe.

◆ isWorking()

bool cutehmi::shareddatabase::DatabaseWorker::isWorking ( ) const

Check if worker is working.

Returns
true if worker is processing its job, false otherwise.
Remarks
This method is thread-safe.

◆ job()

void cutehmi::shareddatabase::DatabaseWorker::job ( QSqlDatabase db)
virtual

Worker's job.

This function can be reimplemented. Default implementation calls task function if it has been set (it can be passed to the constructor or set via setTask() function). Default implementation does nothing if task has not been set (i.e. task = nullptr).

Parameters
dbdatabse connection to be used by the worker.

◆ ready

void cutehmi::shareddatabase::DatabaseWorker::ready ( )
signal

Results are ready.

This signal is emitted when the job() has been completed and the results are ready.

◆ refused

void cutehmi::shareddatabase::DatabaseWorker::refused ( const QString reason)
signal

Worker went on a strike action.

This signal is emitted when worker refused to do the job.

◆ setTask()

void cutehmi::shareddatabase::DatabaseWorker::setTask ( std::function< void(QSqlDatabase &db)>  task)

Set task.

Parameters
tasktask function. See job().

◆ started

void cutehmi::shareddatabase::DatabaseWorker::started ( )
signal

Work started.

This signal is emitted just before worker starts processing the job.

◆ wait()

void cutehmi::shareddatabase::DatabaseWorker::wait ( ) const

Wait for the worker.

Causes calling thread to wait until worker finishes its job. Function does not block until work() has been called. It also won't block if worker has already finished its job.

Remarks
This method is thread-safe.

◆ work()

void cutehmi::shareddatabase::DatabaseWorker::work ( )

Do the work.

This function tells worker to process the job() in database thread.