5206de1eabcba7355f9217b4fe7a38f9e8d31e57
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qdeclarativethread_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVETHREAD_P_H
43 #define QDECLARATIVETHREAD_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56
57 #include <QtCore/qglobal.h>
58
59 #include <private/qintrusivelist_p.h>
60
61 QT_BEGIN_NAMESPACE
62
63 class QThread;
64
65 class QDeclarativeThreadPrivate;
66 class QDeclarativeThread 
67 {
68 public:
69     QDeclarativeThread();
70     virtual ~QDeclarativeThread();
71     void shutdown();
72
73     void lock();
74     void unlock();
75     void wakeOne();
76     void wakeAll();
77     void wait();
78
79     QThread *thread() const;
80     bool isThisThread() const;
81
82     // Synchronously invoke a method in the thread
83     template<class O>
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 &);
89
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
92     // call.
93     template<class O>
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 &);
99
100     // Asynchronously invoke a method in the thread.
101     template<class O>
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 &);
107
108     // Asynchronously invoke a method in the main thread.
109     template<class O>
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 &);
115
116 protected:
117     virtual void startupThread();
118     virtual void shutdownThread();
119
120 private:
121     friend class QDeclarativeThreadPrivate;
122
123     struct Message {
124         Message() : next(0) {}
125         virtual ~Message() {}
126         Message *next;
127         virtual void call(QDeclarativeThread *) = 0;
128     };
129     void internalCallMethodInThread(Message *);
130     void internalCallMethodInMain(Message *);
131     void internalPostMethodToThread(Message *);
132     void internalPostMethodToMain(Message *);
133     QDeclarativeThreadPrivate *d;
134 };
135
136 template<class O>
137 void QDeclarativeThread::callMethodInThread(void (O::*Member)())
138 {
139     struct I : public Message {
140         void (O::*Member)();
141         I(void (O::*Member)()) : Member(Member) {}
142         virtual void call(QDeclarativeThread *thread) {
143             O *me = static_cast<O *>(thread);
144             (me->*Member)();
145         }
146     };
147     internalCallMethodInThread(new I(Member));
148 }
149
150 template<typename T, class V, class O>
151 void QDeclarativeThread::callMethodInThread(void (O::*Member)(V), const T &arg)
152 {
153     struct I : public Message {
154         void (O::*Member)(V);
155         T arg;
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);
159             (me->*Member)(arg);
160         }
161     };
162     internalCallMethodInThread(new I(Member, arg));
163 }
164
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)
167 {
168     struct I : public Message {
169         void (O::*Member)(V, V2);
170         T arg;
171         T2 arg2;
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);
176         }
177     };
178     internalCallMethodInThread(new I(Member, arg, arg2));
179 }
180
181 template<class O>
182 void QDeclarativeThread::callMethodInMain(void (O::*Member)())
183 {
184     struct I : public Message {
185         void (O::*Member)();
186         I(void (O::*Member)()) : Member(Member) {}
187         virtual void call(QDeclarativeThread *thread) {
188             O *me = static_cast<O *>(thread);
189             (me->*Member)();
190         }
191     };
192     internalCallMethodInMain(new I(Member));
193 }
194
195 template<typename T, class V, class O>
196 void QDeclarativeThread::callMethodInMain(void (O::*Member)(V), const T &arg)
197 {
198     struct I : public Message {
199         void (O::*Member)(V);
200         T arg;
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);
204             (me->*Member)(arg);
205         }
206     };
207     internalCallMethodInMain(new I(Member, arg));
208 }
209
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)
212 {
213     struct I : public Message {
214         void (O::*Member)(V, V2);
215         T arg;
216         T2 arg2;
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);
221         }
222     };
223     internalCallMethodInMain(new I(Member, arg, arg2));
224 }
225
226 template<class O>
227 void QDeclarativeThread::postMethodToThread(void (O::*Member)())
228 {
229     struct I : public Message {
230         void (O::*Member)();
231         I(void (O::*Member)()) : Member(Member) {}
232         virtual void call(QDeclarativeThread *thread) {
233             O *me = static_cast<O *>(thread);
234             (me->*Member)();
235         }
236     };
237     internalPostMethodToThread(new I(Member));
238 }
239
240 template<typename T, class V, class O>
241 void QDeclarativeThread::postMethodToThread(void (O::*Member)(V), const T &arg)
242 {
243     struct I : public Message {
244         void (O::*Member)(V);
245         T arg;
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);
249             (me->*Member)(arg);
250         }
251     };
252     internalPostMethodToThread(new I(Member, arg));
253 }
254
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)
257 {
258     struct I : public Message {
259         void (O::*Member)(V, V2);
260         T arg;
261         T2 arg2;
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);
266         }
267     };
268     internalPostMethodToThread(new I(Member, arg, arg2));
269 }
270
271 template<class O>
272 void QDeclarativeThread::postMethodToMain(void (O::*Member)())
273 {
274     struct I : public Message {
275         void (O::*Member)();
276         I(void (O::*Member)()) : Member(Member) {}
277         virtual void call(QDeclarativeThread *thread) {
278             O *me = static_cast<O *>(thread);
279             (me->*Member)();
280         }
281     };
282     internalPostMethodToMain(new I(Member));
283 }
284
285 template<typename T, class V, class O>
286 void QDeclarativeThread::postMethodToMain(void (O::*Member)(V), const T &arg)
287 {
288     struct I : public Message {
289         void (O::*Member)(V);
290         T arg;
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);
294             (me->*Member)(arg);
295         }
296     };
297     internalPostMethodToMain(new I(Member, arg));
298 }
299
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)
302 {
303     struct I : public Message {
304         void (O::*Member)(V, V2);
305         T arg;
306         T2 arg2;
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);
311         }
312     };
313     internalPostMethodToMain(new I(Member, arg, arg2));
314 }
315
316 QT_END_NAMESPACE
317
318 #endif // QDECLARATIVETHREAD_P_H