Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlthread_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLTHREAD_P_H
5#define QQMLTHREAD_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18
19#include <QtCore/qglobal.h>
20
21#include <private/qintrusivelist_p.h>
22
23QT_BEGIN_NAMESPACE
24
25class QThread;
26class QMutex;
27
30{
31public:
32 QQmlThread();
33 virtual ~QQmlThread();
34
35 void lock();
36 void unlock();
37 void wakeOne();
38 void wait();
39
40 bool isThisThread() const;
41 bool isParentThread() const;
42
43 // Synchronously invoke a method in the thread
44 template<typename Method, typename ...Args>
45 void callMethodInThread(Method &&method, Args &&...args);
46
47 // Synchronously invoke a method in the main thread. If the main thread is
48 // blocked in a callMethodInThread() call, the call is made from within that
49 // call.
50 template<typename Method, typename ...Args>
51 void callMethodInMain(Method &&method, Args &&...args);
52
53 // Asynchronously invoke a method in the thread.
54 template<typename Method, typename ...Args>
55 void postMethodToThread(Method &&method, Args &&...args);
56
57 // Asynchronously invoke a method in the main thread.
58 template<typename Method, typename ...Args>
59 void postMethodToMain(Method &&method, Args &&...args);
60
61 void waitForNextMessage();
62 void discardMessages();
63
64 void startup();
65 void shutdown();
66
67protected:
68 QThread *thread() const;
69 QObject *threadObject() const;
70
71private:
72 friend class QQmlThreadPrivate;
73
74 struct Message {
75 Message() : next(nullptr) {}
76 virtual ~Message() {}
77 Message *next;
78 virtual void call(QQmlThread *) = 0;
79 };
80 template<typename Method, typename ...Args>
81 Message *createMessageFromMethod(Method &&method, Args &&...args);
82 void internalCallMethodInThread(Message *);
83 void internalCallMethodInMain(Message *);
84 void internalPostMethodToThread(Message *);
85 void internalPostMethodToMain(Message *);
87};
88
89namespace QtPrivate {
90template <typename> struct member_function_traits;
91
92template <typename Return, typename Object, typename... Args>
93struct member_function_traits<Return (Object::*)(Args...)>
94{
95 using class_type = Object;
96};
97}
98
99template<typename Method, typename ...Args>
100QQmlThread::Message *QQmlThread::createMessageFromMethod(Method &&method, Args &&...args)
101{
102 struct I : public Message {
103 Method m;
104 std::tuple<std::decay_t<Args>...> arguments;
105 I(Method &&method, Args&& ...args) : m(std::forward<Method>(method)), arguments(std::forward<Args>(args)...) {}
106 void call(QQmlThread *thread) override {
107 using class_type = typename QtPrivate::member_function_traits<Method>::class_type;
108 class_type *me = static_cast<class_type *>(thread);
109 std::apply(m, std::tuple_cat(std::make_tuple(me), arguments));
110 }
111 };
112 return new I(std::forward<Method>(method), std::forward<Args>(args)...);
113}
114
115template<typename Method, typename ...Args>
116void QQmlThread::callMethodInMain(Method &&method, Args&& ...args)
117{
118 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
119 internalCallMethodInMain(m);
120}
121
122template<typename Method, typename ...Args>
123void QQmlThread::callMethodInThread(Method &&method, Args&& ...args)
124{
125 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
126 internalCallMethodInThread(m);
127}
128
129template<typename Method, typename ...Args>
130void QQmlThread::postMethodToThread(Method &&method, Args&& ...args)
131{
132 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
133 internalPostMethodToThread(m);
134}
135
136template<typename Method, typename ...Args>
137void QQmlThread::postMethodToMain(Method &&method, Args&& ...args)
138{
139 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
140 internalPostMethodToMain(m);
141}
142
143QT_END_NAMESPACE
144
145#endif // QQMLTHREAD_P_H
QQmlThreadPrivate(QQmlThread *q)
QFieldList< QQmlThread::Message, &QQmlThread::Message::next > MessageList
bool event(QEvent *e) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void postMethodToThread(Method &&method, Args &&...args)
QThread * thread() const
bool isThisThread() const
void waitForNextMessage()
void postMethodToMain(Method &&method, Args &&...args)
bool isParentThread() const
void callMethodInMain(Method &&method, Args &&...args)
virtual ~QQmlThread()
void callMethodInThread(Method &&method, Args &&...args)
QObject * threadObject() const
And object living in the QML thread, in case you want to parent other objects to it.
QT_REQUIRE_CONFIG(thread)