1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #ifndef QTCONCURRENT_RUNEX_H
34 #define QTCONCURRENT_RUNEX_H
36 #include <qrunnable.h>
37 #include <qfutureinterface.h>
38 #include <qthreadpool.h>
42 namespace QtConcurrent {
44 template <typename T, typename FunctionPointer>
45 class StoredInterfaceFunctionCall0 : public QRunnable
48 StoredInterfaceFunctionCall0(void (fn)(QFutureInterface<T> &))
53 futureInterface.reportStarted();
54 QFuture<T> future = futureInterface.future();
55 QThreadPool::globalInstance()->start(this);
62 futureInterface.reportFinished();
65 QFutureInterface<T> futureInterface;
69 template <typename T, typename FunctionPointer, typename Class>
70 class StoredInterfaceMemberFunctionCall0 : public QRunnable
73 StoredInterfaceMemberFunctionCall0(void (Class::*fn)(QFutureInterface<T> &), Class *object)
74 : fn(fn), object(object) { }
78 futureInterface.reportStarted();
79 QFuture<T> future = futureInterface.future();
80 QThreadPool::globalInstance()->start(this);
86 (object->*fn)(futureInterface);
87 futureInterface.reportFinished();
90 QFutureInterface<T> futureInterface;
96 template <typename T, typename FunctionPointer, typename Arg1>
97 class StoredInterfaceFunctionCall1 : public QRunnable
100 StoredInterfaceFunctionCall1(void (fn)(QFutureInterface<T> &, Arg1), Arg1 arg1)
101 : fn(fn), arg1(arg1) { }
105 futureInterface.reportStarted();
106 QFuture<T> future = futureInterface.future();
107 QThreadPool::globalInstance()->start(this);
113 fn(futureInterface, arg1);
114 futureInterface.reportFinished();
117 QFutureInterface<T> futureInterface;
121 template <typename T, typename FunctionPointer, typename Class, typename Arg1>
122 class StoredInterfaceMemberFunctionCall1 : public QRunnable
125 StoredInterfaceMemberFunctionCall1(void (Class::*fn)(QFutureInterface<T> &, Arg1), Class *object, Arg1 arg1)
126 : fn(fn), object(object), arg1(arg1) { }
130 futureInterface.reportStarted();
131 QFuture<T> future = futureInterface.future();
132 QThreadPool::globalInstance()->start(this);
138 (object->*fn)(futureInterface, arg1);
139 futureInterface.reportFinished();
142 QFutureInterface<T> futureInterface;
148 template <typename T, typename FunctionPointer, typename Arg1, typename Arg2>
149 class StoredInterfaceFunctionCall2 : public QRunnable
152 StoredInterfaceFunctionCall2(void (fn)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
153 : fn(fn), arg1(arg1), arg2(arg2) { }
157 futureInterface.reportStarted();
158 QFuture<T> future = futureInterface.future();
159 QThreadPool::globalInstance()->start(this);
165 fn(futureInterface, arg1, arg2);
166 futureInterface.reportFinished();
169 QFutureInterface<T> futureInterface;
171 Arg1 arg1; Arg2 arg2;
173 template <typename T, typename FunctionPointer, typename Class, typename Arg1, typename Arg2>
174 class StoredInterfaceMemberFunctionCall2 : public QRunnable
177 StoredInterfaceMemberFunctionCall2(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2), Class *object, Arg1 arg1, Arg2 arg2)
178 : fn(fn), object(object), arg1(arg1), arg2(arg2) { }
182 futureInterface.reportStarted();
183 QFuture<T> future = futureInterface.future();
184 QThreadPool::globalInstance()->start(this);
190 (object->*fn)(futureInterface, arg1, arg2);
191 futureInterface.reportFinished();
194 QFutureInterface<T> futureInterface;
197 Arg1 arg1; Arg2 arg2;
200 template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
201 class StoredInterfaceFunctionCall3 : public QRunnable
204 StoredInterfaceFunctionCall3(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3)
205 : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3) { }
209 futureInterface.reportStarted();
210 QFuture<T> future = futureInterface.future();
211 QThreadPool::globalInstance()->start(this);
217 fn(futureInterface, arg1, arg2, arg3);
218 futureInterface.reportFinished();
221 QFutureInterface<T> futureInterface;
223 Arg1 arg1; Arg2 arg2; Arg3 arg3;
225 template <typename T, typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3>
226 class StoredInterfaceMemberFunctionCall3 : public QRunnable
229 StoredInterfaceMemberFunctionCall3(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3)
230 : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3) { }
234 futureInterface.reportStarted();
235 QFuture<T> future = futureInterface.future();
236 QThreadPool::globalInstance()->start(this);
242 (object->*fn)(futureInterface, arg1, arg2, arg3);
243 futureInterface.reportFinished();
246 QFutureInterface<T> futureInterface;
249 Arg1 arg1; Arg2 arg2; Arg3 arg3;
252 template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
253 class StoredInterfaceFunctionCall4 : public QRunnable
256 StoredInterfaceFunctionCall4(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
257 : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) { }
261 futureInterface.reportStarted();
262 QFuture<T> future = futureInterface.future();
263 QThreadPool::globalInstance()->start(this);
269 fn(futureInterface, arg1, arg2, arg3, arg4);
270 futureInterface.reportFinished();
273 QFutureInterface<T> futureInterface;
275 Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
277 template <typename T, typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
278 class StoredInterfaceMemberFunctionCall4 : public QRunnable
281 StoredInterfaceMemberFunctionCall4(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
282 : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) { }
286 futureInterface.reportStarted();
287 QFuture<T> future = futureInterface.future();
288 QThreadPool::globalInstance()->start(this);
294 (object->*fn)(futureInterface, arg1, arg2, arg3, arg4);
295 futureInterface.reportFinished();
298 QFutureInterface<T> futureInterface;
301 Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
304 template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
305 class StoredInterfaceFunctionCall5 : public QRunnable
308 StoredInterfaceFunctionCall5(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
309 : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) { }
313 futureInterface.reportStarted();
314 QFuture<T> future = futureInterface.future();
315 QThreadPool::globalInstance()->start(this);
321 fn(futureInterface, arg1, arg2, arg3, arg4, arg5);
322 futureInterface.reportFinished();
325 QFutureInterface<T> futureInterface;
327 Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
329 template <typename T, typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
330 class StoredInterfaceMemberFunctionCall5 : public QRunnable
333 StoredInterfaceMemberFunctionCall5(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Class *object, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
334 : fn(fn), object(object), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) { }
338 futureInterface.reportStarted();
339 QFuture<T> future = futureInterface.future();
340 QThreadPool::globalInstance()->start(this);
346 (object->*fn)(futureInterface, arg1, arg2, arg3, arg4, arg5);
347 futureInterface.reportFinished();
350 QFutureInterface<T> futureInterface;
353 Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
356 template <typename T>
357 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &))
359 return (new StoredInterfaceFunctionCall0<T, void (*)(QFutureInterface<T> &)>(functionPointer))->start();
361 template <typename T, typename Arg1>
362 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1), Arg1 arg1)
364 return (new StoredInterfaceFunctionCall1<T, void (*)(QFutureInterface<T> &, Arg1), Arg1>(functionPointer, arg1))->start();
366 template <typename T, typename Arg1, typename Arg2>
367 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
369 return (new StoredInterfaceFunctionCall2<T, void (*)(QFutureInterface<T> &, Arg1, Arg2), Arg1, Arg2>(functionPointer, arg1, arg2))->start();
371 template <typename T, typename Arg1, typename Arg2, typename Arg3>
372 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3)
374 return (new StoredInterfaceFunctionCall3<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(functionPointer, arg1, arg2, arg3))->start();
376 template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
377 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
379 return (new StoredInterfaceFunctionCall4<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1, Arg2, Arg3, Arg4>(functionPointer, arg1, arg2, arg3, arg4))->start();
381 template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
382 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
384 return (new StoredInterfaceFunctionCall5<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4, Arg5), Arg1, Arg2, Arg3, Arg4, Arg5>(functionPointer, arg1, arg2, arg3, arg4, arg5))->start();
387 template <typename Class, typename T>
388 QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), Class *object)
390 return (new StoredInterfaceMemberFunctionCall0<T, void (Class::*)(QFutureInterface<T> &), Class>(fn, object))->start();
393 } // namespace QtConcurrent
397 #endif // QTCONCURRENT_RUNEX_H