KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
AbstractThreadClass Class Referenceabstract

Abstract base class for QObject-based workers that run in a dedicated QThread. More...

#include <abstractthreadclass.h>

+ Inheritance diagram for AbstractThreadClass:
+ Collaboration diagram for AbstractThreadClass:

Signals

void started ()
 Emitted when the worker thread has started.
 
void finished ()
 Emitted when the worker thread has finished.
 

Public Member Functions

 AbstractThreadClass (const Log::LogCategory &category=Log::LogCategory(), QObject *parent=nullptr)
 Construct with an optional log category and parent.
 
 AbstractThreadClass (const QString &category, QObject *parent=nullptr)
 Construct with a category name string and optional parent.
 
virtual ~AbstractThreadClass ()
 Destructor — stops the thread if still running.
 
virtual bool start (const TimeSpan &timeout=TimeSpan::zero())
 Start the worker thread and optionally wait for it to signal readiness.
 
virtual bool stop (const TimeSpan &timeout=TimeSpan::zero())
 Request the worker thread to stop and optionally wait for it to finish.
 
virtual void abort ()
 Immediately interrupt the worker thread (sets the stopping flag).
 
bool waitForCompletion (const TimeSpan &timeout=TimeSpan::zero())
 Block until the worker thread finishes or the timeout elapses.
 
bool isRunning () const
 Test whether the worker thread is currently running.
 
bool isStopping () const
 Test whether stop() has been called and the thread is winding down.
 
bool success () const
 Return whether the last run completed successfully.
 
QString completionMessage () const
 Return the completion message set by finishAndStop().
 
void setBlockingStart (bool value)
 Control whether start() blocks until the thread signals readiness.
 
- Public Member Functions inherited from LoggingBaseClass
 LoggingBaseClass (const Log::LogCategory &category=Log::LogCategory())
 Construct with an optional log category.
 
 LoggingBaseClass (const QString &category)
 Construct with a category name string.
 
Log::LogCategory logCategory () const
 Return the base log category.
 

Protected Member Functions

virtual void threadStarted ()=0
 Entry point called on the worker thread immediately after it starts.
 
virtual void threadFinished ()
 Called on the worker thread just before it exits.
 
bool waitForStart (const TimeSpan &timeout)
 Block until the thread has entered threadStarted() or the timeout elapses.
 
void finishAndStop (bool success, const QString &message=QString())
 Signal that the worker has finished, recording success and a message.
 
void writeLine (const QString &line)
 Write a line to the standard output stream.
 
void writeErrorLine (const QString &line)
 Write a line to the standard error stream.
 
- Protected Member Functions inherited from LoggingBaseClass
Log::LogCategory LVL0 () const
 Return the base (level 0) log category.
 
Log::LogCategory LVL1 () const
 Return the level-1 sub-category.
 
Log::LogCategory LVL2 () const
 Return the level-2 sub-category.
 
Log::LogCategory LVL3 () const
 Return the level-3 sub-category.
 
void logText (const char *file, int lineNumber, Log::LogLevel level, const QString &text) const
 Write a text message to the system logger using the base category.
 
void logText (const char *file, int lineNumber, Log::LogLevel level, const Log::LogCategory &category, const QString &text) const
 Write a text message to the system logger using an explicit category.
 
void logHex (const char *file, int lineNumber, Log::LogLevel level, const QByteArray &data, const QString &tag=QString()) const
 Write a hex dump to the system logger.
 
void logHex (const char *file, int lineNumber, Log::LogLevel level, const Log::LogCategory &category, const QByteArray &data, const QString &tag=QString()) const
 Write a hex dump to the system logger using an explicit category.
 
void addLogConsumer (LogConsumer *consumer)
 Register an external log consumer to receive entries from this class.
 
void removeLogConsumer (LogConsumer *consumer)
 Remove a previously added log consumer.
 

Detailed Description

Abstract base class for QObject-based workers that run in a dedicated QThread.

Manages the lifecycle of a QObject running in its own QThread.

Subclass AbstractThreadClass and implement threadStarted() to perform work on the worker thread. The thread is created and destroyed automatically by start() and stop(). The started() and finished() signals notify the main thread of state changes.

Definition at line 25 of file abstractthreadclass.h.

Constructor & Destructor Documentation

◆ AbstractThreadClass() [1/2]

AbstractThreadClass::AbstractThreadClass ( const Log::LogCategory category = Log::LogCategory(),
QObject *  parent = nullptr 
)

Construct with an optional log category and parent.

Parameters
categoryLog category for this class (default: uncategorized)
parentOptional QObject parent

◆ AbstractThreadClass() [2/2]

AbstractThreadClass::AbstractThreadClass ( const QString &  category,
QObject *  parent = nullptr 
)

Construct with a category name string and optional parent.

Parameters
categoryCategory name string
parentOptional QObject parent

Member Function Documentation

◆ completionMessage()

QString AbstractThreadClass::completionMessage ( ) const
inline

Return the completion message set by finishAndStop().

Returns
Completion message string

Definition at line 94 of file abstractthreadclass.h.

◆ finishAndStop()

void AbstractThreadClass::finishAndStop ( bool  success,
const QString &  message = QString() 
)
protected

Signal that the worker has finished, recording success and a message.

Parameters
successWhether the work completed successfully
messageOptional human-readable completion message

◆ isRunning()

bool AbstractThreadClass::isRunning ( ) const
inline

Test whether the worker thread is currently running.

Returns
true if the thread is running

Definition at line 76 of file abstractthreadclass.h.

◆ isStopping()

bool AbstractThreadClass::isStopping ( ) const
inline

Test whether stop() has been called and the thread is winding down.

Returns
true if stopping is in progress

Definition at line 82 of file abstractthreadclass.h.

◆ setBlockingStart()

void AbstractThreadClass::setBlockingStart ( bool  value)
inline

Control whether start() blocks until the thread signals readiness.

Parameters
valuetrue to make start() blocking

Definition at line 100 of file abstractthreadclass.h.

◆ start()

virtual bool AbstractThreadClass::start ( const TimeSpan timeout = TimeSpan::zero())
virtual

Start the worker thread and optionally wait for it to signal readiness.

Parameters
timeoutHow long to wait for the thread to start (zero = don't wait)
Returns
true if the thread started successfully

◆ stop()

virtual bool AbstractThreadClass::stop ( const TimeSpan timeout = TimeSpan::zero())
virtual

Request the worker thread to stop and optionally wait for it to finish.

Parameters
timeoutHow long to wait for the thread to finish (zero = don't wait)
Returns
true if the thread stopped within the timeout

◆ success()

bool AbstractThreadClass::success ( ) const
inline

Return whether the last run completed successfully.

Returns
true if finishAndStop() was called with success = true

Definition at line 88 of file abstractthreadclass.h.

◆ threadFinished()

virtual void AbstractThreadClass::threadFinished ( )
inlineprotectedvirtual

Called on the worker thread just before it exits.

Definition at line 112 of file abstractthreadclass.h.

◆ threadStarted()

virtual void AbstractThreadClass::threadStarted ( )
protectedpure virtual

Entry point called on the worker thread immediately after it starts.

Subclasses must implement this method to perform their work. When done, call finishAndStop() to signal completion.

◆ waitForCompletion()

bool AbstractThreadClass::waitForCompletion ( const TimeSpan timeout = TimeSpan::zero())

Block until the worker thread finishes or the timeout elapses.

Parameters
timeoutMaximum wait duration (zero = wait indefinitely)
Returns
true if the thread finished within the timeout

◆ waitForStart()

bool AbstractThreadClass::waitForStart ( const TimeSpan timeout)
protected

Block until the thread has entered threadStarted() or the timeout elapses.

Parameters
timeoutMaximum wait duration
Returns
true if the thread started within the timeout

◆ writeErrorLine()

void AbstractThreadClass::writeErrorLine ( const QString &  line)
inlineprotected

Write a line to the standard error stream.

Parameters
lineText to write

Definition at line 138 of file abstractthreadclass.h.

◆ writeLine()

void AbstractThreadClass::writeLine ( const QString &  line)
inlineprotected

Write a line to the standard output stream.

Parameters
lineText to write

Definition at line 132 of file abstractthreadclass.h.


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