File indexing completed on 2024-04-28 16:53:07

0001 /*
0002  *   SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #ifndef ASYNQT_BASE_CONTINUEWITH_H
0008 #define ASYNQT_BASE_CONTINUEWITH_H
0009 
0010 #include <QFuture>
0011 #include <QFutureWatcher>
0012 
0013 #include <memory>
0014 #include <type_traits>
0015 
0016 #include "flatten.h"
0017 #include "transform.h"
0018 
0019 namespace AsynQt
0020 {
0021 /**
0022  * This method is similar to `transform`,
0023  * but it takes a transformation function (in this
0024  * case, called a continuation) that does not
0025  * return a normal value, but also a new future.
0026  * It returns the future that will be returned
0027  * by the continuation.
0028  *
0029  * It is equivalent to calling `flatten` on the result
0030  * of the `transform` function when a continuation
0031  * is passed to it.
0032  *
0033  * Example:
0034  *
0035  * <code>
0036  *     QFuture<QString> input = getUserInput();
0037  *     QFuture<Status> result = continueWith(input, [] (QString message) {
0038  *          return server.send(message);
0039  *     });
0040  * </code>
0041  *
0042  * @arg future the future to connect the continuation to
0043  * @arg continuation the continuation function
0044  * @returns the future that the continuation will return
0045  */
0046 template<typename _In, typename _Continuation>
0047 auto continueWith(const QFuture<_In> &future, _Continuation &&continuation) -> decltype(flatten(transform(future, std::forward<_Continuation>(continuation))))
0048 {
0049     return flatten(transform(future, std::forward<_Continuation>(continuation)));
0050 }
0051 
0052 namespace operators
0053 {
0054 template<typename _In, typename _Continuation>
0055 auto operator|(const QFuture<_In> &future, _Continuation &&continuation) -> decltype(continueWith(future, continuation))
0056 {
0057     return continueWith(future, continuation);
0058 }
0059 
0060 } // namespace operators
0061 
0062 } // namespace AsynQt
0063 
0064 #endif // ASYNQT_BASE_CONTINUEWITH_H