Refactoring.
This commit is contained in:
parent
bd89d3509b
commit
be5b6d5b1f
|
@ -6,16 +6,15 @@
|
|||
#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
|
||||
{
|
||||
|
@ -28,39 +27,39 @@ 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");
|
||||
|
||||
WorkerClass *worker = new WorkerClass(thread);
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
public:
|
||||
MsgHandlerWorkerClass(QThread *thread, QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
MsgHandlerWorkerClass(QThread *thread, QtMsgType type, const QMessageLogContext &context, QString msg)
|
||||
: m_type(type),
|
||||
m_msg(msg),
|
||||
m_msg(std::move(msg)),
|
||||
m_line(context.line),
|
||||
m_file(context.file),
|
||||
m_function(context.function),
|
||||
|
@ -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");
|
||||
|
||||
MsgHandlerWorkerClass *worker = new MsgHandlerWorkerClass(thread, type, context, msg);
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user