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_TRANSFORM_H
0008 #define ASYNQT_BASE_TRANSFORM_H
0009 
0010 #include <QFuture>
0011 #include <QFutureWatcher>
0012 
0013 #include <memory>
0014 #include <type_traits>
0015 
0016 #include "../private/operations/transform_p.h"
0017 
0018 namespace AsynQt
0019 {
0020 /**
0021  * This method applies the specified transformation function to
0022  * the value stored in the given future. Since the value might not
0023  * yet be present, it returns a future that will contain the
0024  * transformed value as soon as the original future is finished.
0025  *
0026  * If the original future is canceled, the transformation function
0027  * will not be invoked, and the resulting future will also be canceled.
0028  *
0029  * Example:
0030  *
0031  * <code>
0032  *     QFuture<int> answer = meaningOfLife()
0033  *     // answer will eventually contain 42
0034  *
0035  *     QFuture<QString> text = transform(answer, toText)
0036  *     // text will eventually contain the result of toText(42)
0037  * </code>
0038  *
0039  * @arg future the future to transform
0040  * @arg transformation unary function to apply to the value in the future
0041  * @returns a future that will contain the transformed value
0042  */
0043 template<typename _In, typename _Transformation>
0044 QFuture<typename detail::TransformFutureInterface<_In, _Transformation>::result_type> transform(const QFuture<_In> &future, _Transformation &&transormation)
0045 {
0046     using namespace detail;
0047     return transform_impl(future, std::forward<_Transformation>(transormation));
0048 }
0049 
0050 namespace operators
0051 {
0052 template<typename _Transformation>
0053 detail::operators::TransformationModifier<_Transformation> transform(_Transformation &&transormation)
0054 {
0055     return detail::operators::TransformationModifier<_Transformation>(std::forward<_Transformation>(transormation));
0056 }
0057 
0058 } // namespace operators
0059 
0060 } // namespace AsynQt
0061 
0062 #endif // ASYNQT_BASE_TRANSFORM_H