OSDN Git Service

0a31037c6f063dc8bda34bc1e04a90fa4eee5bf6
[qt-creator-jp/qt-creator-jp.git] / src / libs / qtconcurrent / runextensions.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef QTCONCURRENT_RUNEX_H
35 #define QTCONCURRENT_RUNEX_H
36
37 #include <qrunnable.h>
38 #include <qfutureinterface.h>
39 #include <qthreadpool.h>
40
41 QT_BEGIN_NAMESPACE
42
43 namespace QtConcurrent {
44
45 template <typename T,  typename FunctionPointer>
46 class StoredInterfaceFunctionCall0 : public QRunnable
47 {
48 public:
49     StoredInterfaceFunctionCall0(void (fn)(QFutureInterface<T> &))
50     : fn(fn) { }
51
52     QFuture<T> start()
53     {
54         futureInterface.reportStarted();
55         QFuture<T> future = futureInterface.future();
56         QThreadPool::globalInstance()->start(this);
57         return future;
58     }
59
60     void run()
61     {
62         fn(futureInterface);
63         futureInterface.reportFinished();
64     }
65 private:
66     QFutureInterface<T> futureInterface;
67     FunctionPointer fn;
68
69 };
70 template <typename T,  typename FunctionPointer, typename Class>
71 class StoredInterfaceMemberFunctionCall0 : public QRunnable
72 {
73 public:
74     StoredInterfaceMemberFunctionCall0(void (Class::*fn)(QFutureInterface<T> &), Class *object)
75     : fn(fn), object(object) { }
76
77     QFuture<T> start()
78     {
79         futureInterface.reportStarted();
80         QFuture<T> future = futureInterface.future();
81         QThreadPool::globalInstance()->start(this);
82         return future;
83     }
84
85     void run()
86     {
87         (object->*fn)(futureInterface);
88         futureInterface.reportFinished();
89     }
90 private:
91     QFutureInterface<T> futureInterface;
92     FunctionPointer fn;
93     Class *object;
94
95 };
96
97 template <typename T,  typename FunctionPointer, typename Arg1>
98 class StoredInterfaceFunctionCall1 : public QRunnable
99 {
100 public:
101     StoredInterfaceFunctionCall1(void (fn)(QFutureInterface<T> &, Arg1), Arg1 arg1)
102     : fn(fn), arg1(arg1) { }
103
104     QFuture<T> start()
105     {
106         futureInterface.reportStarted();
107         QFuture<T> future = futureInterface.future();
108         QThreadPool::globalInstance()->start(this);
109         return future;
110     }
111
112     void run()
113     {
114         fn(futureInterface, arg1);
115         futureInterface.reportFinished();
116     }
117 private:
118     QFutureInterface<T> futureInterface;
119     FunctionPointer fn;
120     Arg1 arg1;
121 };
122 template <typename T,  typename FunctionPointer, typename Class, typename Arg1>
123 class StoredInterfaceMemberFunctionCall1 : public QRunnable
124 {
125 public:
126     StoredInterfaceMemberFunctionCall1(void (Class::*fn)(QFutureInterface<T> &, Arg1), Class *object, Arg1 arg1)
127     : fn(fn), object(object), arg1(arg1) { }
128
129     QFuture<T> start()
130     {
131         futureInterface.reportStarted();
132         QFuture<T> future = futureInterface.future();
133         QThreadPool::globalInstance()->start(this);
134         return future;
135     }
136
137     void run()
138     {
139         (object->*fn)(futureInterface, arg1);
140         futureInterface.reportFinished();
141     }
142 private:
143     QFutureInterface<T> futureInterface;
144     FunctionPointer fn;
145     Class *object;
146     Arg1 arg1;
147 };
148
149 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2>
150 class StoredInterfaceFunctionCall2 : public QRunnable
151 {
152 public:
153     StoredInterfaceFunctionCall2(void (fn)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
154     : fn(fn), arg1(arg1), arg2(arg2) { }
155
156     QFuture<T> start()
157     {
158         futureInterface.reportStarted();
159         QFuture<T> future = futureInterface.future();
160         QThreadPool::globalInstance()->start(this);
161         return future;
162     }
163
164     void run()
165     {
166         fn(futureInterface, arg1, arg2);
167         futureInterface.reportFinished();
168     }
169 private:
170     QFutureInterface<T> futureInterface;
171     FunctionPointer fn;
172     Arg1 arg1; Arg2 arg2;
173 };
174 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2>
175 class StoredInterfaceMemberFunctionCall2 : public QRunnable
176 {
177 public:
178     StoredInterfaceMemberFunctionCall2(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2), Class *object, Arg1 arg1, Arg2 arg2)
179     : fn(fn), object(object), arg1(arg1), arg2(arg2) { }
180
181     QFuture<T> start()
182     {
183         futureInterface.reportStarted();
184         QFuture<T> future = futureInterface.future();
185         QThreadPool::globalInstance()->start(this);
186         return future;
187     }
188
189     void run()
190     {
191         (object->*fn)(futureInterface, arg1, arg2);
192         futureInterface.reportFinished();
193     }
194 private:
195     QFutureInterface<T> futureInterface;
196     FunctionPointer fn;
197     Class *object;
198     Arg1 arg1; Arg2 arg2;
199 };
200
201 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
202 class StoredInterfaceFunctionCall3 : public QRunnable
203 {
204 public:
205     StoredInterfaceFunctionCall3(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3)
206     : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3) { }
207
208     QFuture<T> start()
209     {
210         futureInterface.reportStarted();
211         QFuture<T> future = futureInterface.future();
212         QThreadPool::globalInstance()->start(this);
213         return future;
214     }
215
216     void run()
217     {
218         fn(futureInterface, arg1, arg2, arg3);
219         futureInterface.reportFinished();
220     }
221 private:
222     QFutureInterface<T> futureInterface;
223     FunctionPointer fn;
224     Arg1 arg1; Arg2 arg2; Arg3 arg3;
225 };
226 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3>
227 class StoredInterfaceMemberFunctionCall3 : public QRunnable
228 {
229 public:
230     StoredInterfaceMemberFunctionCall3(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3)
231     : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3) { }
232
233     QFuture<T> start()
234     {
235         futureInterface.reportStarted();
236         QFuture<T> future = futureInterface.future();
237         QThreadPool::globalInstance()->start(this);
238         return future;
239     }
240
241     void run()
242     {
243         (object->*fn)(futureInterface, arg1, arg2, arg3);
244         futureInterface.reportFinished();
245     }
246 private:
247     QFutureInterface<T> futureInterface;
248     FunctionPointer fn;
249     Class *object;
250     Arg1 arg1; Arg2 arg2; Arg3 arg3;
251 };
252
253 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
254 class StoredInterfaceFunctionCall4 : public QRunnable
255 {
256 public:
257     StoredInterfaceFunctionCall4(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
258     : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) { }
259
260     QFuture<T> start()
261     {
262         futureInterface.reportStarted();
263         QFuture<T> future = futureInterface.future();
264         QThreadPool::globalInstance()->start(this);
265         return future;
266     }
267
268     void run()
269     {
270         fn(futureInterface, arg1, arg2, arg3, arg4);
271         futureInterface.reportFinished();
272     }
273 private:
274     QFutureInterface<T> futureInterface;
275     FunctionPointer fn;
276     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
277 };
278 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
279 class StoredInterfaceMemberFunctionCall4 : public QRunnable
280 {
281 public:
282     StoredInterfaceMemberFunctionCall4(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
283     : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) { }
284
285     QFuture<T> start()
286     {
287         futureInterface.reportStarted();
288         QFuture<T> future = futureInterface.future();
289         QThreadPool::globalInstance()->start(this);
290         return future;
291     }
292
293     void run()
294     {
295         (object->*fn)(futureInterface, arg1, arg2, arg3, arg4);
296         futureInterface.reportFinished();
297     }
298 private:
299     QFutureInterface<T> futureInterface;
300     FunctionPointer fn;
301     Class *object;
302     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
303 };
304
305 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
306 class StoredInterfaceFunctionCall5 : public QRunnable
307 {
308 public:
309     StoredInterfaceFunctionCall5(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
310     : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) { }
311
312     QFuture<T> start()
313     {
314         futureInterface.reportStarted();
315         QFuture<T> future = futureInterface.future();
316         QThreadPool::globalInstance()->start(this);
317         return future;
318     }
319
320     void run()
321     {
322         fn(futureInterface, arg1, arg2, arg3, arg4, arg5);
323         futureInterface.reportFinished();
324     }
325 private:
326     QFutureInterface<T> futureInterface;
327     FunctionPointer fn;
328     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
329 };
330 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
331 class StoredInterfaceMemberFunctionCall5 : public QRunnable
332 {
333 public:
334     StoredInterfaceMemberFunctionCall5(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
335     : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) { }
336
337     QFuture<T> start()
338     {
339         futureInterface.reportStarted();
340         QFuture<T> future = futureInterface.future();
341         QThreadPool::globalInstance()->start(this);
342         return future;
343     }
344
345     void run()
346     {
347         (object->*fn)(futureInterface, arg1, arg2, arg3, arg4, arg5);
348         futureInterface.reportFinished();
349     }
350 private:
351     QFutureInterface<T> futureInterface;
352     FunctionPointer fn;
353     Class *object;
354     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
355 };
356
357 template <typename T>
358 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &))
359 {
360     return (new StoredInterfaceFunctionCall0<T, void (*)(QFutureInterface<T> &)>(functionPointer))->start();
361 }
362 template <typename T, typename Arg1>
363 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1), Arg1 arg1)
364 {
365     return (new StoredInterfaceFunctionCall1<T, void (*)(QFutureInterface<T> &, Arg1), Arg1>(functionPointer, arg1))->start();
366 }
367 template <typename T, typename Arg1, typename Arg2>
368 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
369 {
370     return (new StoredInterfaceFunctionCall2<T, void (*)(QFutureInterface<T> &, Arg1, Arg2), Arg1, Arg2>(functionPointer, arg1, arg2))->start();
371 }
372 template <typename T, typename Arg1, typename Arg2, typename Arg3>
373 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3)
374 {
375     return (new StoredInterfaceFunctionCall3<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(functionPointer, arg1, arg2, arg3))->start();
376 }
377 template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
378 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
379 {
380     return (new StoredInterfaceFunctionCall4<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1, Arg2, Arg3, Arg4>(functionPointer, arg1, arg2, arg3, arg4))->start();
381 }
382 template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
383 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
384 {
385     return (new StoredInterfaceFunctionCall5<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1, Arg2, Arg3, Arg4, Arg5>(functionPointer, arg1, arg2, arg3, arg4, arg5))->start();
386 }
387
388 template <typename Class, typename T>
389 QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), Class *object)
390 {
391     return (new StoredInterfaceMemberFunctionCall0<T, void (Class::*)(QFutureInterface<T> &), Class>(fn, object))->start();
392 }
393
394 } // namespace QtConcurrent
395
396 QT_END_NAMESPACE
397
398 #endif // QTCONCURRENT_RUNEX_H