2 ******************************************************************************
4 * @file runextensions.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef QTCONCURRENT_RUNEX_H
30 #define QTCONCURRENT_RUNEX_H
32 #include <qrunnable.h>
33 #include <qfutureinterface.h>
34 #include <qthreadpool.h>
38 namespace QtConcurrent
{
39 template <typename T
, typename FunctionPointer
>
40 class StoredInterfaceFunctionCall0
: public QRunnable
{
42 StoredInterfaceFunctionCall0(void(fn
)(QFutureInterface
<T
> &))
47 futureInterface
.reportStarted();
48 QFuture
<T
> future
= futureInterface
.future();
49 QThreadPool::globalInstance()->start(this);
56 futureInterface
.reportFinished();
59 QFutureInterface
<T
> futureInterface
;
62 template <typename T
, typename FunctionPointer
, typename Class
>
63 class StoredInterfaceMemberFunctionCall0
: public QRunnable
{
65 StoredInterfaceMemberFunctionCall0(void(Class::*fn
)(QFutureInterface
<T
> &), Class
*object
)
66 : fn(fn
), object(object
) {}
70 futureInterface
.reportStarted();
71 QFuture
<T
> future
= futureInterface
.future();
72 QThreadPool::globalInstance()->start(this);
78 (object
->*fn
)(futureInterface
);
79 futureInterface
.reportFinished();
82 QFutureInterface
<T
> futureInterface
;
87 template <typename T
, typename FunctionPointer
, typename Arg1
>
88 class StoredInterfaceFunctionCall1
: public QRunnable
{
90 StoredInterfaceFunctionCall1(void(fn
)(QFutureInterface
<T
> &, Arg1
), Arg1 arg1
)
91 : fn(fn
), arg1(arg1
) {}
95 futureInterface
.reportStarted();
96 QFuture
<T
> future
= futureInterface
.future();
97 QThreadPool::globalInstance()->start(this);
103 fn(futureInterface
, arg1
);
104 futureInterface
.reportFinished();
107 QFutureInterface
<T
> futureInterface
;
111 template <typename T
, typename FunctionPointer
, typename Class
, typename Arg1
>
112 class StoredInterfaceMemberFunctionCall1
: public QRunnable
{
114 StoredInterfaceMemberFunctionCall1(void(Class::*fn
)(QFutureInterface
<T
> &, Arg1
), Class
*object
, Arg1 arg1
)
115 : fn(fn
), object(object
), arg1(arg1
) {}
119 futureInterface
.reportStarted();
120 QFuture
<T
> future
= futureInterface
.future();
121 QThreadPool::globalInstance()->start(this);
127 (object
->*fn
)(futureInterface
, arg1
);
128 futureInterface
.reportFinished();
131 QFutureInterface
<T
> futureInterface
;
137 template <typename T
, typename FunctionPointer
, typename Arg1
, typename Arg2
>
138 class StoredInterfaceFunctionCall2
: public QRunnable
{
140 StoredInterfaceFunctionCall2(void(fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
), Arg1 arg1
, Arg2 arg2
)
141 : fn(fn
), arg1(arg1
), arg2(arg2
) {}
145 futureInterface
.reportStarted();
146 QFuture
<T
> future
= futureInterface
.future();
147 QThreadPool::globalInstance()->start(this);
153 fn(futureInterface
, arg1
, arg2
);
154 futureInterface
.reportFinished();
157 QFutureInterface
<T
> futureInterface
;
159 Arg1 arg1
; Arg2 arg2
;
161 template <typename T
, typename FunctionPointer
, typename Class
, typename Arg1
, typename Arg2
>
162 class StoredInterfaceMemberFunctionCall2
: public QRunnable
{
164 StoredInterfaceMemberFunctionCall2(void(Class::*fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
), Class
*object
, Arg1 arg1
, Arg2 arg2
)
165 : fn(fn
), object(object
), arg1(arg1
), arg2(arg2
) {}
169 futureInterface
.reportStarted();
170 QFuture
<T
> future
= futureInterface
.future();
171 QThreadPool::globalInstance()->start(this);
177 (object
->*fn
)(futureInterface
, arg1
, arg2
);
178 futureInterface
.reportFinished();
181 QFutureInterface
<T
> futureInterface
;
184 Arg1 arg1
; Arg2 arg2
;
187 template <typename T
, typename FunctionPointer
, typename Arg1
, typename Arg2
, typename Arg3
>
188 class StoredInterfaceFunctionCall3
: public QRunnable
{
190 StoredInterfaceFunctionCall3(void(fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
)
191 : fn(fn
), arg1(arg1
), arg2(arg2
), arg3(arg3
) {}
195 futureInterface
.reportStarted();
196 QFuture
<T
> future
= futureInterface
.future();
197 QThreadPool::globalInstance()->start(this);
203 fn(futureInterface
, arg1
, arg2
, arg3
);
204 futureInterface
.reportFinished();
207 QFutureInterface
<T
> futureInterface
;
209 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
;
211 template <typename T
, typename FunctionPointer
, typename Class
, typename Arg1
, typename Arg2
, typename Arg3
>
212 class StoredInterfaceMemberFunctionCall3
: public QRunnable
{
214 StoredInterfaceMemberFunctionCall3(void(Class::*fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
), Class
*object
, Arg1 arg1
, Arg2 arg2
, Arg3 arg3
)
215 : fn(fn
), object(object
), arg1(arg1
), arg2(arg2
), arg3(arg3
) {}
219 futureInterface
.reportStarted();
220 QFuture
<T
> future
= futureInterface
.future();
221 QThreadPool::globalInstance()->start(this);
227 (object
->*fn
)(futureInterface
, arg1
, arg2
, arg3
);
228 futureInterface
.reportFinished();
231 QFutureInterface
<T
> futureInterface
;
234 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
;
237 template <typename T
, typename FunctionPointer
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
>
238 class StoredInterfaceFunctionCall4
: public QRunnable
{
240 StoredInterfaceFunctionCall4(void(fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
)
241 : fn(fn
), arg1(arg1
), arg2(arg2
), arg3(arg3
), arg4(arg4
) {}
245 futureInterface
.reportStarted();
246 QFuture
<T
> future
= futureInterface
.future();
247 QThreadPool::globalInstance()->start(this);
253 fn(futureInterface
, arg1
, arg2
, arg3
, arg4
);
254 futureInterface
.reportFinished();
257 QFutureInterface
<T
> futureInterface
;
259 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
; Arg4 arg4
;
261 template <typename T
, typename FunctionPointer
, typename Class
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
>
262 class StoredInterfaceMemberFunctionCall4
: public QRunnable
{
264 StoredInterfaceMemberFunctionCall4(void(Class::*fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
), Class
*object
, Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
)
265 : fn(fn
), object(object
), arg1(arg1
), arg2(arg2
), arg3(arg3
), arg4(arg4
) {}
269 futureInterface
.reportStarted();
270 QFuture
<T
> future
= futureInterface
.future();
271 QThreadPool::globalInstance()->start(this);
277 (object
->*fn
)(futureInterface
, arg1
, arg2
, arg3
, arg4
);
278 futureInterface
.reportFinished();
281 QFutureInterface
<T
> futureInterface
;
284 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
; Arg4 arg4
;
287 template <typename T
, typename FunctionPointer
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
, typename Arg5
>
288 class StoredInterfaceFunctionCall5
: public QRunnable
{
290 StoredInterfaceFunctionCall5(void(fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
, Arg5
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
, Arg5 arg5
)
291 : fn(fn
), arg1(arg1
), arg2(arg2
), arg3(arg3
), arg4(arg4
), arg5(arg5
) {}
295 futureInterface
.reportStarted();
296 QFuture
<T
> future
= futureInterface
.future();
297 QThreadPool::globalInstance()->start(this);
303 fn(futureInterface
, arg1
, arg2
, arg3
, arg4
, arg5
);
304 futureInterface
.reportFinished();
307 QFutureInterface
<T
> futureInterface
;
309 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
; Arg4 arg4
; Arg5 arg5
;
311 template <typename T
, typename FunctionPointer
, typename Class
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
, typename Arg5
>
312 class StoredInterfaceMemberFunctionCall5
: public QRunnable
{
314 StoredInterfaceMemberFunctionCall5(void(Class::*fn
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
, Arg5
), Class
*object
, Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
, Arg5 arg5
)
315 : fn(fn
), object(object
), arg1(arg1
), arg2(arg2
), arg3(arg3
), arg4(arg4
), arg5(arg5
) {}
319 futureInterface
.reportStarted();
320 QFuture
<T
> future
= futureInterface
.future();
321 QThreadPool::globalInstance()->start(this);
327 (object
->*fn
)(futureInterface
, arg1
, arg2
, arg3
, arg4
, arg5
);
328 futureInterface
.reportFinished();
331 QFutureInterface
<T
> futureInterface
;
334 Arg1 arg1
; Arg2 arg2
; Arg3 arg3
; Arg4 arg4
; Arg5 arg5
;
337 template <typename T
>
338 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &))
340 return (new StoredInterfaceFunctionCall0
<T
, void (*)(QFutureInterface
<T
> &)>(functionPointer
))->start();
342 template <typename T
, typename Arg1
>
343 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &, Arg1
), Arg1 arg1
)
345 return (new StoredInterfaceFunctionCall1
<T
, void (*)(QFutureInterface
<T
> &, Arg1
), Arg1
>(functionPointer
, arg1
))->start();
347 template <typename T
, typename Arg1
, typename Arg2
>
348 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &, Arg1
, Arg2
), Arg1 arg1
, Arg2 arg2
)
350 return (new StoredInterfaceFunctionCall2
<T
, void (*)(QFutureInterface
<T
> &, Arg1
, Arg2
), Arg1
, Arg2
>(functionPointer
, arg1
, arg2
))->start();
352 template <typename T
, typename Arg1
, typename Arg2
, typename Arg3
>
353 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
)
355 return (new StoredInterfaceFunctionCall3
<T
, void (*)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
), Arg1
, Arg2
, Arg3
>(functionPointer
, arg1
, arg2
, arg3
))->start();
357 template <typename T
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
>
358 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
)
360 return (new StoredInterfaceFunctionCall4
<T
, void (*)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
), Arg1
, Arg2
, Arg3
, Arg4
>(functionPointer
, arg1
, arg2
, arg3
, arg4
))->start();
362 template <typename T
, typename Arg1
, typename Arg2
, typename Arg3
, typename Arg4
, typename Arg5
>
363 QFuture
<T
> run(void (*functionPointer
)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
, Arg5
), Arg1 arg1
, Arg2 arg2
, Arg3 arg3
, Arg4 arg4
, Arg5 arg5
)
365 return (new StoredInterfaceFunctionCall5
<T
, void (*)(QFutureInterface
<T
> &, Arg1
, Arg2
, Arg3
, Arg4
, Arg5
), Arg1
, Arg2
, Arg3
, Arg4
, Arg5
>(functionPointer
, arg1
, arg2
, arg3
, arg4
, arg5
))->start();
368 template <typename Class
, typename T
>
369 QFuture
<T
> run(void (Class::*fn
)(QFutureInterface
<T
> &), Class
*object
)
371 return (new StoredInterfaceMemberFunctionCall0
<T
, void(Class::*) (QFutureInterface
<T
> &), Class
>(fn
, object
))->start();
373 } // namespace QtConcurrent
377 #endif // QTCONCURRENT_RUNEX_H