aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlworkerscript/qquickworkerscript.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2025-06-12 10:45:15 +0200
committerUlf Hermann <ulf.hermann@qt.io>2025-06-12 17:27:14 +0200
commita68aab2413c10fbf1e344921b04530ba1ba04746 (patch)
tree1de747bd59ea04d582871120bd7b213c1d6dd129 /src/qmlworkerscript/qquickworkerscript.cpp
parenta3776349055b791969bfafecec2e3103a9195d1e (diff)
WorkerScript: Clean up event classes and enums
We only need one enumeration, and all the methods of the different events can be inline. Then we can switch over the values of the one enum. Change-Id: I7691fb95fca57b6930de84fa71fc7e778edd879c Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> Reviewed-by: Dmitrii Akshintsev <dmitrii.akshintsev@qt.io>
Diffstat (limited to 'src/qmlworkerscript/qquickworkerscript.cpp')
-rw-r--r--src/qmlworkerscript/qquickworkerscript.cpp166
1 files changed, 67 insertions, 99 deletions
diff --git a/src/qmlworkerscript/qquickworkerscript.cpp b/src/qmlworkerscript/qquickworkerscript.cpp
index 283e8adaf0..d7c9f0192f 100644
--- a/src/qmlworkerscript/qquickworkerscript.cpp
+++ b/src/qmlworkerscript/qquickworkerscript.cpp
@@ -32,63 +32,79 @@
QT_BEGIN_NAMESPACE
-class WorkerDataEvent : public QEvent
+enum class WorkerEventType
{
-public:
- enum Type { WorkerData = QEvent::User };
+ Data = QEvent::User,
+ Load,
+ Remove,
+ Error,
+ Destroy = QEvent::User + 100,
+};
- WorkerDataEvent(int workerId, const QByteArray &data);
- virtual ~WorkerDataEvent();
+class WorkerIdEvent : public QEvent
+{
+public:
+ WorkerIdEvent(int workerId, WorkerEventType type)
+ : QEvent(QEvent::Type(type)), m_workerId(workerId)
+ {}
- int workerId() const;
- QByteArray data() const;
+ int workerId() const { return m_workerId; }
private:
- int m_id;
- QByteArray m_data;
+ int m_workerId = -1;
};
-class WorkerLoadEvent : public QEvent
+class WorkerDataEvent : public WorkerIdEvent
{
public:
- enum Type { WorkerLoad = WorkerDataEvent::WorkerData + 1 };
+ WorkerDataEvent(int workerId, const QByteArray &data)
+ : WorkerIdEvent(workerId, WorkerEventType::Data), m_data(data)
+ {}
- WorkerLoadEvent(int workerId, const QUrl &url);
-
- int workerId() const;
- QUrl url() const;
+ QByteArray data() const { return m_data; }
private:
- int m_id;
- QUrl m_url;
+ QByteArray m_data;
};
-class WorkerRemoveEvent : public QEvent
+class WorkerLoadEvent : public WorkerIdEvent
{
public:
- enum Type { WorkerRemove = WorkerLoadEvent::WorkerLoad + 1 };
-
- WorkerRemoveEvent(int workerId);
+ WorkerLoadEvent(int workerId, const QUrl &url)
+ : WorkerIdEvent(workerId, WorkerEventType::Load), m_url(url)
+ {}
- int workerId() const;
+ QUrl url() const { return m_url; }
private:
- int m_id;
+ QUrl m_url;
};
-class WorkerErrorEvent : public QEvent
+class WorkerRemoveEvent : public WorkerIdEvent
{
public:
- enum Type { WorkerError = WorkerRemoveEvent::WorkerRemove + 1 };
+ WorkerRemoveEvent(int workerId) : WorkerIdEvent(workerId, WorkerEventType::Remove) {}
+};
- WorkerErrorEvent(const QQmlError &error);
+class WorkerErrorEvent : public QEvent
+{
+public:
+ WorkerErrorEvent(const QQmlError &error)
+ : QEvent(QEvent::Type(WorkerEventType::Error)), m_error(error)
+ {}
- QQmlError error() const;
+ QQmlError error() const { return m_error; }
private:
QQmlError m_error;
};
+class WorkerDestroyEvent : public QEvent
+{
+public:
+ WorkerDestroyEvent() : QEvent(QEvent::Type(WorkerEventType::Destroy)) {}
+};
+
struct WorkerScript : public QV4::ExecutionEngine::Deletable
{
WorkerScript(QV4::ExecutionEngine *);
@@ -107,10 +123,6 @@ class QQuickWorkerScriptEnginePrivate : public QObject
{
Q_OBJECT
public:
- enum WorkerEventTypes {
- WorkerDestroyEvent = QEvent::User + 100
- };
-
QQuickWorkerScriptEnginePrivate(QQmlTypeLoader *typeLoader)
: m_typeLoader(typeLoader), m_nextId(0)
{
@@ -163,18 +175,18 @@ QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::method_sendMessage(const QV4
bool QQuickWorkerScriptEnginePrivate::event(QEvent *event)
{
- if (event->type() == (QEvent::Type)WorkerDataEvent::WorkerData) {
+ switch (WorkerEventType(event->type())) {
+ case WorkerEventType::Data: {
WorkerDataEvent *workerEvent = static_cast<WorkerDataEvent *>(event);
processMessage(workerEvent->workerId(), workerEvent->data());
return true;
- } else if (event->type() == (QEvent::Type)WorkerLoadEvent::WorkerLoad) {
+ }
+ case WorkerEventType::Load: {
WorkerLoadEvent *workerEvent = static_cast<WorkerLoadEvent *>(event);
processLoad(workerEvent->workerId(), workerEvent->url());
return true;
- } else if (event->type() == (QEvent::Type)WorkerDestroyEvent) {
- emit stopThread();
- return true;
- } else if (event->type() == (QEvent::Type)WorkerRemoveEvent::WorkerRemove) {
+ }
+ case WorkerEventType::Remove: {
QMutexLocker locker(&m_lock);
WorkerRemoveEvent *workerEvent = static_cast<WorkerRemoveEvent *>(event);
auto itr = workers.constFind(workerEvent->workerId());
@@ -184,9 +196,15 @@ bool QQuickWorkerScriptEnginePrivate::event(QEvent *event)
workers.erase(itr);
}
return true;
- } else {
- return QObject::event(event);
}
+ case WorkerEventType::Destroy:
+ emit stopThread();
+ return true;
+ default:
+ break;
+ }
+
+ return QObject::event(event);
}
QV4::ExecutionEngine *QQuickWorkerScriptEnginePrivate::workerEngine(int id)
@@ -285,60 +303,6 @@ void QQuickWorkerScriptEnginePrivate::reportScriptException(WorkerScript *script
QCoreApplication::postEvent(script->owner, new WorkerErrorEvent(error));
}
-WorkerDataEvent::WorkerDataEvent(int workerId, const QByteArray &data)
-: QEvent((QEvent::Type)WorkerData), m_id(workerId), m_data(data)
-{
-}
-
-WorkerDataEvent::~WorkerDataEvent()
-{
-}
-
-int WorkerDataEvent::workerId() const
-{
- return m_id;
-}
-
-QByteArray WorkerDataEvent::data() const
-{
- return m_data;
-}
-
-WorkerLoadEvent::WorkerLoadEvent(int workerId, const QUrl &url)
-: QEvent((QEvent::Type)WorkerLoad), m_id(workerId), m_url(url)
-{
-}
-
-int WorkerLoadEvent::workerId() const
-{
- return m_id;
-}
-
-QUrl WorkerLoadEvent::url() const
-{
- return m_url;
-}
-
-WorkerRemoveEvent::WorkerRemoveEvent(int workerId)
-: QEvent((QEvent::Type)WorkerRemove), m_id(workerId)
-{
-}
-
-int WorkerRemoveEvent::workerId() const
-{
- return m_id;
-}
-
-WorkerErrorEvent::WorkerErrorEvent(const QQmlError &error)
-: QEvent((QEvent::Type)WorkerError), m_error(error)
-{
-}
-
-QQmlError WorkerErrorEvent::error() const
-{
- return m_error;
-}
-
QQuickWorkerScriptEngine::QQuickWorkerScriptEngine(QQmlEngine *parent)
: QThread(parent)
, d(new QQuickWorkerScriptEnginePrivate(&QQmlEnginePrivate::get(parent)->typeLoader))
@@ -352,7 +316,7 @@ QQuickWorkerScriptEngine::QQuickWorkerScriptEngine(QQmlEngine *parent)
QQuickWorkerScriptEngine::~QQuickWorkerScriptEngine()
{
- QCoreApplication::postEvent(d, new QEvent((QEvent::Type)QQuickWorkerScriptEnginePrivate::WorkerDestroyEvent));
+ QCoreApplication::postEvent(d, new WorkerDestroyEvent);
//We have to force to cleanup the main thread's event queue here
//to make sure the main GUI release all pending locks/wait conditions which
@@ -637,21 +601,25 @@ void QQuickWorkerScript::componentComplete()
bool QQuickWorkerScript::event(QEvent *event)
{
- if (event->type() == (QEvent::Type)WorkerDataEvent::WorkerData) {
+ switch (WorkerEventType(event->type())) {
+ case WorkerEventType::Data:
if (QQmlEngine *engine = qmlEngine(this)) {
QV4::ExecutionEngine *v4 = engine->handle();
WorkerDataEvent *workerEvent = static_cast<WorkerDataEvent *>(event);
emit message(QJSValuePrivate::fromReturnedValue(
- QV4::Serialize::deserialize(workerEvent->data(), v4)));
+ QV4::Serialize::deserialize(workerEvent->data(), v4)));
}
return true;
- } else if (event->type() == (QEvent::Type)WorkerErrorEvent::WorkerError) {
+ case WorkerEventType::Error: {
WorkerErrorEvent *workerEvent = static_cast<WorkerErrorEvent *>(event);
QQmlEnginePrivate::warning(qmlEngine(this), workerEvent->error());
return true;
- } else {
- return QObject::event(event);
}
+ default:
+ break;
+ }
+
+ return QObject::event(event);
}
QT_END_NAMESPACE