Refactoring.

develop
Roman Telezhynskyi 2024-02-17 11:24:26 +02:00
parent bd89d3509b
commit be5b6d5b1f
1 changed files with 39 additions and 37 deletions

View File

@ -6,20 +6,19 @@
#ifndef THREAD_DISPATCHER_H
#define THREAD_DISPATCHER_H
#include <QThread>
#include <QMetaObject>
#include <QThread>
#include <QCoreApplication>
#include <QMetaObject>
#include <QObject>
#include <functional>
#include <QThread>
#include <cassert>
#include <functional>
#include <utility>
typedef std::function<void()> voidBlock;
using voidBlock = std::function<void()>;
class WorkerClass : public QObject
{
Q_OBJECT // NOLINT
Q_OBJECT // NOLINT
public:
explicit WorkerClass(QThread *thread)
@ -28,43 +27,43 @@ public:
connect(QThread::currentThread(), &QThread::finished, this, &WorkerClass::deleteLater);
}
public slots:
void DoWork(voidBlock block)
void DoWork(const voidBlock &block)
{
block();
deleteLater();
}
};
Q_DECL_UNUSED static inline void q_dispatch_async(QThread* thread, voidBlock block);
static inline void q_dispatch_async(QThread* thread, voidBlock block)
Q_DECL_UNUSED static inline void q_dispatch_async(QThread *thread, const voidBlock &block);
static inline void q_dispatch_async(QThread *thread, const voidBlock &block)
{
qRegisterMetaType<voidBlock>("voidBlock");
qRegisterMetaType<voidBlock>("voidBlock");
WorkerClass *worker = new WorkerClass(thread);
QMetaObject::invokeMethod(worker, "DoWork", Qt::QueuedConnection, Q_ARG(voidBlock, block));
auto *worker = new WorkerClass(thread);
QMetaObject::invokeMethod(worker, "DoWork", Qt::QueuedConnection, Q_ARG(voidBlock, block));
}
Q_DECL_UNUSED static inline void q_dispatch_async_main(voidBlock block);
static inline void q_dispatch_async_main(voidBlock block)
Q_DECL_UNUSED static inline void q_dispatch_async_main(const voidBlock &block);
static inline void q_dispatch_async_main(const voidBlock &block)
{
QThread *mainThread = QCoreApplication::instance()->thread();
q_dispatch_async(mainThread, block);
QThread *mainThread = QCoreApplication::instance()->thread();
q_dispatch_async(mainThread, block);
}
typedef std::function<void(QtMsgType, const QMessageLogContext &, const QString &)> msgHandlerBlock;
using msgHandlerBlock = std::function<void(QtMsgType, const QMessageLogContext &, const QString &)>;
class MsgHandlerWorkerClass : public QObject
{
Q_OBJECT // NOLINT
Q_OBJECT // NOLINT
public:
MsgHandlerWorkerClass(QThread *thread, QtMsgType type, const QMessageLogContext &context, const QString &msg)
: m_type(type),
m_msg(msg),
m_line(context.line),
m_file(context.file),
m_function(context.function),
m_category(context.category)
MsgHandlerWorkerClass(QThread *thread, QtMsgType type, const QMessageLogContext &context, QString msg)
: m_type(type),
m_msg(std::move(msg)),
m_line(context.line),
m_file(context.file),
m_function(context.function),
m_category(context.category)
{
#ifndef V_NO_ASSERT
assert(context.version == 2);
@ -73,12 +72,15 @@ public:
connect(QThread::currentThread(), &QThread::finished, this, &WorkerClass::deleteLater);
}
public slots:
void DoWork(msgHandlerBlock block)
void DoWork(const msgHandlerBlock &block)
{
block(m_type, QMessageLogContext(qUtf8Printable(m_file), m_line, qUtf8Printable(m_function),
qUtf8Printable(m_category)), m_msg);
block(
m_type,
QMessageLogContext(qUtf8Printable(m_file), m_line, qUtf8Printable(m_function), qUtf8Printable(m_category)),
m_msg);
deleteLater();
}
private:
QtMsgType m_type;
QString m_msg;
@ -90,21 +92,21 @@ private:
QString m_category;
};
Q_DECL_UNUSED static inline void q_dispatch_async(QThread* thread, msgHandlerBlock block, QtMsgType type,
Q_DECL_UNUSED static inline void q_dispatch_async(QThread *thread, const msgHandlerBlock &block, QtMsgType type,
const QMessageLogContext &context, const QString &msg);
static inline void q_dispatch_async(QThread* thread, msgHandlerBlock block, QtMsgType type,
static inline void q_dispatch_async(QThread *thread, const msgHandlerBlock &block, QtMsgType type,
const QMessageLogContext &context, const QString &msg)
{
qRegisterMetaType<msgHandlerBlock>("msgHandlerBlock");
qRegisterMetaType<msgHandlerBlock>("msgHandlerBlock");
MsgHandlerWorkerClass *worker = new MsgHandlerWorkerClass(thread, type, context, msg);
QMetaObject::invokeMethod(worker, "DoWork", Qt::QueuedConnection, Q_ARG(msgHandlerBlock, block));
auto *worker = new MsgHandlerWorkerClass(thread, type, context, msg);
QMetaObject::invokeMethod(worker, "DoWork", Qt::QueuedConnection, Q_ARG(msgHandlerBlock, block));
}
Q_DECL_UNUSED static inline void q_dispatch_async_main(msgHandlerBlock block, QtMsgType type,
Q_DECL_UNUSED static inline void q_dispatch_async_main(const msgHandlerBlock &block, QtMsgType type,
const QMessageLogContext &context, const QString &msg);
static inline void q_dispatch_async_main(msgHandlerBlock block, QtMsgType type, const QMessageLogContext &context,
const QString &msg)
static inline void q_dispatch_async_main(const msgHandlerBlock &block, QtMsgType type,
const QMessageLogContext &context, const QString &msg)
{
QThread *mainThread = QCoreApplication::instance()->thread();
q_dispatch_async(mainThread, block, type, context, msg);