summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
index dfa9b670e7..66fa62f6b3 100644
--- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
@@ -72,3 +72,106 @@ i.toBack();
while (i.hasPrevious())
qDebug() << i.previous();
//! [2]
+
+//! [3]
+using NetworkReply = std::variant<QByteArray, QNetworkReply::NetworkError>;
+
+enum class IOError { FailedToRead, FailedToWrite };
+using IOResult = std::variant<QString, IOError>;
+//! [3]
+
+//! [4]
+QFuture<IOResult> future = QtConcurrent::run([url] {
+ ...
+ return NetworkReply(QNetworkReply::TimeoutError);
+}).then([](NetworkReply reply) {
+ if (auto error = std::get_if<QNetworkReply::NetworkError>(&reply))
+ return IOResult(IOError::FailedToRead);
+
+ auto data = std::get_if<QByteArray>(&reply);
+ // try to write *data and return IOError::FailedToWrite on failure
+ ...
+});
+
+auto result = future.result();
+if (auto filePath = std::get_if<QString>(&result)) {
+ // do something with *filePath
+else
+ // process the error
+//! [4]
+
+//! [5]
+QFuture<int> future = ...;
+ future.then([](QFuture<int> f) {
+ try {
+ ...
+ auto result = f.result();
+ ...
+ } catch (QException &e) {
+ // handle the exception
+ }
+ }).then(...);
+//! [5]
+
+//! [6]
+QFuture<int> parentFuture = ...;
+auto continuation = parentFuture.then([](int res1){ ... }).then([](int res2){ ... })...
+...
+// parentFuture throws an exception
+try {
+ auto result = continuation.result();
+} catch (QException &e) {
+ // handle the exception
+}
+//! [6]
+
+//! [7]
+QFuture<int> future = ...;
+auto resultFuture = future.then([](int res) {
+ ...
+ throw Error();
+ ...
+}).onFailed([](const Error &e) {
+ // Handle exceptions of type Error
+ ...
+ return -1;
+}).onFailed([] {
+ // Handle all other types of errors
+ ...
+ return -1;
+});
+
+auto result = resultFuture.result(); // result is -1
+//! [7]
+
+//! [8]
+QFuture<int> future = ...;
+future.then([](int res) {
+ ...
+ throw std::runtime_error("message");
+ ...
+}).onFailed([](const std::exception &e) {
+ // This handler will be invoked
+}).onFailed([](const std::runtime_error &e) {
+ // This handler won't be invoked, because of the handler above.
+});
+//! [8]
+
+//! [9]
+QFuture<int> future = ...;
+auto resultFuture = future.then([](int res) {
+ ...
+ throw Error("message");
+ ...
+}).onFailed([](const std::exception &e) {
+ // Won't be invoked
+}).onFailed([](const QException &e) {
+ // Won't be invoked
+});
+
+try {
+ auto result = resultFuture.result();
+} catch(...) {
+ // Handle the exception
+}
+//! [9]