1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVETHREAD_P_H
43 #define QDECLARATIVETHREAD_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
57 #include <QtCore/qglobal.h>
59 #include <private/qintrusivelist_p.h>
65 class QDeclarativeThreadPrivate;
66 class QDeclarativeThread
70 virtual ~QDeclarativeThread();
79 QThread *thread() const;
80 bool isThisThread() const;
82 // Synchronously invoke a method in the thread
84 inline void callMethodInThread(void (O::*Member)());
85 template<typename T, class V, class O>
86 inline void callMethodInThread(void (O::*Member)(V), const T &);
87 template<typename T, typename T2, class V, class V2, class O>
88 inline void callMethodInThread(void (O::*Member)(V, V2), const T &, const T2 &);
90 // Synchronously invoke a method in the main thread. If the main thread is
91 // blocked in a callMethodInThread() call, the call is made from within that
94 inline void callMethodInMain(void (O::*Member)());
95 template<typename T, class V, class O>
96 inline void callMethodInMain(void (O::*Member)(V), const T &);
97 template<typename T, typename T2, class V, class V2, class O>
98 inline void callMethodInMain(void (O::*Member)(V, V2), const T &, const T2 &);
100 // Asynchronously invoke a method in the thread.
102 inline void postMethodToThread(void (O::*Member)());
103 template<typename T, class V, class O>
104 inline void postMethodToThread(void (O::*Member)(V), const T &);
105 template<typename T, typename T2, class V, class V2, class O>
106 inline void postMethodToThread(void (O::*Member)(V, V2), const T &, const T2 &);
108 // Asynchronously invoke a method in the main thread.
110 inline void postMethodToMain(void (O::*Member)());
111 template<typename T, class V, class O>
112 inline void postMethodToMain(void (O::*Member)(V), const T &);
113 template<typename T, typename T2, class V, class V2, class O>
114 inline void postMethodToMain(void (O::*Member)(V, V2), const T &, const T2 &);
117 virtual void startupThread();
118 virtual void shutdownThread();
121 friend class QDeclarativeThreadPrivate;
124 Message() : next(0) {}
125 virtual ~Message() {}
127 virtual void call(QDeclarativeThread *) = 0;
129 void internalCallMethodInThread(Message *);
130 void internalCallMethodInMain(Message *);
131 void internalPostMethodToThread(Message *);
132 void internalPostMethodToMain(Message *);
133 QDeclarativeThreadPrivate *d;
137 void QDeclarativeThread::callMethodInThread(void (O::*Member)())
139 struct I : public Message {
141 I(void (O::*Member)()) : Member(Member) {}
142 virtual void call(QDeclarativeThread *thread) {
143 O *me = static_cast<O *>(thread);
147 internalCallMethodInThread(new I(Member));
150 template<typename T, class V, class O>
151 void QDeclarativeThread::callMethodInThread(void (O::*Member)(V), const T &arg)
153 struct I : public Message {
154 void (O::*Member)(V);
156 I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {}
157 virtual void call(QDeclarativeThread *thread) {
158 O *me = static_cast<O *>(thread);
162 internalCallMethodInThread(new I(Member, arg));
165 template<typename T, typename T2, class V, class V2, class O>
166 void QDeclarativeThread::callMethodInThread(void (O::*Member)(V, V2), const T &arg, const T2 &arg2)
168 struct I : public Message {
169 void (O::*Member)(V, V2);
172 I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {}
173 virtual void call(QDeclarativeThread *thread) {
174 O *me = static_cast<O *>(thread);
175 (me->*Member)(arg, arg2);
178 internalCallMethodInThread(new I(Member, arg, arg2));
182 void QDeclarativeThread::callMethodInMain(void (O::*Member)())
184 struct I : public Message {
186 I(void (O::*Member)()) : Member(Member) {}
187 virtual void call(QDeclarativeThread *thread) {
188 O *me = static_cast<O *>(thread);
192 internalCallMethodInMain(new I(Member));
195 template<typename T, class V, class O>
196 void QDeclarativeThread::callMethodInMain(void (O::*Member)(V), const T &arg)
198 struct I : public Message {
199 void (O::*Member)(V);
201 I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {}
202 virtual void call(QDeclarativeThread *thread) {
203 O *me = static_cast<O *>(thread);
207 internalCallMethodInMain(new I(Member, arg));
210 template<typename T, typename T2, class V, class V2, class O>
211 void QDeclarativeThread::callMethodInMain(void (O::*Member)(V, V2), const T &arg, const T2 &arg2)
213 struct I : public Message {
214 void (O::*Member)(V, V2);
217 I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {}
218 virtual void call(QDeclarativeThread *thread) {
219 O *me = static_cast<O *>(thread);
220 (me->*Member)(arg, arg2);
223 internalCallMethodInMain(new I(Member, arg, arg2));
227 void QDeclarativeThread::postMethodToThread(void (O::*Member)())
229 struct I : public Message {
231 I(void (O::*Member)()) : Member(Member) {}
232 virtual void call(QDeclarativeThread *thread) {
233 O *me = static_cast<O *>(thread);
237 internalPostMethodToThread(new I(Member));
240 template<typename T, class V, class O>
241 void QDeclarativeThread::postMethodToThread(void (O::*Member)(V), const T &arg)
243 struct I : public Message {
244 void (O::*Member)(V);
246 I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {}
247 virtual void call(QDeclarativeThread *thread) {
248 O *me = static_cast<O *>(thread);
252 internalPostMethodToThread(new I(Member, arg));
255 template<typename T, typename T2, class V, class V2, class O>
256 void QDeclarativeThread::postMethodToThread(void (O::*Member)(V, V2), const T &arg, const T2 &arg2)
258 struct I : public Message {
259 void (O::*Member)(V, V2);
262 I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {}
263 virtual void call(QDeclarativeThread *thread) {
264 O *me = static_cast<O *>(thread);
265 (me->*Member)(arg, arg2);
268 internalPostMethodToThread(new I(Member, arg, arg2));
272 void QDeclarativeThread::postMethodToMain(void (O::*Member)())
274 struct I : public Message {
276 I(void (O::*Member)()) : Member(Member) {}
277 virtual void call(QDeclarativeThread *thread) {
278 O *me = static_cast<O *>(thread);
282 internalPostMethodToMain(new I(Member));
285 template<typename T, class V, class O>
286 void QDeclarativeThread::postMethodToMain(void (O::*Member)(V), const T &arg)
288 struct I : public Message {
289 void (O::*Member)(V);
291 I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {}
292 virtual void call(QDeclarativeThread *thread) {
293 O *me = static_cast<O *>(thread);
297 internalPostMethodToMain(new I(Member, arg));
300 template<typename T, typename T2, class V, class V2, class O>
301 void QDeclarativeThread::postMethodToMain(void (O::*Member)(V, V2), const T &arg, const T2 &arg2)
303 struct I : public Message {
304 void (O::*Member)(V, V2);
307 I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {}
308 virtual void call(QDeclarativeThread *thread) {
309 O *me = static_cast<O *>(thread);
310 (me->*Member)(arg, arg2);
313 internalPostMethodToMain(new I(Member, arg, arg2));
318 #endif // QDECLARATIVETHREAD_P_H