QGIS API Documentation
4.3.0-Master (8c22baaae17)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
qgsfutureutils.h
Go to the documentation of this file.
1
/***************************************************************************
2
qgsfutureutils.h
3
--------------------------------------
4
Date : August 2026
5
Copyright : (C) 2026 by David Koňařík
6
email : dvdkon at konarici dot cz
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
16
#ifndef QGSFUTUREUTILS_H
17
#define QGSFUTUREUTILS_H
18
19
#include <tuple>
20
21
#include "
qgslogger.h
"
22
23
#include <QCoreApplication>
24
#include <QEventLoop>
25
#include <QFuture>
26
#include <QFutureWatcher>
27
#include <QThread>
28
29
#define SIP_NO_FILE
30
32
45
class
CORE_EXPORT QgsCombinedFutureHelper :
public
QObject
46
{
47
Q_OBJECT
48
public
:
49
QgsCombinedFutureHelper( QList<QFuture<void>> futures, QFuture<void> cancellationFuture, std::function<
void
()> allFinishedCallback );
50
51
private
:
52
QList<QFutureWatcher<void> *> mWatchers;
53
QFutureWatcher<void> mCancellationWatcher;
54
std::function<void()> mAllFinishedCallback;
55
bool
mFired =
false
;
56
57
private
slots:
58
void
onOneFinished();
59
void
onCanceled();
60
};
61
63
70
class
CORE_EXPORT
QgsFutureUtils
71
{
72
public
:
79
static
void
waitForFinished
( QFuture<void> future );
80
87
static
QFuture<void>
clone
( QFuture<void> base );
88
95
static
void
cancelChain
( QFuture<void> future );
96
103
template
<
typename
... Ts>
static
QFuture<std::tuple<Ts...>>
combine
( QFuture<Ts>... futures )
104
{
105
auto
combinedPromise =
new
QPromise<std::tuple<Ts...>>;
106
combinedPromise->start();
107
new
QgsCombinedFutureHelper( { futures... }, combinedPromise->future(), [combinedPromise, futures...]()
mutable
{
108
// The futures are now finished, but we need to call this so exceptions
109
// are thrown.
110
try
111
{
112
( futures.waitForFinished(), ... );
113
}
114
catch ( QException &e )
115
{
116
combinedPromise->setException( e );
117
delete
combinedPromise;
118
return
;
119
}
120
121
bool
allValid = (
true
&& ... && futures.isValid() );
122
if
( !allValid )
123
QgsDebugError
(
"Part of combined future not valid after finishing!"
);
124
bool
allHaveResult = (
true
&& ... && futures.isResultReadyAt( 0 ) );
125
if
( allValid && allHaveResult )
126
{
127
std::tuple<Ts...> result { ( futures.takeResult() )... };
128
combinedPromise->addResult( result );
129
}
130
combinedPromise->finish();
131
delete
combinedPromise;
132
} );
133
return
combinedPromise->future();
134
}
135
};
136
137
#endif
// QGSFUTUREUTILS_H
QgsFutureUtils
Provides utilities for handling QFutures.
Definition
qgsfutureutils.h:71
QgsFutureUtils::clone
static QFuture< void > clone(QFuture< void > base)
Returns a future that will finish at the same time base will.
Definition
qgsfutureutils.cpp:92
QgsFutureUtils::cancelChain
static void cancelChain(QFuture< void > future)
Runs future.cancelChain() if supported by Qt version, future.cancel() otherwise.
Definition
qgsfutureutils.cpp:103
QgsFutureUtils::combine
static QFuture< std::tuple< Ts... > > combine(QFuture< Ts >... futures)
Converts multiple futures into a single future that will finish when all its parts have finished.
Definition
qgsfutureutils.h:103
QgsFutureUtils::waitForFinished
static void waitForFinished(QFuture< void > future)
Waits for the future to become finished without blocking the main thread's event loop.
Definition
qgsfutureutils.cpp:61
qgslogger.h
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
Generated on
for QGIS API Documentation by
1.17.0