-
Notifications
You must be signed in to change notification settings - Fork 40
/
task.hpp
1489 lines (1265 loc) · 36.7 KB
/
task.hpp
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
/*
* copyright: 2014-2020
* name : Francis Banyikwa
* email: [email protected]
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef __TASK_H_INCLUDED__
#define __TASK_H_INCLUDED__
#include <type_traits>
#include <vector>
#include <utility>
#include <future>
#include <functional>
#include <QThread>
#include <QEventLoop>
#include <QMutex>
#include <QProcess>
#include <QVariant>
/*
*
* Examples on how to use the library are at the end of this file.
*
*/
/*
* This library wraps a function into a future where the result of the function
* can be retrieved through the future's below public methods:
*
* 1. .get(). This method runs the wrapped function on the current thread
* and could block the thread and hang GUI.
*
* 2. .then(). This method does three things:
*
* 1. Registers a method to be called when a wrapped function finish running.
*
* 2. Runs the wrapped function on a background thread.
*
* 3. Runs the registered method on the current thread when the wrapped function finish
* running.
*
* 3. .await(). This method does three things:
*
* 1. Suspends the current thread at a point where this method is called.
*
* 2. Creates a background thread and then runs the wrapped function in the background
* thread.
*
* 3. Unsuspends the current thread when the wrapped function finish and let the
* current thread continue normally.
*
* The suspension at step 1 is done without blocking the thread and hence the suspension
* can be done in the GUI thread and the GUI will remain responsive.
*
* 4. .queue(). This method runs tasks in a future sequentially and a passed in function will be called
* when all tasks finish running. This method behaves like .then( [](){} ) if the future is
* managing only one task.
*
* 5. .cancel(). This method can be used to cancel a future. It is important to know
* that this method does not terminate a running thread that is powering a future, it just
* releases memory used by a future and this method should be used if a future is to be discarded
* after it it is acquired but never used.
*
* 6. .all_threads(). This method returns a vector of QThreads that are powering futures.
* The vector will contain a single entry if this future powers its own task. If this future
* manages other futures,then the returned vector will contain QThread pointers that are in
* the same order as tasks/futures passed to Task::run().
*
* 7. .start(). This method is to be used if a future is to be run without caring about
* its result. Use this method if you want a future to run but dont want to use any of the above mentioned
* methods.
*
* 8. .manages_multiple_futures(). This method can be used to check if a future powers
* its own task or manages other futures.
*
*
* The future is of type "Task::future<T>&" and "std::reference_wrapper"[1]
* class can be used if they are to be managed in a container that can not handle references.
*
* [1] http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
*/
namespace Task
{
template< typename T >
class future;
namespace detail
{
template< typename T >
void add_void( Task::future< T >&,Task::future< T >&,std::function< T() >&& ) ;
template< typename T >
void add( Task::future< T >&,Task::future< T >&,std::function< void( T ) >&& ) ;
template<typename Function>
class functionWrapper
{
public:
template< typename ... Args >
auto operator()( Args&& ... args ) const
{
return (*m_function)( std::forward<Args>( args ) ... ) ;
}
functionWrapper( Function function ) :
m_function( std::make_shared<Function>( Function( std::move( function ) ) ) )
{
}
private:
std::shared_ptr<Function> m_function ;
};
template<typename Function>
functionWrapper<Function> function( Function function )
{
return functionWrapper<Function>( std::move( function ) ) ;
}
#if __cplusplus >= 201703L
template<typename Function,typename ... Args>
using result_of = std::invoke_result_t<Function,Args ...> ;
#else
template<typename Function,typename ... Args>
using result_of = std::result_of_t<Function(Args ...)> ;
#endif
template<typename Function>
using copyable = std::enable_if_t<std::is_copy_constructible<Function>::value,int> ;
template<typename Function>
using not_copyable = std::enable_if_t<!std::is_copy_constructible<Function>::value,int> ;
template<typename ReturnType,typename Function,typename ... Args>
using has_same_return_type = std::enable_if_t<std::is_same<result_of<Function,Args...>,ReturnType>::value,int> ;
template<typename Function,typename ... Args>
using has_void_return_type = has_same_return_type<void,Function,Args...> ;
template<typename Function,typename ... Args>
using has_bool_return_type = has_same_return_type<bool,Function,Args...> ;
template<typename Function,typename ... Args>
using has_non_void_return_type = std::enable_if_t<!std::is_void<result_of<Function,Args...>>::value,int> ;
template<typename Function,typename ... Args>
using has_argument = has_same_return_type<result_of<Function,Args...>,Function,Args...> ;
template<typename Function>
using has_no_argument = has_same_return_type<result_of<Function>,Function> ;
template<typename Function,typename ... Args>
using returns_void = has_void_return_type<Function,Args...> ;
template<typename Function,typename ... Args>
using returns_value = has_non_void_return_type<Function,Args...> ;
}
template< typename T >
struct pair{
pair( std::function< T() > first,std::function< void( T ) > second ) :
value( std::make_pair( std::move( first ),std::move( second ) ) )
{
}
std::pair< std::function< T() >,std::function< void( T ) > > value ;
};
template<>
struct pair<void>{
pair( std::function< void() > first,std::function< void() > second ) :
value( std::make_pair( std::move( first ),std::move( second ) ) )
{
}
std::pair< std::function< void() >,std::function< void() > > value ;
};
template< typename E,
typename F,
Task::detail::not_copyable<E> = 0,
Task::detail::not_copyable<F> = 0 >
pair<Task::detail::result_of<E>> make_pair( E e,F f )
{
using type = Task::detail::result_of<E> ;
return pair<type>( Task::detail::function( std::move( e ) ),Task::detail::function( std::move( f ) ) ) ;
}
template< typename E,
typename F,
Task::detail::copyable<E> = 0,
Task::detail::copyable<F> = 0 >
pair<Task::detail::result_of<E>> make_pair( E e,F f )
{
return pair<Task::detail::result_of<E>>( std::move( e ),std::move( f ) ) ;
}
template< typename E,
typename F,
Task::detail::not_copyable<E> = 0,
Task::detail::copyable<F> = 0 >
pair<Task::detail::result_of<E>> make_pair( E e,F f )
{
using type = Task::detail::result_of<E> ;
return pair<type>( Task::detail::function( std::move( e ) ),std::move( f ) ) ;
}
template< typename E,
typename F,
Task::detail::copyable<E> = 0,
Task::detail::not_copyable<F> = 0 >
pair<Task::detail::result_of<E>> make_pair( E e,F f )
{
using type = Task::detail::result_of<E> ;
return pair<type>( std::move( e ),Task::detail::function( std::move( f ) ) ) ;
}
template< typename T >
class future : private QObject
{
public:
/*
* Use this API if you care about the result
*/
//std::function< void( T ) >
template<typename Function,
Task::detail::has_argument<Function,T> = 0,
Task::detail::copyable<Function> = 0>
void then( Function function )
{
m_function = std::move( function ) ;
this->start() ;
}
template<typename Function,
Task::detail::has_argument<Function,T> = 0,
Task::detail::not_copyable<Function> = 0>
void then( Function function )
{
m_function = Task::detail::function( std::move( function ) ) ;
this->start() ;
}
/*
* Use this API if you DO NOT care about the result
*/
//std::function< void( void ) >
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::copyable<Function> = 0>
void then( Function function )
{
m_function_1 = std::move( function ) ;
this->start() ;
}
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::not_copyable<Function> = 0>
void then( Function function )
{
m_function_1 = Task::detail::function( std::move( function ) ) ;
this->start() ;
}
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::not_copyable<Function> = 0>
void queue( Function function )
{
if( this->manages_multiple_futures() ){
m_function_1 = Task::detail::function( std::move( function ) ) ;
this->_queue() ;
}else{
this->then( Task::detail::function( std::move( function ) ) ) ;
}
}
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::copyable<Function> = 0>
void queue( Function function )
{
if( this->manages_multiple_futures() ){
m_function_1 = std::move( function ) ;
this->_queue() ;
}else{
this->then( std::move( function ) ) ;
}
}
void queue()
{
if( this->manages_multiple_futures() ){
m_function_1 = [](){} ;
this->_queue() ;
}else{
this->then( [](){} ) ;
}
}
/*
* Below two API just exposes existing functionality using more standard names
*/
template<typename Function>
void when_all( Function function )
{
this->then( std::move( function ) ) ;
}
template<typename Function>
void when_seq( Function function )
{
this->queue( std::move( function ) ) ;
}
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::copyable<Function> = 0>
void when_any( Function function )
{
if( this->manages_multiple_futures() ){
this->_when_any( std::move( function ) ) ;
}else{
this->then( std::move( function ) ) ;
}
}
template<typename Function,
Task::detail::has_no_argument<Function> = 0,
Task::detail::not_copyable<Function> = 0>
void when_any( Function function )
{
if( this->manages_multiple_futures() ){
this->_when_any( Task::detail::function( std::move( function ) ) ) ;
}else{
this->then( Task::detail::function( std::move( function ) ) ) ;
}
}
void when_all()
{
this->then( [](){} ) ;
}
void when_seq()
{
this->queue( [](){} ) ;
}
void when_any()
{
if( this->manages_multiple_futures() ){
this->_when_any( [](){} ) ;
}else{
this->then( [](){} ) ;
}
}
T get()
{
if( this->manages_multiple_futures() ){
for( auto& it : m_tasks ){
it.second( it.first->get() ) ;
}
this->deleteLater() ;
return T() ;
}else{
return m_get() ;
}
}
T await()
{
QEventLoop p ;
T q ;
m_function = [ & ]( T&& r ){ q = std::move( r ) ; p.exit() ; } ;
this->start() ;
p.exec() ;
return q ;
}
bool manages_multiple_futures()
{
return m_tasks.size() > 0 ;
}
const std::vector< QThread * >& all_threads()
{
return m_threads ;
}
QThread * first_thread()
{
return m_threads[ 0 ] ;
}
QThread * thread_at( std::vector< QThread * >::size_type s )
{
return m_threads[ s ] ;
}
void start()
{
if( this->manages_multiple_futures() ){
this->_start() ;
}else{
m_start() ;
}
}
void cancel()
{
if( this->manages_multiple_futures() ){
for( auto& it : m_tasks ){
it.first->cancel() ;
}
this->deleteLater() ;
}else{
m_cancel() ;
}
}
future() = default ;
future( const future& ) = delete ;
future( future&& ) = delete ;
future& operator=( const future& ) = delete ;
future& operator=( future&& ) = delete ;
future( QThread * e,
std::function< void() >&& start,
std::function< void() >&& cancel,
std::function< T() >&& get ) :
m_thread( e ),
m_start ( std::move( start ) ),
m_cancel( std::move( cancel ) ),
m_get ( std::move( get ) )
{
if( m_thread ){
m_threads.push_back( m_thread ) ;
}else{
/*
* This object was created by "_private_future< T >()" class.
* It has no QThread of its own because it only manages other futures.
*
*/
}
}
void run( T&& r )
{
if( m_function_1 != nullptr ){
m_function_1() ;
}else if( m_function != nullptr ){
m_function( std::move( r ) ) ;
}
}
template< typename E >
friend void Task::detail::add( Task::future< E >&,
Task::future< E >&,
std::function< void( E ) >&& ) ;
private:
void _when_any( std::function< void() > function )
{
m_when_any_function = std::move( function ) ;
for( auto& it : m_tasks ){
it.first->then( [ & ]( T&& e ){
QMutexLocker m( &m_mutex ) ;
m_counter++ ;
if( m_task_not_run ){
m_task_not_run = false ;
m.unlock() ;
it.second( std::forward<T>( e ) ) ;
m_when_any_function() ;
}else{
m.unlock() ;
it.second( std::forward<T>( e ) ) ;
}
if( m_counter == m_tasks.size() ){
this->deleteLater() ;
}
} ) ;
}
}
void _queue()
{
m_tasks[ m_counter ].first->then( [ this ]( T&& e ){
m_tasks[ m_counter ].second( std::forward<T>( e ) ) ;
m_counter++ ;
if( m_counter == m_tasks.size() ){
m_function_1() ;
this->deleteLater() ;
}else{
this->_queue() ;
}
} ) ;
}
void _start()
{
for( auto& it : m_tasks ){
it.first->then( [ & ]( T&& e ){
QMutexLocker m( &m_mutex ) ;
Q_UNUSED( m ) ;
m_counter++ ;
it.second( std::forward<T>( e ) ) ;
if( m_counter == m_tasks.size() ){
if( m_function_1 != nullptr ){
m_function_1() ;
}else if( m_function != nullptr ){
m_function( T() ) ;
}
this->deleteLater() ;
}
} ) ;
}
}
QThread * m_thread = nullptr ;
std::function< void( T ) > m_function = nullptr ;
std::function< void() > m_function_1 = nullptr ;
std::function< void() > m_start = [](){} ;
std::function< void() > m_cancel = [](){} ;
std::function< T() > m_get = [](){ return T() ; } ;
std::function< void() > m_when_any_function ;
QMutex m_mutex ;
std::vector< std::pair< Task::future< T > *,std::function< void( T ) > > > m_tasks ;
std::vector< QThread * > m_threads ;
decltype( m_tasks.size() ) m_counter = 0 ;
bool m_task_not_run = true ;
};
template<>
class future< void > : private QObject
{
public:
template<typename Function,Task::detail::copyable<Function> = 0>
void then( Function function )
{
m_function = std::move( function ) ;
this->start() ;
}
template<typename Function,Task::detail::not_copyable<Function> = 0>
void then( Function function )
{
m_function = Task::detail::function( std::move( function ) ) ;
this->start() ;
}
template<typename Function,Task::detail::copyable<Function> = 0>
void queue( Function function )
{
if( this->manages_multiple_futures() ){
m_function = std::move( function ) ;
this->_queue() ;
}else{
this->then( std::move( function ) ) ;
}
}
template<typename Function,Task::detail::not_copyable<Function> = 0>
void queue( Function function )
{
if( this->manages_multiple_futures() ){
m_function = Task::detail::function( std::move( function ) ) ;
this->_queue() ;
}else{
this->then( Task::detail::function( std::move( function ) ) ) ;
}
}
void queue()
{
if( this->manages_multiple_futures() ){
m_function = [](){} ;
this->_queue() ;
}else{
this->then( [](){} ) ;
}
}
void when_any()
{
if( this->manages_multiple_futures() ){
this->_when_any( [](){} ) ;
}else{
this->then( [](){} ) ;
}
}
template<typename Function,Task::detail::copyable<Function> = 0>
void when_any( Function function )
{
if( this->manages_multiple_futures() ){
this->_when_any( std::move( function ) ) ;
}else{
this->then( std::move( function ) ) ;
}
}
template<typename Function,Task::detail::not_copyable<Function> = 0>
void when_any( Function function )
{
if( this->manages_multiple_futures() ){
this->_when_any( Task::detail::function( std::move( function ) ) ) ;
}else{
this->then( Task::detail::function( std::move( function ) ) ) ;
}
}
/*
* Below two API just exposes existing functionality using more standard names
*/
template<typename Function>
void when_all( Function function )
{
this->then( std::move( function ) ) ;
}
template<typename Function>
void when_seq( Function function )
{
this->queue( std::move( function ) ) ;
}
void when_all()
{
this->then( [](){} ) ;
}
void when_seq()
{
this->queue() ;
}
void get()
{
if( this->manages_multiple_futures() ){
for( auto& it : m_tasks ){
it.first->get() ;
it.second() ;
}
this->deleteLater() ;
}else{
m_get() ;
}
}
void await()
{
QEventLoop p ;
m_function = [ & ](){ p.exit() ; } ;
this->start() ;
p.exec() ;
}
bool manages_multiple_futures()
{
return m_tasks.size() > 0 ;
}
const std::vector< QThread * >& all_threads()
{
return m_threads ;
}
QThread * first_thread()
{
return m_threads[ 0 ] ;
}
QThread * thread_at( std::vector< QThread * >::size_type s )
{
return m_threads[ s ] ;
}
void start()
{
if( this->manages_multiple_futures() ){
this->_start() ;
}else{
m_start() ;
}
}
void cancel()
{
if( this->manages_multiple_futures() ){
for( auto& it : m_tasks ){
it.first->cancel() ;
}
this->deleteLater() ;
}else{
m_cancel() ;
}
}
future() = default ;
future( const future& ) = delete ;
future( future&& ) = delete ;
future& operator=( const future& ) = delete ;
future& operator=( future&& ) = delete ;
future( QThread * e ,
std::function< void() >&& start,
std::function< void() >&& cancel,
std::function< void() >&& get ) :
m_thread( e ),
m_start ( std::move( start ) ),
m_cancel( std::move( cancel ) ),
m_get ( std::move( get ) )
{
if( m_thread ){
m_threads.push_back( m_thread ) ;
}else{
/*
* This object was created by "_private_future< T >()" class.
* It has no QThread of its own because it only manages other futures.
*
*/
}
}
template< typename T >
friend void Task::detail::add_void( Task::future< T >&,
Task::future< T >&,
std::function< T() >&& ) ;
void run()
{
m_function() ;
}
private:
void _when_any( std::function< void() > function )
{
m_when_any_function = std::move( function ) ;
for( auto& it : m_tasks ){
it.first->then( [ & ](){
QMutexLocker m( &m_mutex ) ;
m_counter++ ;
if( m_task_not_run ){
m_task_not_run = false ;
m.unlock() ;
it.second() ;
m_when_any_function() ;
}else{
m.unlock() ;
it.second() ;
}
if( m_counter == m_tasks.size() ){
this->deleteLater() ;
}
} ) ;
}
}
void _queue()
{
m_tasks[ m_counter ].first->then( [ this ](){
m_tasks[ m_counter ].second() ;
m_counter++ ;
if( m_counter == m_tasks.size() ){
m_function() ;
this->deleteLater() ;
}else{
this->_queue() ;
}
} ) ;
}
void _start()
{
for( auto& it : m_tasks ){
it.first->then( [ & ](){
QMutexLocker m( &m_mutex ) ;
Q_UNUSED( m ) ;
m_counter++ ;
it.second() ;
if( m_counter == m_tasks.size() ){
m_function() ;
this->deleteLater() ;
}
} ) ;
}
}
QThread * m_thread = nullptr ;
std::function< void() > m_function = [](){} ;
std::function< void() > m_start = [](){} ;
std::function< void() > m_cancel = [](){} ;
std::function< void() > m_get = [](){} ;
std::function< void() > m_when_any_function ;
QMutex m_mutex ;
std::vector< std::pair< Task::future< void > *,std::function< void() > > > m_tasks ;
std::vector< QThread * > m_threads ;
decltype( m_tasks.size() ) m_counter = 0 ;
bool m_task_not_run = true ;
};
namespace detail
{
/*
* -------------------------Start of internal helper functions-------------------------
*/
template< typename Type,typename Function >
class ThreadHelper : public QThread
{
public:
ThreadHelper( Function function ) :
m_function( std::move( function ) ),
m_future( this,
[ this ](){ this->start() ; },
[ this ](){ this->deleteLater() ; },
[ this ](){ this->deleteLater() ; return m_function() ; } )
{
connect( this,&QThread::finished,this,&QThread::deleteLater ) ;
}
Task::future<Type>& Future()
{
return m_future ;
}
private:
~ThreadHelper()
{
m_future.run( std::move( m_result ) ) ;
}
void run()
{
m_result = m_function() ;
}
Function m_function ;
Task::future<Type> m_future ;
Type m_result ;
};
template< typename Function>
class ThreadHelperVoid : public QThread
{
public:
ThreadHelperVoid( Function function ) :
m_function( std::move( function ) ),
m_future( this,
[ this ](){ this->start() ; },
[ this ](){ this->deleteLater() ; },
[ this ](){ m_function() ; this->deleteLater() ; } )
{
connect( this,&QThread::finished,this,&QThread::deleteLater ) ;
}
Task::future< void >& Future()
{
return m_future ;
}
private:
~ThreadHelperVoid()
{
m_future.run() ;
}
void run()
{
m_function() ;
}
Function m_function ;
Task::future< void > m_future ;
};
template<typename Fn,Task::detail::returns_value<Fn> = 0>
Task::future<Task::detail::result_of<Fn>>& run( Fn function )
{
using t = Task::detail::result_of<Fn> ;
return ( new ThreadHelper<t,Fn>( std::move( function ) ) )->Future() ;
}
template<typename Fn,Task::detail::returns_void<Fn> = 0>
Task::future<Task::detail::result_of<Fn>>& run( Fn function )
{
return ( new ThreadHelperVoid<Fn>( std::move( function ) ) )->Future() ;
}
template< typename T >
void add( Task::future< T >& a,Task::future< T >& b,std::function< void( T ) >&& c )
{
a.m_tasks.emplace_back( std::addressof( b ),std::move( c ) ) ;
a.m_threads.push_back( b.m_thread ) ;
}
template< typename T >
void add_void( Task::future< T >& a,Task::future< T >& b,std::function< T() >&& c )
{
a.m_tasks.emplace_back( std::addressof( b ),std::move( c ) ) ;
a.m_threads.push_back( b.m_thread ) ;
}
template< typename T >
void add_task( Task::future< T >& f )
{
Q_UNUSED( f )
}
template< typename T >
void add_future( Task::future< T >& f )
{
Q_UNUSED( f )
}
template< typename T >
void add_pair( Task::future< T >& f )
{
Q_UNUSED( f )
}
template< typename T >
void add_pair_void( Task::future< T >& f )
{
Q_UNUSED( f )
}
template< typename ... T,
typename Function,
Task::detail::not_copyable<Function> = 0 >
void add_task( Task::future< void >& f,Function e,T&& ... t ) ;
template< typename ... T,
typename Function,
Task::detail::copyable<Function> = 0 >
void add_task( Task::future< void >& f,Function e,T&& ... t )
{
add_void( f,Task::detail::run( std::function< void() >( std::move( e ) ) ),
std::function< void() >( [](){} ) ) ;
add_task( f,std::forward<T>( t ) ... ) ;
}
template< typename ... T,