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