Mercurial > projects > qtd
annotate generator/typesystem_core-java.java @ 183:d3f4f14d43a5
fixes with QObjects
author | eldar |
---|---|
date | Fri, 03 Jul 2009 20:53:07 +0000 |
parents | 39921f499a22 |
children | 7d9db724ee1d |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved. | |
4 ** | |
5 ** This file is part of Qt Jambi. | |
6 ** | |
7 ** * Commercial Usage | |
8 * Licensees holding valid Qt Commercial licenses may use this file in | |
9 * accordance with the Qt Commercial License Agreement provided with the | |
10 * Software or, alternatively, in accordance with the terms contained in | |
11 * a written agreement between you and Nokia. | |
12 * | |
13 * | |
14 * GNU General Public License Usage | |
15 * Alternatively, this file may be used under the terms of the GNU | |
16 * General Public License versions 2.0 or 3.0 as published by the Free | |
17 * Software Foundation and appearing in the file LICENSE.GPL included in | |
18 * the packaging of this file. Please review the following information | |
19 * to ensure GNU General Public Licensing requirements will be met: | |
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
22 * exception, Nokia gives you certain additional rights. These rights | |
23 * are described in the Nokia Qt GPL Exception version 1.2, included in | |
24 * the file GPL_EXCEPTION.txt in this package. | |
25 * | |
26 * Qt for Windows(R) Licensees | |
27 * As a special exception, Nokia, as the sole copyright holder for Qt | |
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the | |
29 * right for the Qt/Eclipse Integration to link to functionality | |
30 * provided by Qt Designer and its related libraries. | |
31 * | |
32 * | |
33 * If you are unsure which license is appropriate for your use, please | |
34 * contact the sales department at qt-sales@nokia.com. | |
35 | |
36 ** | |
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 package generator; | |
43 | |
44 import qt.*; | |
45 import qt.core.*; | |
46 | |
47 class QObject___ extends QObject { | |
183 | 48 |
49 public bool __stackAllocated = false; | |
50 | |
51 public bool __qobject_is_deleting = false; | |
52 /* | |
53 new(uint size, void* p = null) | |
54 { | |
55 if (!p) | |
56 { | |
57 p = malloc(size); | |
58 if (!p) | |
59 assert(false, "Out of memory"); | |
60 } | |
61 | |
62 return p; | |
63 } | |
64 | |
65 delete(void* p) | |
66 { | |
67 if (!(cast(typeof(this))p).__stackAllocated) | |
68 free(p); | |
69 } | |
70 */ | |
71 // list of QObjects references to prevent them from garbage collecting if they are managed by Qt | |
72 private static QObject[] __gc_ref_list; | |
1 | 73 |
74 // this flag controls whether QObject is managed by D's GC, or it has a parent and therefore managed by Qt | |
75 private bool __gc_managed = true; | |
183 | 76 |
77 // this flag needs to be set false when QObject is deleted from inside Qt so when deleting it from D it won't delete C++ object | |
78 public bool __no_real_delete = false; | |
1 | 79 }// class |
80 | |
81 abstract class QAbstractItemModel___ extends QAbstractItemModel { | |
82 /* private native boolean setData_native(long id, int row, int col, Object value, int role); | |
83 | |
84 public final boolean setData(int row, int col, Object value) { | |
85 return setData_native(nativeId(), row, col, value, qt.core.Qt.ItemDataRole.DisplayRole); | |
86 } | |
87 | |
88 public final boolean setData(int row, int col, Object value, int role) { | |
89 return setData_native(nativeId(), row, col, value, role); | |
90 } | |
91 | |
92 private native Object data_native(long id, int row, int col, int role); | |
93 | |
94 public final Object data(int row, int col, int role) { | |
95 return data_native(nativeId(), row, col, role); | |
96 } | |
97 | |
98 public final Object data(int row, int col) { | |
99 return data_native(nativeId(), row, col, Qt.ItemDataRole.DisplayRole); | |
100 }*/ | |
101 }// class | |
102 | |
103 class QTimer___ extends QTimer { | |
104 static private class QSingleShotTimer extends QObject { | |
105 private int timerId = -1; | |
106 public Signal0 timeout = new Signal0(); | |
107 | |
108 public QSingleShotTimer(int msec, QObject obj, String method) { | |
109 super(obj); | |
110 timeout.connect(obj, method); | |
111 timerId = startTimer(msec); | |
112 } | |
113 | |
114 protected void disposed() { | |
115 if (timerId > 0) | |
116 killTimer(timerId); | |
117 super.disposed(); | |
118 } | |
119 | |
120 protected void timerEvent(QTimerEvent e) { | |
121 if (timerId > 0) | |
122 killTimer(timerId); | |
123 timerId = -1; | |
124 timeout.emit(); | |
125 disposeLater(); | |
126 } | |
127 } | |
128 | |
129 public static void singleShot(int msec, QObject obj, String method) { | |
130 new QSingleShotTimer(msec, obj, method); | |
131 } | |
132 }// class | |
133 | |
134 class QCoreApplication___ extends QCoreApplication { | |
135 | |
136 protected static QCoreApplication m_instance = null; | |
137 | |
90 | 138 this(in string[] args) |
1 | 139 { |
140 // if (m_instance != null) | |
141 // throw new RuntimeException("QCoreApplication can only be initialized once"); | |
142 | |
143 argc = args.length; | |
144 argv = toStringzArray(args); | |
145 this(&argc, argv); | |
146 | |
147 // m_instance.aboutToQuit.connect(m_instance, "disposeOfMyself()"); | |
148 } | |
149 | |
150 private int argc; | |
151 private char **argv; | |
56
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
152 /* |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
153 public static string translate(string context, string key, string disambiguation, QCoreApplication_Encoding encoding) { |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
154 string res; |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
155 qtd_QCoreApplication_translate_nativepointerchar_nativepointerchar_nativepointerchar_Encoding(&res, toStringz(context), toStringz(key), toStringz(disambiguation), encoding); |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
156 return res; |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
157 } |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
158 |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
159 public static string translate(string context, string key, string disambiguation, QCoreApplication_Encoding encoding, int n) { |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
160 string res; |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
161 qtd_QCoreApplication_translate_nativepointerchar_nativepointerchar_nativepointerchar_Encoding_int(&res, toStringz(context), toStringz(key), toStringz(disambiguation), encoding, n); |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
162 return res; |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
163 } |
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
1
diff
changeset
|
164 */ |
1 | 165 }// class |
166 | |
167 class QTranslator___ extends QTranslator { | |
168 public final bool load(ubyte[] data) { | |
169 return load(data.ptr, data.length); | |
170 } | |
171 }// class | |
172 | |
173 class QProcess___ extends QProcess { | |
174 | |
175 public static class DetachedProcessInfo { | |
176 public DetachedProcessInfo(boolean success, long pid) { | |
177 this.success = success; | |
178 this.pid = pid; | |
179 } | |
180 | |
181 public boolean success; | |
182 public long pid; | |
183 } | |
184 | |
185 public static DetachedProcessInfo startDetached(String program, java.util.List<String> arguments, String workingDirectory) { | |
186 QNativePointer pid = new QNativePointer(QNativePointer.Type.Long); | |
187 boolean success = startDetached(program, arguments, workingDirectory, pid); | |
188 return new DetachedProcessInfo(success, pid.longValue()); | |
189 } | |
190 }// class | |
191 | |
192 class QDataStream___ extends QDataStream { | |
193 | |
194 public final bool readBoolean() { | |
195 bool res; | |
196 operator_shift_right_boolean(&res); | |
197 return res; | |
198 } | |
199 | |
200 public final byte readByte() { | |
201 byte res; | |
202 operator_shift_right_byte(&res); | |
203 return res; | |
204 } | |
205 | |
206 public final short readShort() { | |
207 short res; | |
208 operator_shift_right_short(&res); | |
209 return res; | |
210 } | |
211 | |
212 public final int readInt() { | |
213 int res; | |
214 operator_shift_right_int(&res); | |
215 return res; | |
216 } | |
217 | |
218 public final long readLong() { | |
219 long res; | |
220 operator_shift_right_long(&res); | |
221 return res; | |
222 } | |
223 | |
224 public final ushort readChar() { | |
225 ushort res; | |
226 operator_shift_right_char(&res); | |
227 return res; | |
228 } | |
229 | |
230 public final float readFloat() { | |
231 float res; | |
232 operator_shift_right_float(&res); | |
233 return res; | |
234 } | |
235 | |
236 public final double readDouble() { | |
237 double res; | |
238 operator_shift_right_double(&res); | |
239 return res; | |
240 } | |
241 | |
242 public final QDataStream writeShort(short s) { | |
243 writeShort_char(s); | |
244 return this; | |
245 } | |
246 /* | |
247 private static extern(C) void qtd_QDataStream_readString_private(void* __this_nativeId); | |
248 | |
249 private native void writeString_private(long nativeId, String string); | |
250 | |
251 public final String readString() { | |
252 if (nativeId() == 0) | |
253 throw new QNoNativeResourcesException("Function call on incomplete object of type: " + getClass().getName()); | |
254 return readString_private(nativeId()); | |
255 } | |
256 | |
257 public final void writeString(String string) { | |
258 if (nativeId() == 0) | |
259 throw new QNoNativeResourcesException("Function call on incomplete object of type: " + getClass().getName()); | |
260 writeString_private(nativeId(), string); | |
261 } | |
262 | |
263 private native int writeBytes(long id, byte buffer[], int length); | |
264 | |
265 private native int readBytes(long id, byte buffer[], int length); | |
266 | |
267 public final int writeBytes(byte buffer[]) { | |
268 return writeBytes(buffer, buffer.length); | |
269 } | |
270 | |
271 public final int writeBytes(byte buffer[], int length) { | |
272 return writeBytes(nativeId(), buffer, length); | |
273 } | |
274 | |
275 public final int readBytes(byte buffer[]) { | |
276 return readBytes(buffer, buffer.length); | |
277 } | |
278 | |
279 public final int readBytes(byte buffer[], int length) { | |
280 return readBytes(nativeId(), buffer, length); | |
281 } | |
282 */ | |
283 }// class | |
284 | |
285 class QTextStream___ extends QTextStream { | |
286 /* public final void setCodec(String codecName) { | |
287 setCodec(QNativePointer.createCharPointer(codecName)); | |
288 if (codec() != __rcCodec) | |
289 __rcCodec = null; | |
290 } | |
291 | |
292 private QNativePointer srb = new QNativePointer(QNativePointer.Type.Byte, 32) { | |
293 { | |
294 setVerificationEnabled(false); | |
295 } | |
296 }; | |
297 | |
298 public final byte readByte() { | |
299 operator_shift_right_byte(srb); | |
300 return srb.byteValue(); | |
301 } | |
302 | |
303 public final short readShort() { | |
304 operator_shift_right_short(srb); | |
305 return srb.shortValue(); | |
306 } | |
307 | |
308 public final int readInt() { | |
309 operator_shift_right_int(srb); | |
310 return srb.intValue(); | |
311 } | |
312 | |
313 public final long readLong() { | |
314 operator_shift_right_long(srb); | |
315 return srb.longValue(); | |
316 } | |
317 | |
318 public final float readFloat() { | |
319 operator_shift_right_float(srb); | |
320 return srb.floatValue(); | |
321 } | |
322 | |
323 public final double readDouble() { | |
324 operator_shift_right_double(srb); | |
325 return srb.doubleValue(); | |
326 } | |
327 | |
328 public final QTextStream writeShort(short s) { | |
329 writeShort_char((char) s); | |
330 return this; | |
331 } | |
332 | |
333 public final QTextStream writeChar(char c) { | |
334 writeShort_char(c); | |
335 return this; | |
336 } | |
337 | |
338 public final char readChar() { | |
339 operator_shift_right_short(srb); | |
340 return srb.charValue(); | |
341 } | |
342 | |
343 public final String readString() { | |
344 return readString_native(nativeId()); | |
345 } | |
346 | |
347 public final void writeString(String string) { | |
348 writeString_native(nativeId(), string); | |
349 } | |
350 | |
351 private final native String readString_native(long id); | |
352 | |
353 private final native void writeString_native(long id, String string); | |
354 */ | |
355 }// class | |
356 | |
357 class QBitArray___ extends QBitArray { | |
358 /* | |
359 @qt.QtBlockedSlot | |
360 public final void xor(QBitArray other) { | |
361 operator_xor_assign(other); | |
362 } | |
363 | |
364 @qt.QtBlockedSlot | |
365 public final void and(QBitArray other) { | |
366 operator_and_assign(other); | |
367 } | |
368 | |
369 @qt.QtBlockedSlot | |
370 public final void or(QBitArray other) { | |
371 operator_or_assign(other); | |
372 } | |
373 | |
374 @qt.QtBlockedSlot | |
375 public final void set(QBitArray other) { | |
376 operator_assign(other); | |
377 } | |
378 | |
379 @qt.QtBlockedSlot | |
380 public final QBitArray inverted() { | |
381 return operator_negate(); | |
382 } | |
383 */ | |
384 }// class | |
385 | |
386 // hfr | |
387 | |
388 class QDate___ extends QDate { | |
389 | |
390 public final int weekNumber() { | |
391 return weekNumber(null); | |
392 } | |
393 | |
394 public final int yearOfWeekNumber() { | |
395 int np; | |
396 weekNumber(&np); | |
397 return np; | |
398 } | |
399 | |
400 }// class | |
401 | |
402 class QDir___ extends QDir { | |
403 /* | |
404 @qt.QtBlockedSlot | |
405 public String at(int i) { | |
406 return operator_subscript(i); | |
407 } | |
408 */ | |
409 }// class | |
410 | |
411 class QByteArray___ extends QByteArray { | |
412 public QByteArray(String s) { | |
413 this(); | |
414 append(s); | |
415 } | |
416 | |
417 public QByteArray(byte data[]) { | |
418 this(qt.internal.QtJambiInternal.byteArrayToNativePointer(data), data.length); | |
419 } | |
420 | |
421 public final boolean contains(String str) { | |
422 return contains(new QByteArray(str)); | |
423 } | |
424 | |
425 public final int count(String str) { | |
426 return count(new QByteArray(str)); | |
427 } | |
428 | |
429 public final boolean endsWith(String str) { | |
430 return endsWith(new QByteArray(str)); | |
431 } | |
432 | |
433 public final QByteArray prepend(String str) { | |
434 return prepend(new QByteArray(str)); | |
435 } | |
436 | |
437 public final QByteArray replace(QByteArray before, String after) { | |
438 return replace(before, new QByteArray(after)); | |
439 } | |
440 | |
441 public final QByteArray replace(String before, String after) { | |
442 return replace(new QByteArray(before), new QByteArray(after)); | |
443 } | |
444 | |
445 public final boolean startsWith(String str) { | |
446 return startsWith(new QByteArray(str)); | |
447 } | |
448 | |
449 public final byte[] toByteArray() { | |
450 byte[] res = new byte[size()]; | |
451 | |
452 for (int i = 0; i < size(); i++) { | |
453 res[i] = at(i); | |
454 } | |
455 return res; | |
456 } | |
457 | |
458 @qt.QtBlockedSlot | |
459 public final QByteArray set(QByteArray other) { | |
460 operator_assign(other); | |
461 return this; | |
462 } | |
463 | |
464 }// class | |
465 | |
466 class QFile___ extends QFile { | |
467 /* | |
468 public static String decodeName(String localFileName) { | |
469 return decodeName(qt.QNativePointer.createCharPointer(localFileName)); | |
470 } | |
471 */ | |
472 }// class | |
473 | |
474 class QIODevice___ extends QIODevice { | |
475 | |
476 /** | |
477 * Gets a byte from the device. | |
478 * | |
479 * @return -1 on failure, or the value of the byte on success | |
480 */ | |
481 /* public final int getByte() { | |
482 QNativePointer np = new QNativePointer(QNativePointer.Type.Byte); | |
483 boolean success = getByte(np); | |
484 return success ? np.byteValue() : -1; | |
485 } | |
486 */ | |
180 | 487 |
488 public final long write(string str) { | |
489 return write(str.ptr, str.length); | |
490 } | |
1 | 491 }// class |
492 | |
493 class QCryptographicHash___ extends QCryptographicHash { | |
494 | |
495 public final void addData(byte data[]) { | |
496 QNativePointer np = qt.internal.QtJambiInternal.byteArrayToNativePointer(data); | |
497 addData(np, data.length); | |
498 } | |
499 | |
500 }// class | |
501 | |
502 class QTextCodec___ extends QTextCodec { | |
503 /* | |
504 static { | |
505 setCodecForTr(QTextCodec.codecForName("UTF-8")); | |
506 } | |
507 | |
508 public static QTextCodec codecForName(String name) { | |
509 return codecForName(qt.QNativePointer.createCharPointer(name)); | |
510 } | |
511 */ | |
512 }// class | |
513 | |
514 class QBuffer___ extends QBuffer { | |
515 | |
516 // retain a reference to avoid gc | |
517 private Object strongDataReference = null; | |
518 public final void setBuffer(QByteArray byteArray) { | |
90 | 519 setBuffer_private(byteArray); |
1 | 520 strongDataReference = byteArray; |
521 } | |
522 | |
523 public final void setData(byte data[]) { | |
90 | 524 setData(cast(char*)data.ptr, data.length); |
1 | 525 } |
526 | |
527 }// class | |
528 | |
529 class QSignalMapper___ extends QSignalMapper { | |
530 | |
531 private java.util.Hashtable<QObject, QObject> __rcObjectForObject = new java.util.Hashtable<QObject, QObject>(); | |
532 | |
533 private java.util.Hashtable<QObject, Object> __rcWidgetForObject = new java.util.Hashtable<QObject, Object>(); | |
534 | |
535 }// class | |
536 | |
537 class QAbstractFileEngine_MapExtensionReturn___ extends QAbstractFileEngine_MapExtensionReturn { | |
538 /* private QNativePointer currentAddressNativePointer; // don't garbage collect while in use | |
539 public final void setAddress(String address) { | |
540 currentAddressNativePointer = address != null ? QNativePointer.createCharPointer(address) : null; | |
541 address_private(currentAddressNativePointer); | |
542 } | |
543 | |
544 public final String address() { | |
545 QNativePointer np = address_private(); | |
546 return np != null ? qt.internal.QtJambiInternal.charPointerToString(np) : null; | |
547 }*/ | |
548 }// class | |
549 | |
550 class QAbstractFileEngine___ extends QAbstractFileEngine { | |
551 /** | |
552 * Adds <tt>path</tt> to the set of paths in which Qt Jambi should search for resources. Resources | |
553 * can be accessed using the "classpath:" scheme. | |
554 */ | |
555 /* public static void addSearchPathForResourceEngine(String path) | |
556 { | |
557 qt.internal.QClassPathEngine.addSearchPath(path); | |
558 } | |
559 */ | |
560 /** | |
561 * Removes <tt>path</tt> from the set of paths in which Qt Jambi searches | |
562 * for resources. | |
563 */ | |
564 /* public static void removeSearchPathForResourceEngine(String path) | |
565 { | |
566 qt.internal.QClassPathEngine.removeSearchPath(path); | |
567 } | |
568 */ | |
569 }// class | |
570 | |
571 class QAbstractFileEngine_UnMapExtensionOption___ extends QAbstractFileEngine_UnMapExtensionOption { | |
572 private QNativePointer currentAddressNativePointer; // don't garbage collect while in use | |
573 public final void setAddress(String address) { | |
574 currentAddressNativePointer = address != null ? QNativePointer.createCharPointer(address) : null; | |
575 address_private(currentAddressNativePointer); | |
576 } | |
577 | |
578 public final String address() { | |
579 QNativePointer np = address_private(); | |
580 return np != null ? qt.internal.QtJambiInternal.charPointerToString(np) : null; | |
581 } | |
582 }// class | |
583 | |
584 class QFutureWatcher___ extends QFutureWatcher { | |
585 | |
586 public final QFuture<T> future() { | |
587 if (nativeId() == 0) | |
588 throw new QNoNativeResourcesException("Function call on incomplete object of type: " +getClass().getName()); | |
589 return __qt_future(nativeId()); | |
590 } | |
591 private native QFuture<T> __qt_future(long nativeId); | |
592 | |
593 }// class | |
594 | |
595 class QFutureWatcherVoid___ extends QFutureWatcherVoid { | |
596 | |
597 public final QFutureVoid future() { | |
598 if (nativeId() == 0) | |
599 throw new QNoNativeResourcesException("Function call on incomplete object of type: " +getClass().getName()); | |
600 return __qt_future(nativeId()); | |
601 } | |
602 | |
603 private native QFutureVoid __qt_future(long nativeId); | |
604 | |
605 }// class | |
606 | |
607 class QFutureSynchronizer___ extends QFutureSynchronizer { | |
608 | |
609 public final java.util.List<QFuture<T>> futures() { | |
610 if (nativeId() == 0) | |
611 throw new QNoNativeResourcesException("Function call on incomplete object of type: " +getClass().getName()); | |
612 return __qt_futures(nativeId()); | |
613 } | |
614 private native java.util.List<QFuture<T>> __qt_futures(long nativeId); | |
615 | |
616 }// class | |
617 | |
618 class QFutureSynchronizerVoid___ extends QFutureSynchronizerVoid { | |
619 | |
620 public final java.util.List<QFutureVoid> futures() { | |
621 if (nativeId() == 0) | |
622 throw new QNoNativeResourcesException("Function call on incomplete object of type: " +getClass().getName()); | |
623 return __qt_futures(nativeId()); | |
624 } | |
625 private native java.util.List<QFutureVoid> __qt_futures(long nativeId); | |
626 | |
627 }// class | |
628 | |
629 class QLineF___ extends QLineF { | |
630 /* | |
631 public final QLineF.IntersectType intersect(QLineF line, qt.core.QPointF intersectionPoint) { | |
632 return intersect(line, intersectionPoint != null ? intersectionPoint.nativePointer() : null); | |
633 } | |
634 */ | |
635 }// class | |
636 | |
637 /** | |
638 The QtConcurrent class contains static methods for running computations in parallel (using separate threads) | |
639 on the items in a java.util.Collection, such as a Vector or LinkedList. We will now describe these methods. | |
640 | |
641 The QtConcurrent::map(), QtConcurrent::mapped() and QtConcurrent::mappedReduced() functions run computations in parallel on the items in a sequence such as a QList or a QVector. QtConcurrent::map() modifies a sequence in-place, QtConcurrent::mapped() returns a new sequence containing the modified content, and QtConcurrent::mappedReduced() returns a single result. | |
642 Concurrent Map | |
643 <p> | |
644 QtConcurrent::mapped() takes an input sequence and a map function. This map function is then called for each item in the sequence, and a new sequence containing the return values from the map function is returned. | |
645 <p> | |
646 The map function must be of the form: | |
647 <pre> | |
648 U function(const T &t); | |
649 </pre> | |
650 T and U can be any type (and they can even be the same type), but T must match the type stored in the sequence. The function returns the modified or mapped content. | |
651 <p> | |
652 This example shows how to apply a scale function to all the items in a sequence: | |
653 <pre> | |
654 QImage scaled(const QImage &image) | |
655 { | |
656 return image.scaled(100, 100); | |
657 } | |
658 | |
659 QList <QImage> images = ...; | |
660 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled); | |
661 </pre> | |
662 The results of the map are made available through QFuture. See the QFuture and QFutureWatcher documentation for more information on how to use QFuture in your applications. | |
663 <p> | |
664 If you want to modify a sequence in-place, use QtConcurrent::map(). The map function must then be of the form: | |
665 <pre> | |
666 U function(T &t); | |
667 </pre> | |
668 Note that the return value and return type of the map function are not used. | |
669 <p> | |
670 Using QtConcurrent::map() is similar to using QtConcurrent::mapped(): | |
671 <pre> | |
672 void scale(QImage &image) | |
673 { | |
674 image = image.scaled(100, 100); | |
675 } | |
676 | |
677 QList<QImage> images = ...; | |
678 QFuture<void> future = QtConcurrent::map(images, scale); | |
679 </pre> | |
680 Since the sequence is modified in place, QtConcurrent::map() does not return any results via QFuture. However, you can still use QFuture and QFutureWatcher to monitor the status of the map. | |
681 Concurrent Map-Reduce | |
682 <p> | |
683 QtConcurrent::mappedReduced() is similar to QtConcurrent::mapped(), but instead of returning a sequence with the new results, the results are combined into a single value using a reduce function. | |
684 <p> | |
685 The reduce function must be of the form: | |
686 <pre> | |
687 V function(T &result, const U &intermediate) | |
688 </pre> | |
689 T is the type of the final result, U is the return type of the map function. Note that the return value and return type of the reduce function are not used. | |
690 <p> | |
691 Call QtConcurrent::mappedReduced() like this: | |
692 <pre> | |
693 void addToCollage(QImage &collage, const QImage &thumbnail) | |
694 { | |
695 QPainter p(&collage); | |
696 static QPoint offset = QPoint(0, 0); | |
697 p.drawImage(offset, thumbnail); | |
698 offset += ...; | |
699 } | |
700 | |
701 QList<QImage> images = ...; | |
702 QFuture<QImage> collage = QtConcurrent::mappedReduced(images, scaled, addToCollage); | |
703 </pre> | |
704 The reduce function will be called once for each result returned by the map function, and should merge the intermediate into the result variable. QtConcurrent::mappedReduced() guarantees that only one thread will call reduce at a time, so using a mutex to lock the result variable is not neccesary. The QtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done. If QtConcurrent::UnorderedReduce is used (the default), the order is undefined, while QtConcurrent::OrderedReduce ensures that the reduction is done in the order of the original sequence. | |
705 Additional API Features | |
706 Using Iterators instead of Sequence | |
707 <p> | |
708 Each of the above functions has a variant that takes an iterator range instead of a sequence. You use them in the same way as the sequence variants: | |
709 <pre> | |
710 QList<QImage> images = ...; | |
711 | |
712 QFuture<QImage> thumbnails = QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled); | |
713 | |
714 // map in-place only works on non-const iterators | |
715 QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale); | |
716 | |
717 QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage); | |
718 </pre> | |
719 Blocking Variants | |
720 <p> | |
721 Each of the above functions has a blocking variant that returns the final result instead of a QFuture. You use them in the same way as the asynchronous variants. | |
722 <pre> | |
723 QList<QImage> images = ...; | |
724 | |
725 // each call blocks until the entire operation is finished | |
726 QList<QImage> future = QtConcurrent::blockingMapped(images, scaled); | |
727 | |
728 QtConcurrent::blockingMap(images, scale); | |
729 | |
730 QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage); | |
731 </pre> | |
732 Note that the result types above are not QFuture objects, but real result types (in this case, QList<QImage> and QImage). | |
733 Using Member Functions | |
734 <p> | |
735 QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence: | |
736 <pre> | |
737 // squeeze all strings in a QStringList | |
738 QStringList strings = ...; | |
739 QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze); | |
740 | |
741 // swap the rgb values of all pixels on a list of images | |
742 QList<QImage> images = ...; | |
743 QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped); | |
744 | |
745 // create a set of the lengths of all strings in a list | |
746 QStringList strings = ...; | |
747 QFuture<QSet<int> > wordLengths = QtConcurrent::mappedReduced(string, &QString::length, &QSet<int>::insert); | |
748 </pre> | |
749 Note that when using QtConcurrent::mappedReduced(), you can mix the use of normal and member functions freely: | |
750 <p> | |
751 <pre> | |
752 // can mix normal functions and member functions with QtConcurrent::mappedReduced() | |
753 | |
754 // compute the average length of a list of strings | |
755 extern void computeAverage(int &average, int length); | |
756 QStringList strings = ...; | |
757 QFuture<int> averageWordLength = QtConcurrent::mappedReduced(strings, &QString::length, computeAverage); | |
758 | |
759 // create a set of the color distribution of all images in a list | |
760 extern int colorDistribution(const QImage &string); | |
761 QList<QImage> images = ...; | |
762 QFuture<QSet<int> > totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution, QSet<int>::insert); | |
763 </pre> | |
764 Using Function Objects | |
765 <p> | |
766 QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept function objects, which can be used to add state to a function call. The result_type typedef must define the result type of the function call operator: | |
767 <pre> | |
768 struct Scaled | |
769 { | |
770 Scaled(int size) | |
771 : m_size(size) { } | |
772 | |
773 typedef QImage result_type; | |
774 | |
775 QImage operator()(const QImage &image) | |
776 { | |
777 return image.scaled(m_size, m_size); | |
778 } | |
779 | |
780 int m_size; | |
781 }; | |
782 | |
783 QList<QImage> images = ...; | |
784 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100)); | |
785 </pre> | |
786 Using Bound Function Arguments | |
787 <p> | |
788 Note that Qt does not provide support for bound functions. This is provided by 3rd party libraries like Boost or C++ TR1 Library Extensions. | |
789 <p> | |
790 If you want to use a map function that takes more than one argument you can use boost::bind() or std::tr1::bind() to transform it onto a function that takes one argument. | |
791 <p> | |
792 As an example, we'll use QImage::scaledToWidth(): | |
793 <pre> | |
794 QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const; | |
795 </pre> | |
796 scaledToWidth takes three arguments (including the "this" pointer) and can't be used with QtConcurrent::mapped() directly, because QtConcurrent::mapped() expects a function that takes one argument. To use QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a value for the width and the transformation mode: | |
797 <pre> | |
798 boost::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation) | |
799 </pre> | |
800 The return value from boost::bind() is a function object (functor) with the following signature: | |
801 <pre> | |
802 QImage scaledToWith(const QImage &image) | |
803 </pre> | |
804 This matches what QtConcurrent::mapped() expects, and the complete example becomes: | |
805 <pre> | |
806 QList>QImage< images = ...; | |
807 QFuture>QImage< thumbnails = QtConcurrent::mapped(images, boost::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation)); | |
808 </pre> | |
809 */ | |
810 class QtConcurrent___ extends QtConcurrent { | |
811 | |
812 static { | |
813 qt.QtJambi_LibraryInitializer.init(); | |
814 qt.core.QtJambi_LibraryInitializer.init(); | |
815 } | |
816 | |
817 /** | |
818 * An implemetation of this interface is given one to QtConcurrent's map() methods. | |
819 * The map() method of this interface is called for each object in a java.util.Collection. | |
820 * | |
821 */ | |
822 public interface MapFunctor<T> { | |
823 | |
824 /** | |
825 * This function is called for each item in the Collection. The function is then free to alter <tt>object</tt> as it see fit. | |
826 */ | |
827 public void map(T object); | |
828 } | |
829 /** | |
830 * Calls function once for each item in sequence. The function is passed a reference to the item, so that any modifications done to the item will appear in sequence. | |
831 */ | |
832 public static native <T> QFutureVoid map(java.util.Collection<T> sequence, MapFunctor<T> functor); | |
833 | |
834 /** | |
835 * Calls function once for each item in sequence. The function is passed a reference to the item, so that any modifications done to the item will appear in sequence. | |
836 */ | |
837 public static native <T> void blockingMap(java.util.Collection<T> sequence, MapFunctor<T> functor); | |
838 | |
839 /** | |
840 * Implement this interface to perform a mapped operation. An implementation of the interface is sendt | |
841 * to ome of the mapped methods of QtConcurrent, which applies the MappedFunctor.map() method to all elements in a collection, | |
842 * and returns the result. | |
843 */ | |
844 public interface MappedFunctor<U, T> { | |
845 | |
846 /** | |
847 * This method is called for each object in a collection. It should returned a new altered | |
848 * object. | |
849 */ | |
850 public U map(T object); | |
851 } | |
852 | |
853 /** | |
854 * Calls function once for each item in sequence and returns a future with each mapped item as a result. You can QFutureIterator to iterate through the results. | |
855 * | |
856 */ | |
857 public static native <U, T> QFuture<U> mapped(java.util.Collection<T> sequence, MappedFunctor<U, T> functor); | |
858 | |
859 /** | |
860 * Calls function once for each item in sequence and returns a future with each mapped item as a result. You can QFutureIterator to iterate through the results. | |
861 */ | |
862 public static native <U, T> java.util.List<U> blockingMapped(java.util.Collection<T> sequence, MappedFunctor<U, T> functor); | |
863 | |
864 /** | |
865 * Implement this interface in order to perform a reduce operation. | |
866 * <p> | |
867 * The reduce method will be called once per intermediate result (the result of the mapping of the data) | |
868 * and the very first time the reduce() method is called for the particular data set, the result is set to | |
869 * the returned value of the defaultResult() method. | |
870 */ | |
871 public interface ReducedFunctor<U, T> { | |
872 public U defaultResult(); | |
873 | |
874 /** | |
875 * Performs a reduce operation on <tt>intermediate</tt>. <tt>result</tt> is the result of the reduction. | |
876 */ | |
877 public void reduce(U result, T intermediate); | |
878 } | |
879 | |
880 /** | |
881 * This is an overloaded method provided for convenience. | |
882 * <p> | |
883 * It is equivalent of mappedReduced(sequence, functor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce) | |
884 */ | |
885 public static <U, V, T> QFuture<U> mappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor) { | |
886 return mappedReduced(sequence, functor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce); | |
887 } | |
888 | |
889 | |
890 /** | |
891 * This is an overloaded method provided for convenience. | |
892 * <p> | |
893 * Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions. | |
894 * | |
895 */ | |
896 public static <U, V, T> QFuture<U> mappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, ReduceOption ... options) { | |
897 return mappedReduced(sequence, functor, reducedFunctor, new ReduceOptions(options)); | |
898 } | |
899 | |
900 /** | |
901 * Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction. | |
902 * <p> | |
903 * Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions. | |
904 * | |
905 */ | |
906 public static <U, V, T> QFuture<U> mappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, ReduceOptions options) { | |
907 return mappedReduced(sequence, functor, reducedFunctor, options.value()); | |
908 } | |
909 | |
910 private native static <U, V, T> QFuture<U> mappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, int options); | |
911 | |
912 | |
913 /** | |
914 * This is an overloaded method provided for convenience. | |
915 * <p> | |
916 * It is equivalent of calling blockingMappedReduced(sequence, functor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce) | |
917 * | |
918 */ | |
919 public static <U, V, T> U blockingMappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor) { | |
920 return blockingMappedReduced(sequence, functor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce); | |
921 } | |
922 | |
923 /** | |
924 * Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction. | |
925 * <p> | |
926 * Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions. | |
927 * <p> | |
928 * Note: This function will block until all items in the sequence have been processed. | |
929 */ | |
930 public static <U, V, T> U blockingMappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, ReduceOption ... options) { | |
931 return blockingMappedReduced(sequence, functor, reducedFunctor, new ReduceOptions(options)); | |
932 } | |
933 | |
934 /** | |
935 * Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction. | |
936 * <p> | |
937 * Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions. | |
938 * <p> | |
939 * Note: This function will block until all items in the sequence have been processed. | |
940 */ | |
941 public static <U, V, T> U blockingMappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, ReduceOptions options) { | |
942 return blockingMappedReduced(sequence, functor, reducedFunctor, options.value()); | |
943 } | |
944 | |
945 private native static <U, V, T> U blockingMappedReduced(java.util.Collection<T> sequence, MappedFunctor<V, T> functor, ReducedFunctor<U, V> reducedFunctor, int options); | |
946 | |
947 /** | |
948 * An implementation of this interface is given to one of QtConcurrent's filtered() methods. | |
949 * The filter method if this interface is called for each item in a java.util.Collection. | |
950 * | |
951 */ | |
952 public interface FilteredFunctor<T> { | |
953 | |
954 /** | |
955 * This method is called for each item in a java.util.Collection. The items for which | |
956 * this method returns true are removed from the collection. | |
957 */ | |
958 public boolean filter(T object); | |
959 } | |
960 | |
961 /** | |
962 * Calls filterFunctor's filtered() method once for each item in sequence and returns a new Sequence of kept items. If filterFunction returns true, a copy of the item is put in the new Sequence. Otherwise, the item will not appear in the new Sequence. | |
963 */ | |
964 public native static <T> QFuture<T> filtered(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor); | |
965 | |
966 /** | |
967 * Calls filterFunctor's filtered() method once for each item in sequence and returns a new Sequence of kept items. If filterFunction returns true, a copy of the item is put in the new Sequence. Otherwise, the item will not appear in the new Sequence. | |
968 */ | |
969 public native static <T> java.util.List<T> blockingFiltered(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor); | |
970 | |
971 | |
972 /** | |
973 * This is an overloaded method provided for convenience. It is equivalent of calling filteredReduced(sequence, filteredFunctor, ReduceOption.UnorderedReduce, ReduceOption.Seq This is an overloaded method provided for convenience. It is equivalent of calling filteredReduced) | |
974 */ | |
975 public static <U, T> QFuture<U> filteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor) { | |
976 return filteredReduced(sequence, filteredFunctor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce); | |
977 } | |
978 | |
979 /** | |
980 * Calls filterFunction once for each item in sequence. If filterFunction returns true for an item, that item is then passed to reduceFunction. In other words, the return value is the result of reduceFunction for each item where filterFunction returns true. | |
981 * <p> | |
982 * Note that while filterFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined if reduceOptions is QtConcurrent::UnorderedReduce. If reduceOptions is QtConcurrent::OrderedReduce, reduceFunction is called in the order of the original sequence. | |
983 */ | |
984 public static <U, T> QFuture<U> filteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, ReduceOption ... options) { | |
985 return filteredReduced(sequence, filteredFunctor, reducedFunctor, new ReduceOptions(options)); | |
986 } | |
987 | |
988 /** | |
989 * Calls filterFunction once for each item in sequence. If filterFunction returns true for an item, that item is then passed to reduceFunction. In other words, the return value is the result of reduceFunction for each item where filterFunction returns true. | |
990 * <p> | |
991 * Note that while filterFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined if reduceOptions is QtConcurrent::UnorderedReduce. If reduceOptions is QtConcurrent::OrderedReduce, reduceFunction is called in the order of the original sequence. | |
992 */ | |
993 public static <U, T> QFuture<U> filteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, ReduceOptions options) { | |
994 return filteredReduced(sequence, filteredFunctor, reducedFunctor, options.value()); | |
995 } | |
996 private native static <U, T> QFuture<U> filteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, int options); | |
997 | |
998 /** | |
999 * This is an overloaded method provided for convenience. It is the equivalent of calling blockingFilteredReduced(sequence, filteredFunctor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce) | |
1000 */ | |
1001 public static <U, T> U blockingFilteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor) { | |
1002 return blockingFilteredReduced(sequence, filteredFunctor, reducedFunctor, ReduceOption.UnorderedReduce, ReduceOption.SequentialReduce); | |
1003 } | |
1004 | |
1005 /** | |
1006 * Calls filterFunction once for each item in sequence. If filterFunction returns true for an item, that item is then passed to reduceFunction. In other words, the return value is the result of reduceFunction for each item where filterFunction returns true. | |
1007 * <p> | |
1008 * Note that while filterFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined if reduceOptions is QtConcurrent::UnorderedReduce. If reduceOptions is QtConcurrent::OrderedReduce, reduceFunction is called in the order of the original sequence. | |
1009 */ | |
1010 public static <U, T> U blockingFilteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, ReduceOption ... options) { | |
1011 return blockingFilteredReduced(sequence, filteredFunctor, reducedFunctor, new ReduceOptions(options)); | |
1012 } | |
1013 | |
1014 /** | |
1015 * Calls filterFunction once for each item in sequence. If filterFunction returns true for an item, that item is then passed to reduceFunction. In other words, the return value is the result of reduceFunction for each item where filterFunction returns true. | |
1016 * <p> | |
1017 * Note that while filterFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined if reduceOptions is QtConcurrent::UnorderedReduce. If reduceOptions is QtConcurrent::OrderedReduce, reduceFunction is called in the order of the original sequence. | |
1018 */ | |
1019 public static <U, T> U blockingFilteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, ReduceOptions options) { | |
1020 return blockingFilteredReduced(sequence, filteredFunctor, reducedFunctor, options.value()); | |
1021 } | |
1022 | |
1023 private native static <U, T> U blockingFilteredReduced(java.util.Collection<T> sequence, FilteredFunctor<T> filteredFunctor, ReducedFunctor<U, T> reducedFunctor, int options); | |
1024 | |
1025 /** | |
1026 * Executes the method <tt>m</tt> through the QtConcurrent framework with the given arguments. The returned QFuture object's result wil be the | |
1027 * return value of <tt>m</tt>. Note that this method does not accept function that return void. use runVoidMethod() for this. | |
1028 */ | |
1029 public static <T> QFuture<T> run(Object _this, java.lang.reflect.Method m, Object ... args) { | |
1030 if (m.getReturnType() == null || m.getReturnType().equals(Void.TYPE)) | |
1031 throw new IllegalArgumentException("Cannot call run on method returning void. Use 'runVoidMethod' instead."); | |
1032 | |
1033 return runPrivate(_this, m.getDeclaringClass(), m, args, qt.internal.QtJambiInternal.resolveConversionSchema(m.getParameterTypes(), m.getParameterTypes()), qt.internal.QtJambiInternal.typeConversionCode(m.getReturnType())); | |
1034 } | |
1035 private native static <T> QFuture<T> runPrivate(Object _this, Class<?> declaringClass, java.lang.reflect.Method m, Object args[], int conversionScheme[], byte returnType); | |
1036 | |
1037 /** | |
1038 * Executes the method <tt>m</tt> with the given arguments using the QtConcurrent framework. Notice that runVoidMethod() does not | |
1039 * accept methods that has a return value. Use the run() method for this purpose. | |
1040 */ | |
1041 public static QFutureVoid runVoidMethod(Object _this, java.lang.reflect.Method m, Object ... args) { | |
1042 if (m.getReturnType() != null && !m.getReturnType().equals(Void.TYPE)) | |
1043 throw new IllegalArgumentException("Cannot call runVoidMethod on method returning non-void type. Use 'run' instead."); | |
1044 | |
1045 return runVoidMethodPrivate(_this, m.getDeclaringClass(), m, args, qt.internal.QtJambiInternal.resolveConversionSchema(m.getParameterTypes(), m.getParameterTypes())); | |
1046 } | |
1047 private native static QFutureVoid runVoidMethodPrivate(Object _this, Class<?> declaringClass, java.lang.reflect.Method m, Object args[], int conversionScheme[]); | |
1048 | |
1049 | |
1050 }// class |