OSDN Git Service

Update license.
[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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef QTCONCURRENT_RUNEX_H
34 #define QTCONCURRENT_RUNEX_H
35
36 #include <qrunnable.h>
37 #include <qfutureinterface.h>
38 #include <qthreadpool.h>
39
40 QT_BEGIN_NAMESPACE
41
42 namespace QtConcurrent {
43
44 template <typename T,  typename FunctionPointer>
45 class StoredInterfaceFunctionCall0 : public QRunnable
46 {
47 public:
48     StoredInterfaceFunctionCall0(void (fn)(QFutureInterface<T> &))
49     : fn(fn) { }
50
51     QFuture<T> start()
52     {
53         futureInterface.reportStarted();
54         QFuture<T> future = futureInterface.future();
55         QThreadPool::globalInstance()->start(this);
56         return future;
57     }
58
59     void run()
60     {
61         fn(futureInterface);
62         futureInterface.reportFinished();
63     }
64 private:
65     QFutureInterface<T> futureInterface;
66     FunctionPointer fn;
67
68 };
69 template <typename T,  typename FunctionPointer, typename Class>
70 class StoredInterfaceMemberFunctionCall0 : public QRunnable
71 {
72 public:
73     StoredInterfaceMemberFunctionCall0(void (Class::*fn)(QFutureInterface<T> &), Class *object)
74     : fn(fn), object(object) { }
75
76     QFuture<T> start()
77     {
78         futureInterface.reportStarted();
79         QFuture<T> future = futureInterface.future();
80         QThreadPool::globalInstance()->start(this);
81         return future;
82     }
83
84     void run()
85     {
86         (object->*fn)(futureInterface);
87         futureInterface.reportFinished();
88     }
89 private:
90     QFutureInterface<T> futureInterface;
91     FunctionPointer fn;
92     Class *object;
93
94 };
95
96 template <typename T,  typename FunctionPointer, typename Arg1>
97 class StoredInterfaceFunctionCall1 : public QRunnable
98 {
99 public:
100     StoredInterfaceFunctionCall1(void (fn)(QFutureInterface<T> &, Arg1), Arg1 arg1)
101     : fn(fn), arg1(arg1) { }
102
103     QFuture<T> start()
104     {
105         futureInterface.reportStarted();
106         QFuture<T> future = futureInterface.future();
107         QThreadPool::globalInstance()->start(this);
108         return future;
109     }
110
111     void run()
112     {
113         fn(futureInterface, arg1);
114         futureInterface.reportFinished();
115     }
116 private:
117     QFutureInterface<T> futureInterface;
118     FunctionPointer fn;
119     Arg1 arg1;
120 };
121 template <typename T,  typename FunctionPointer, typename Class, typename Arg1>
122 class StoredInterfaceMemberFunctionCall1 : public QRunnable
123 {
124 public:
125     StoredInterfaceMemberFunctionCall1(void (Class::*fn)(QFutureInterface<T> &, Arg1), Class *object, Arg1 arg1)
126     : fn(fn), object(object), arg1(arg1) { }
127
128     QFuture<T> start()
129     {
130         futureInterface.reportStarted();
131         QFuture<T> future = futureInterface.future();
132         QThreadPool::globalInstance()->start(this);
133         return future;
134     }
135
136     void run()
137     {
138         (object->*fn)(futureInterface, arg1);
139         futureInterface.reportFinished();
140     }
141 private:
142     QFutureInterface<T> futureInterface;
143     FunctionPointer fn;
144     Class *object;
145     Arg1 arg1;
146 };
147
148 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2>
149 class StoredInterfaceFunctionCall2 : public QRunnable
150 {
151 public:
152     StoredInterfaceFunctionCall2(void (fn)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
153     : fn(fn), arg1(arg1), arg2(arg2) { }
154
155     QFuture<T> start()
156     {
157         futureInterface.reportStarted();
158         QFuture<T> future = futureInterface.future();
159         QThreadPool::globalInstance()->start(this);
160         return future;
161     }
162
163     void run()
164     {
165         fn(futureInterface, arg1, arg2);
166         futureInterface.reportFinished();
167     }
168 private:
169     QFutureInterface<T> futureInterface;
170     FunctionPointer fn;
171     Arg1 arg1; Arg2 arg2;
172 };
173 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2>
174 class StoredInterfaceMemberFunctionCall2 : public QRunnable
175 {
176 public:
177     StoredInterfaceMemberFunctionCall2(void (Class::*fn)(QFutureInterface<T> &, Arg1, Arg2), Class *object, Arg1 arg1, Arg2 arg2)
178     : fn(fn), object(object), arg1(arg1), arg2(arg2) { }
179
180     QFuture<T> start()
181     {
182         futureInterface.reportStarted();
183         QFuture<T> future = futureInterface.future();
184         QThreadPool::globalInstance()->start(this);
185         return future;
186     }
187
188     void run()
189     {
190         (object->*fn)(futureInterface, arg1, arg2);
191         futureInterface.reportFinished();
192     }
193 private:
194     QFutureInterface<T> futureInterface;
195     FunctionPointer fn;
196     Class *object;
197     Arg1 arg1; Arg2 arg2;
198 };
199
200 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
201 class StoredInterfaceFunctionCall3 : public QRunnable
202 {
203 public:
204     StoredInterfaceFunctionCall3(void (fn)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3)
205     : fn(fn), arg1(arg1), arg2(arg2), arg3(arg3) { }
206
207     QFuture<T> start()
208     {
209         futureInterface.reportStarted();
210         QFuture<T> future = futureInterface.future();
211         QThreadPool::globalInstance()->start(this);
212         return future;
213     }
214
215     void run()
216     {
217         fn(futureInterface, arg1, arg2, arg3);
218         futureInterface.reportFinished();
219     }
220 private:
221     QFutureInterface<T> futureInterface;
222     FunctionPointer fn;
223     Arg1 arg1; Arg2 arg2; Arg3 arg3;
224 };
225 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3>
226 class StoredInterfaceMemberFunctionCall3 : public QRunnable
227 {
228 public:
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) { }
231
232     QFuture<T> start()
233     {
234         futureInterface.reportStarted();
235         QFuture<T> future = futureInterface.future();
236         QThreadPool::globalInstance()->start(this);
237         return future;
238     }
239
240     void run()
241     {
242         (object->*fn)(futureInterface, arg1, arg2, arg3);
243         futureInterface.reportFinished();
244     }
245 private:
246     QFutureInterface<T> futureInterface;
247     FunctionPointer fn;
248     Class *object;
249     Arg1 arg1; Arg2 arg2; Arg3 arg3;
250 };
251
252 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
253 class StoredInterfaceFunctionCall4 : public QRunnable
254 {
255 public:
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) { }
258
259     QFuture<T> start()
260     {
261         futureInterface.reportStarted();
262         QFuture<T> future = futureInterface.future();
263         QThreadPool::globalInstance()->start(this);
264         return future;
265     }
266
267     void run()
268     {
269         fn(futureInterface, arg1, arg2, arg3, arg4);
270         futureInterface.reportFinished();
271     }
272 private:
273     QFutureInterface<T> futureInterface;
274     FunctionPointer fn;
275     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
276 };
277 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
278 class StoredInterfaceMemberFunctionCall4 : public QRunnable
279 {
280 public:
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) { }
283
284     QFuture<T> start()
285     {
286         futureInterface.reportStarted();
287         QFuture<T> future = futureInterface.future();
288         QThreadPool::globalInstance()->start(this);
289         return future;
290     }
291
292     void run()
293     {
294         (object->*fn)(futureInterface, arg1, arg2, arg3, arg4);
295         futureInterface.reportFinished();
296     }
297 private:
298     QFutureInterface<T> futureInterface;
299     FunctionPointer fn;
300     Class *object;
301     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
302 };
303
304 template <typename T,  typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
305 class StoredInterfaceFunctionCall5 : public QRunnable
306 {
307 public:
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) { }
310
311     QFuture<T> start()
312     {
313         futureInterface.reportStarted();
314         QFuture<T> future = futureInterface.future();
315         QThreadPool::globalInstance()->start(this);
316         return future;
317     }
318
319     void run()
320     {
321         fn(futureInterface, arg1, arg2, arg3, arg4, arg5);
322         futureInterface.reportFinished();
323     }
324 private:
325     QFutureInterface<T> futureInterface;
326     FunctionPointer fn;
327     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
328 };
329 template <typename T,  typename FunctionPointer, typename Class, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
330 class StoredInterfaceMemberFunctionCall5 : public QRunnable
331 {
332 public:
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) { }
335
336     QFuture<T> start()
337     {
338         futureInterface.reportStarted();
339         QFuture<T> future = futureInterface.future();
340         QThreadPool::globalInstance()->start(this);
341         return future;
342     }
343
344     void run()
345     {
346         (object->*fn)(futureInterface, arg1, arg2, arg3, arg4, arg5);
347         futureInterface.reportFinished();
348     }
349 private:
350     QFutureInterface<T> futureInterface;
351     FunctionPointer fn;
352     Class *object;
353     Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
354 };
355
356 template <typename T>
357 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &))
358 {
359     return (new StoredInterfaceFunctionCall0<T, void (*)(QFutureInterface<T> &)>(functionPointer))->start();
360 }
361 template <typename T, typename Arg1>
362 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1), Arg1 arg1)
363 {
364     return (new StoredInterfaceFunctionCall1<T, void (*)(QFutureInterface<T> &, Arg1), Arg1>(functionPointer, arg1))->start();
365 }
366 template <typename T, typename Arg1, typename Arg2>
367 QFuture<T> run(void (*functionPointer)(QFutureInterface<T> &, Arg1, Arg2), Arg1 arg1, Arg2 arg2)
368 {
369     return (new StoredInterfaceFunctionCall2<T, void (*)(QFutureInterface<T> &, Arg1, Arg2), Arg1, Arg2>(functionPointer, arg1, arg2))->start();
370 }
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)
373 {
374     return (new StoredInterfaceFunctionCall3<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(functionPointer, arg1, arg2, arg3))->start();
375 }
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)
378 {
379     return (new StoredInterfaceFunctionCall4<T, void (*)(QFutureInterface<T> &, Arg1, Arg2, Arg3, Arg4), Arg1, Arg2, Arg3, Arg4>(functionPointer, arg1, arg2, arg3, arg4))->start();
380 }
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)
383 {
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();
385 }
386
387 template <typename Class, typename T>
388 QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), Class *object)
389 {
390     return (new StoredInterfaceMemberFunctionCall0<T, void (Class::*)(QFutureInterface<T> &), Class>(fn, object))->start();
391 }
392
393 } // namespace QtConcurrent
394
395 QT_END_NAMESPACE
396
397 #endif // QTCONCURRENT_RUNEX_H