This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
asyncfuture.h
1806 lines (1450 loc) · 49.2 KB
/
asyncfuture.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* AsyncFuture Version: 0.4.1 */
#pragma once
#include <QFuture>
#include <QMetaMethod>
#include <QPointer>
#include <QThread>
#include <QFutureWatcher>
#include <QCoreApplication>
#include <QMutex>
#include <functional>
#define ASYNCFUTURE_ERROR_OBSERVE_VOID_WITH_ARGUMENT "Observe a QFuture<void> but your callback contains an input argument"
#define ASYNCFUTURE_ERROR_CALLBACK_NO_MORE_ONE_ARGUMENT "Callback function should not take more than 1 argument"
#define ASYNCFUTURE_ERROR_ARGUMENT_MISMATCHED "The callback function is not callable. The input argument doesn't match with the observing QFuture type"
#define ASYNC_FUTURE_CALLBACK_STATIC_ASSERT(Tag, Completed) \
static_assert(Private::arg_count<Completed>::value <= 1, Tag ASYNCFUTURE_ERROR_CALLBACK_NO_MORE_ONE_ARGUMENT); \
static_assert(!(std::is_same<void, T>::value && Private::arg_count<Completed>::value >= 1), Tag ASYNCFUTURE_ERROR_OBSERVE_VOID_WITH_ARGUMENT); \
static_assert( Private::decay_is_same<T, Private::Arg0Type<Completed>>::value || \
Private::arg0_is_future<Completed>::value || \
std::is_same<void, Private::Arg0Type<Completed>>::value, Tag ASYNCFUTURE_ERROR_ARGUMENT_MISMATCHED);
namespace AsyncFuture {
/* Naming Convention
*
* typename T - The type of observable QFuture
* typename R - The return type of callback
*/
namespace Private {
/* Begin traits functions */
// Determine is the input type a QFuture
template <typename T>
struct future_traits {
enum {
is_future = 0
};
typedef void arg_type;
};
template <template <typename> class C, typename T>
struct future_traits<C <T> >
{
enum {
is_future = 0
};
typedef void arg_type;
};
template <typename T>
struct future_traits<QFuture<QFuture<T>>> : public future_traits<QFuture<T>> {
};
template <typename T>
struct future_traits<QFuture<T> >{
enum {
is_future = 1
};
typedef T arg_type;
};
// function_traits: Source: http://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
enum {
result_type_is_future = future_traits<result_type>::is_future
};
// If the result_type is a QFuture<T>, the type will be T. Otherwise, it is void
typedef typename future_traits<result_type>::arg_type future_arg_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};
/* It is an additional to the original function_traits to handle non-const function (with mutable keyword lambda). */
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...)>
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
enum {
result_type_is_future = future_traits<result_type>::is_future
};
// If the result_type is a QFuture<T>, the type will be T. Otherwise, it is void
typedef typename future_traits<result_type>::arg_type future_arg_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};
/// Decay 2nd parameter and check is it same as the first argument
template <typename T, typename U>
struct decay_is_same :
std::is_same<T, typename std::decay<U>::type>::type
{};
template <typename T>
struct signal_traits {
// Match class member function only
};
template <typename R, typename C>
struct signal_traits<R (C::*)()> {
typedef void result_type;
};
template <typename R, typename C, typename ARG0>
struct signal_traits<R (C::*)(ARG0)> {
typedef typename std::decay<ARG0>::type result_type;
};
template <typename T>
struct arg0_traits : public arg0_traits<decltype(&T::operator())> {
};
template <typename C, typename R>
struct arg0_traits<R(C::*)() const> {
typedef void type;
};
template <typename C, typename R>
struct arg0_traits<R(C::*)()> {
typedef void type;
};
template <typename C, typename R, typename Arg0, typename ...Args>
struct arg0_traits<R(C::*)(Arg0, Args...) const> {
typedef Arg0 type;
};
template <typename C, typename R, typename Arg0, typename ...Args>
struct arg0_traits<R(C::*)(Arg0, Args...)> {
typedef Arg0 type;
};
template <typename R>
struct arg0_traits<R()> {
typedef void type;
};
template <typename R, typename Arg0, typename ...Args>
struct arg0_traits<R(Arg0, Args...)> {
typedef Arg0 type;
};
// Obtain the observable type according to the Functor
template <typename T>
struct observable_traits: public observable_traits<decltype(&T::operator())> {
};
template <typename C, typename R, typename ...Args>
struct observable_traits<QFuture<QFuture<R>>(C::*)(Args...) const> {
typedef R type;
};
template <typename C, typename R, typename ...Args>
struct observable_traits<QFuture<QFuture<R>>(C::*)(Args...)> {
typedef R type;
};
template <typename C, typename R, typename ...Args>
struct observable_traits<QFuture<R>(C::*)(Args...) const> {
typedef R type;
};
template <typename C, typename R, typename ...Args>
struct observable_traits<QFuture<R>(C::*)(Args...)> {
typedef R type;
};
template <typename C, typename R, typename ...Args>
struct observable_traits<R(C::*)(Args...) const> {
typedef R type;
};
template <typename C, typename R, typename ...Args>
struct observable_traits<R(C::*)(Args...)> {
typedef R type;
};
template <typename Functor>
using RetType = typename function_traits<Functor>::result_type;
template <typename Functor>
using Arg0Type = typename arg0_traits<Functor>::type;
template <typename Functor>
struct ret_type_is_void {
enum {
value = std::is_same<RetType<Functor>, void>::value
};
};
template <typename Functor>
struct ret_type_is_future {
enum {
value = future_traits<RetType<Functor>>::is_future
};
};
template <typename Functor>
struct arg0_is_future {
enum {
value = future_traits<typename std::decay<Arg0Type<Functor>>::type>::is_future
};
};
template <typename Functor>
struct arg_count_is_zero {
enum {
value = (function_traits<Functor>::arity == 0)
};
};
template <typename Functor>
struct arg_count_is_not_zero {
enum {
value = (function_traits<Functor>::arity > 0)
};
};
template <typename Functor>
struct arg_count_is_one {
enum {
value = (function_traits<Functor>::arity == 1)
};
};
template <typename Functor>
struct arg_count {
enum {
value = function_traits<Functor>::arity
};
};
template<typename T>
struct False : std::false_type {
};
template< typename Functor, typename T>
struct is_callable {
enum {
value = (arg_count<Functor>::value == 1) &&
(!std::is_same<void, T>::value) &&
(std::is_convertible<Arg0Type<Functor>, T>::value)
};
};
/* End of traits functions */
// Value is a wrapper of a data structure which could contain <void> type.
// AsyncFuture does not use QVariant because it needs user to register before use.
template <typename R>
class Value {
public:
Value() {
}
Value(R&& v) : value(v){
}
Value(R* v) : value(*v) {
}
Value(QFuture<R> future) {
value = future.result();
}
R value;
};
template <>
class Value<void> {
public:
Value() {
}
Value(void*) {
}
Value(QFuture<void> future) {
Q_UNUSED(future);
}
};
template <typename F>
void runInMainThread(F func) {
QObject tmp;
QObject::connect(&tmp, &QObject::destroyed,
QCoreApplication::instance(), std::move(func), Qt::QueuedConnection);
}
/*
* @param owner If the object is destroyed, it should destroy the watcher
* @param contextObject Determine the receiver callback
*/
template <typename T, typename Finished, typename Canceled, typename Progress, typename ProgressRange>
void watch(QFuture<T> future,
const QObject* owner,
const QObject* contextObject,
Finished finished,
Canceled canceled,
Progress progress,
ProgressRange progressRange) {
Q_ASSERT(owner);
QPointer<const QObject> ownerAlive = owner;
QPointer<QFutureWatcher<T>> watcher(new QFutureWatcher<T>());
if (owner) {
// Don't set parent as the context object as it may live in different thread
QObject::connect(owner, &QObject::destroyed,
watcher, [watcher]() {
delete watcher;
});
}
if (contextObject) {
QObject::connect(watcher, &QFutureWatcher<T>::finished,
contextObject, [=]() {
bool watcherCancelled = true;
if(!watcher.isNull()) {
watcherCancelled = watcher->isCanceled();
delete watcher;
} else {
return;
}
if (ownerAlive.isNull()) {
return;
}
if(!watcherCancelled) {
finished();
} else {
canceled();
}
});
QObject::connect(watcher, &QFutureWatcher<T>::canceled,
contextObject, [=]() {
if(!watcher.isNull()) {
delete watcher;
} else {
return;
}
if (ownerAlive.isNull()) {
return;
}
canceled();
});
QObject::connect(watcher, &QFutureWatcher<T>::progressValueChanged,
contextObject, [=](int value) {
progress(value);
});
QObject::connect(watcher, &QFutureWatcher<T>::progressRangeChanged,
contextObject, [=](int min, int max) {
progressRange(min, max);
});
} else {
QObject::connect(watcher, &QFutureWatcher<T>::finished,
[=]() {
if(!watcher.isNull()) {
delete watcher;
}
if (ownerAlive.isNull()) {
return;
}
finished();
});
QObject::connect(watcher, &QFutureWatcher<T>::canceled,
[=]() {
if(!watcher.isNull()) {
delete watcher;
}
if (ownerAlive.isNull()) {
return;
}
canceled();
});
QObject::connect(watcher, &QFutureWatcher<T>::progressValueChanged,
[=](int value) {
progress(value);
});
QObject::connect(watcher, &QFutureWatcher<T>::progressRangeChanged,
[=](int min, int max) {
progressRange(min, max);
});
}
if ((QThread::currentThread() != QCoreApplication::instance()->thread()) &&
(contextObject == 0 || QThread::currentThread() != contextObject->thread())) {
// Move watcher to main thread if context object is not set.
watcher->moveToThread(QCoreApplication::instance()->thread());
}
watcher->setFuture(future);
}
/* DeferredFuture implements a QFutureInterface that could complete/cancel a QFuture.
*
* 1) It is a private class that won't export to public
*
* 2) Its member function do not use <T> to avoid to use template specialization to handle <void>. Type checking should be done by user classes (e.g Deferred)
*
*/
template <typename T>
class DeferredFuture : public QObject, public QFutureInterface<T>{
public:
~DeferredFuture() {
cancel();
}
template <typename ANY>
void track(QFuture<ANY> future) {
QPointer<DeferredFuture<T>> thiz = this;
QFutureWatcher<ANY> *watcher = new QFutureWatcher<ANY>();
if ((QThread::currentThread() != QCoreApplication::instance()->thread())) {
watcher->moveToThread(QCoreApplication::instance()->thread());
}
QObject::connect(watcher, &QFutureWatcher<ANY>::finished, [=]() {
watcher->disconnect();
watcher->deleteLater();
});
QObject::connect(watcher, &QFutureWatcher<ANY>::progressValueChanged, this, [=](int value) {
if (thiz.isNull()) {
return;
}
thiz->setWatchProgressValue(value);
});
QObject::connect(watcher, &QFutureWatcher<ANY>::progressRangeChanged, this, [=](int min, int max) {
if (thiz.isNull()) {
return;
}
thiz->setWatchProgressRange(min, max);
});
QObject::connect(watcher, &QFutureWatcher<ANY>::started, this, [=](){
thiz->reportStarted();
});
QObject::connect(watcher, &QFutureWatcher<ANY>::paused, this, [=](){
thiz->setPaused(true);
});
QObject::connect(watcher, &QFutureWatcher<ANY>::resumed, this, [=](){
thiz->setPaused(false);
});
watcher->setFuture(future);
setWatchProgressRange(future.progressMinimum(), future.progressMaximum());
setWatchProgressValue(future.progressValue());
if (future.isStarted()) {
QFutureInterface<T>::reportStarted();
}
if (future.isPaused()) {
QFutureInterface<T>::setPaused(true);
}
}
bool isFinished() const {
return QFutureInterface<T>::isFinished();
}
// complete<void>()
void complete() {
if (isFinished()) {
return;
}
QFutureInterface<T>::reportFinished();
}
template <typename R>
void complete(R value) {
if (isFinished()) {
return;
}
reportResult(value);
QFutureInterface<T>::reportFinished();
}
template <typename R>
void complete(QList<R>& value) {
if (isFinished()) {
return;
}
reportResult(value);
QFutureInterface<T>::reportFinished();
}
template <typename R>
void complete(Value<R> value) {
this->complete(value.value);
}
void complete(Value<void> value) {
Q_UNUSED(value);
this->complete();
}
void complete(QFuture<T> future) {
incWeakRefCount();
auto onFinished = [=]() {
this->completeByFinishedFuture<T>(future);
this->decWeakRefCount();
};
auto onCanceled = [=]() {
this->cancel();
this->decWeakRefCount();
};
watch(future,
this,
nullptr,
onFinished,
onCanceled,
[](int){},
[](int,int){}
);
auto pushCancel = [=]() {
auto tmpFuture = future;
tmpFuture.cancel();
};
//Pushes cancel to child futures in the chain
watch(this->future(),
this,
nullptr,
[](){},
pushCancel,
[](int){},
[](int,int){}
);
track(future);
}
template <typename ANY>
void complete(QFuture<QFuture<ANY>> future) {
incWeakRefCount();
auto onFinished = [=]() {
complete(future.result());
this->decWeakRefCount();
};
auto onCanceled = [=]() {
this->cancel();
this->decWeakRefCount();
};
watch(future,
this,
nullptr,
onFinished,
onCanceled,
[](int){},
[](int,int){});
// It don't track for the first level of future
}
void cancel() {
if (isFinished()) {
return;
}
QFutureInterface<T>::reportCanceled();
QFutureInterface<T>::reportFinished();
}
template <typename Member>
void cancel(const QObject* sender, Member member) {
incWeakRefCount();
QObject::connect(sender, member,
this, [=]() {
this->cancel();
decWeakRefCount();
});
}
template <typename ANY>
void cancel(QFuture<ANY> future) {
incWeakRefCount();
auto onFinished = [=]() {
cancel();
decWeakRefCount();
};
auto onCanceled = [=]() {
decWeakRefCount();
};
watch(future,
this,
nullptr,
onFinished,
onCanceled,
[](int){},
[](int,int){}
);
}
void incWeakRefCount() {
mutex.lock();
refCount++;
mutex.unlock();
}
void decWeakRefCount() {
int count;
mutex.lock();
refCount--;
count = refCount;
mutex.unlock();
if (count <= 0) {
if (!isFinished()) {
cancel();
}
}
if (strongRefCount == 0 && isFinished()) {
//This prevents deletion this on a seperate thread
if(thread() != QThread::currentThread()) {
QMetaObject::invokeMethod(this, "deleteLater");
} else {
delete this;
}
}
}
void incStrongRef() {
mutex.lock();
strongRefCount++;
mutex.unlock();
}
void decStrongRef() {
mutex.lock();
strongRefCount--;
mutex.unlock();
}
/// Create a DeferredFugture instance and manage by a shared pointer
static QSharedPointer<DeferredFuture<T> > create() {
auto deleter = [](DeferredFuture<T> *object) {
object->decStrongRef();
object->decWeakRefCount();
};
QSharedPointer<DeferredFuture<T> > ptr(new DeferredFuture<T>(), deleter);
ptr->incStrongRef();
return ptr;
}
template <typename R>
void reportResult(R& value, int index = -1) {
QFutureInterface<T>::reportResult(value, index);
}
void reportResult(Value<void> &value) {
Q_UNUSED(value);
}
template <typename R>
void reportResult(QList<R>& value) {
for (int i = 0 ; i < value.size();i++) {
QFutureInterface<T>::reportResult(value[i], i);
}
}
template <typename R>
void reportResult(Value<R>& value) {
QFutureInterface<T>::reportResult(value.value);
}
void setParentProgressValue(int value) {
mutex.lock();
parentProgress.value = value;
updateProgressValue();
mutex.unlock();
}
void setParentProgressRange(int min, int max) {
mutex.lock();
parentProgress.min = min;
parentProgress.max = max;
updateProgressRanges();
mutex.unlock();
}
protected:
DeferredFuture(QObject* parent = nullptr): QObject(parent),
QFutureInterface<T>(QFutureInterface<T>::Running),
refCount(1),
strongRefCount(0) {
moveToThread(QCoreApplication::instance()->thread());
}
QMutex mutex;
private:
// A reference count system. If it is dropped to zero, it will cancel this object
int refCount;
// Unless it is zero, this object will not be destroyed.
int strongRefCount;
class Progress {
public:
int range() { return max - min; }
int value = 0;
int min = 0;
int max = 0;
};
Progress parentProgress;
Progress watchProgress;
void setWatchProgressValue(int value) {
mutex.lock();
watchProgress.value = value;
updateProgressValue();
mutex.unlock();
}
void setWatchProgressRange(int min, int max) {
mutex.lock();
watchProgress.min = min;
watchProgress.max = max;
updateProgressRanges();
mutex.unlock();
}
void updateProgressRanges() {
int newMax = parentProgress.range() + watchProgress.range();
if(QFutureInterface<T>::progressMaximum() != newMax) {
QFutureInterface<T>::setProgressRange(0, newMax);
}
}
void updateProgressValue() {
int newProgress = parentProgress.value + watchProgress.value;
if(QFutureInterface<T>::progressValue() != newProgress) {
QFutureInterface<T>::setProgressValue(newProgress);
}
}
/// The future is already finished. It will take effect immediately
template <typename ANY>
typename std::enable_if<!std::is_same<ANY,void>::value, void>::type
completeByFinishedFuture(QFuture<T> future) {
if (future.resultCount() > 1) {
complete(future.results());
} else if (future.resultCount() == 1) {
complete(future.result());
} else {
complete();
}
}
template <typename ANY>
typename std::enable_if<std::is_same<ANY,void>::value, void>::type
completeByFinishedFuture(QFuture<T> future) {
Q_UNUSED(future);
complete();
}
protected:
};
class CombinedFuture: public DeferredFuture<void> {
public:
CombinedFuture(bool settleAllModeArg = false) : DeferredFuture<void>(),
settledCount(0),
count(0),
anyCanceled(false),
settleAllMode(settleAllModeArg)
{
//Cancel all sub futures if this future is cancelled
Private::watch(
future(),
this,
this,
[](){},
[this](){
mutex.lock();
for(FutureInfo* info : futures) {
if(info->childFuture.isRunning() && !info->childFuture.isFinished()) {
info->childFuture.cancel();
}
}
mutex.unlock();
},
[](int){},
[](int, int){}
);
}
~CombinedFuture() {
for(auto progress : futures) {
delete progress;
}
}
template <typename T>
void addFuture(const QFuture<T> future) {
if (isFinished()) {
return;
}
incWeakRefCount();
mutex.lock();
int index = count++;
auto info = new FutureInfo(future);
futures.append(info);
Q_ASSERT(index == futures.size() - 1);
if(future.progressMaximum() > 0) {
info->max = future.progressMaximum();
}
info->value = future.progressValue();
auto progressFunc = [=](int progressValue) {
mutex.lock();
info->value = progressValue;
updateProgress();
mutex.unlock();
};
auto progressRangeFunc = [=](int min, int max) {
Q_UNUSED(min);
mutex.lock();
if(max > 0) {
info->max = max;
}
updateProgressRange();
mutex.unlock();
};
QFutureInterface<void>::setProgressRange(0, progressMaximum() + info->max);
mutex.unlock();
Private::watch(future, this, 0,
[=]() {
completeFutureAt(index);
decWeakRefCount();
},[=]() {
cancelFutureAt(index);
decWeakRefCount();
},
progressFunc,
progressRangeFunc
);
}
static QSharedPointer<CombinedFuture> create(bool settleAllMode) {
auto deleter = [](CombinedFuture *object) {
// Regardless of the no. of instance of QSharedPointer<CombinedFuture>,
// it only increase the reference by one.
object->decStrongRef();
object->decWeakRefCount();
};
QSharedPointer<CombinedFuture> ptr(new CombinedFuture(settleAllMode), deleter);
ptr->incStrongRef();
return ptr;
}
private:
class FutureInfo {
public:
FutureInfo() = default;
FutureInfo(QFuture<void> childFuture) :
childFuture(childFuture)
{}
int max = 1;
int value = 0;
QFuture<void> childFuture;
};
int settledCount;
int count;
bool anyCanceled;
bool settleAllMode;
QVector<FutureInfo*> futures;
void completeFutureAt(int index) {
Q_UNUSED(index);
mutex.lock();
settledCount++;
finishProgress(index);
mutex.unlock();
checkFulfilled();
}
void cancelFutureAt(int index) {
Q_UNUSED(index);
mutex.lock();
settledCount++;
anyCanceled = true;
finishProgress(index);
mutex.unlock();
checkFulfilled();
}
void checkFulfilled() {
if (isFinished()) {
return;
}
if (anyCanceled && !settleAllMode) {
cancel();
return;
}
if (settledCount == count) {
if (anyCanceled) {
cancel();
} else {
complete();
}
}
}