| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QTCONCURRENT_STOREDFUNCTIONCALL_H |
| 6 | #define QTCONCURRENT_STOREDFUNCTIONCALL_H |
| 7 | |
| 8 | #include <QtConcurrent/qtconcurrent_global.h> |
| 9 | |
| 10 | #ifndef QT_NO_CONCURRENT |
| 11 | #include <QtConcurrent/qtconcurrentrunbase.h> |
| 12 | #include <QtCore/qpromise.h> |
| 13 | |
| 14 | #include <type_traits> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | #ifndef Q_QDOC |
| 19 | |
| 20 | namespace QtConcurrent { |
| 21 | |
| 22 | template<typename...> |
| 23 | struct NonMemberFunctionResolver; |
| 24 | |
| 25 | template <class Function, class PromiseType, class... Args> |
| 26 | struct NonMemberFunctionResolver<Function, PromiseType, Args...> |
| 27 | { |
| 28 | using Type = std::tuple<std::decay_t<Function>, QPromise<PromiseType> &, std::decay_t<Args>...>; |
| 29 | static_assert(std::is_invocable_v<std::decay_t<Function>, QPromise<PromiseType> &, std::decay_t<Args>...>, |
| 30 | "It's not possible to invoke the function with passed arguments." ); |
| 31 | static_assert(std::is_void_v<std::invoke_result_t<std::decay_t<Function>, QPromise<PromiseType> &, std::decay_t<Args>...>>, |
| 32 | "The function must return void type." ); |
| 33 | |
| 34 | static constexpr void invoke(std::decay_t<Function> function, QPromise<PromiseType> &promise, |
| 35 | std::decay_t<Args>... args) |
| 36 | { |
| 37 | std::invoke(function, promise, args...); |
| 38 | } |
| 39 | static Type initData(Function &&f, QPromise<PromiseType> &promise, Args &&...args) |
| 40 | { |
| 41 | return Type { std::forward<Function>(f), std::ref(promise), std::forward<Args>(args)... }; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | template<typename...> |
| 46 | struct MemberFunctionResolver; |
| 47 | |
| 48 | template <typename Function, typename PromiseType, typename Arg, typename ... Args> |
| 49 | struct MemberFunctionResolver<Function, PromiseType, Arg, Args...> |
| 50 | { |
| 51 | using Type = std::tuple<std::decay_t<Function>, std::decay_t<Arg>, QPromise<PromiseType> &, std::decay_t<Args>...>; |
| 52 | static_assert(std::is_invocable_v<std::decay_t<Function>, std::decay_t<Arg>, QPromise<PromiseType> &, std::decay_t<Args>...>, |
| 53 | "It's not possible to invoke the function with passed arguments." ); |
| 54 | static_assert(std::is_void_v<std::invoke_result_t<std::decay_t<Function>, std::decay_t<Arg>, QPromise<PromiseType> &, std::decay_t<Args>...>>, |
| 55 | "The function must return void type." ); |
| 56 | |
| 57 | static constexpr void invoke(std::decay_t<Function> function, std::decay_t<Arg> object, |
| 58 | QPromise<PromiseType> &promise, std::decay_t<Args>... args) |
| 59 | { |
| 60 | std::invoke(function, object, promise, args...); |
| 61 | } |
| 62 | static Type initData(Function &&f, QPromise<PromiseType> &promise, Arg &&fa, Args &&...args) |
| 63 | { |
| 64 | return Type { std::forward<Function>(f), std::forward<Arg>(fa), std::ref(promise), std::forward<Args>(args)... }; |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | template <class IsMember, class Function, class PromiseType, class... Args> |
| 69 | struct FunctionResolverHelper; |
| 70 | |
| 71 | template <class Function, class PromiseType, class... Args> |
| 72 | struct FunctionResolverHelper<std::false_type, Function, PromiseType, Args...> |
| 73 | : public NonMemberFunctionResolver<Function, PromiseType, Args...> |
| 74 | { |
| 75 | }; |
| 76 | |
| 77 | template <class Function, class PromiseType, class... Args> |
| 78 | struct FunctionResolverHelper<std::true_type, Function, PromiseType, Args...> |
| 79 | : public MemberFunctionResolver<Function, PromiseType, Args...> |
| 80 | { |
| 81 | }; |
| 82 | |
| 83 | template <class Function, class PromiseType, class... Args> |
| 84 | struct FunctionResolver |
| 85 | : public FunctionResolverHelper<typename std::is_member_function_pointer< |
| 86 | std::decay_t<Function>>::type, Function, PromiseType, Args...> |
| 87 | { |
| 88 | }; |
| 89 | |
| 90 | template <class Function, class ...Args> |
| 91 | struct InvokeResult |
| 92 | { |
| 93 | static_assert(std::is_invocable_v<std::decay_t<Function>, std::decay_t<Args>...>, |
| 94 | "It's not possible to invoke the function with passed arguments." ); |
| 95 | |
| 96 | using Type = std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>; |
| 97 | }; |
| 98 | |
| 99 | template <class Function, class ...Args> |
| 100 | using InvokeResultType = typename InvokeResult<Function, Args...>::Type; |
| 101 | |
| 102 | template <class ...Types> |
| 103 | using DecayedTuple = std::tuple<std::decay_t<Types>...>; |
| 104 | |
| 105 | template <class Function, class ...Args> |
| 106 | struct StoredFunctionCall : public RunFunctionTaskBase<InvokeResultType<Function, Args...>> |
| 107 | { |
| 108 | StoredFunctionCall(DecayedTuple<Function, Args...> &&_data) |
| 109 | : data(std::move(_data)) |
| 110 | {} |
| 111 | |
| 112 | protected: |
| 113 | void runFunctor() override |
| 114 | { |
| 115 | constexpr auto invoke = [] (std::decay_t<Function> function, |
| 116 | std::decay_t<Args>... args) -> auto { |
| 117 | return std::invoke(function, args...); |
| 118 | }; |
| 119 | |
| 120 | if constexpr (std::is_void_v<InvokeResultType<Function, Args...>>) { |
| 121 | std::apply(invoke, std::move(data)); |
| 122 | } else { |
| 123 | auto result = std::apply(invoke, std::move(data)); |
| 124 | |
| 125 | using T = InvokeResultType<Function, Args...>; |
| 126 | if constexpr (std::is_move_constructible_v<T>) |
| 127 | this->promise.reportAndMoveResult(std::move(result)); |
| 128 | else if constexpr (std::is_copy_constructible_v<T>) |
| 129 | this->promise.reportResult(result); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | DecayedTuple<Function, Args...> data; |
| 135 | }; |
| 136 | |
| 137 | template <class Function, class PromiseType, class ...Args> |
| 138 | struct StoredFunctionCallWithPromise : public RunFunctionTaskBase<PromiseType> |
| 139 | { |
| 140 | using Resolver = FunctionResolver<Function, PromiseType, Args...>; |
| 141 | using DataType = typename Resolver::Type; |
| 142 | StoredFunctionCallWithPromise(Function &&f, Args &&...args) |
| 143 | : prom(this->promise), |
| 144 | data(std::move(Resolver::initData(std::forward<Function>(f), std::ref(prom), |
| 145 | std::forward<Args>(args)...))) |
| 146 | {} |
| 147 | |
| 148 | StoredFunctionCallWithPromise(DecayedTuple<Function, Args...> &&_data) |
| 149 | : StoredFunctionCallWithPromise(std::move(_data), |
| 150 | std::index_sequence_for<std::decay_t<Function>, std::decay_t<Args>...>()) |
| 151 | {} |
| 152 | |
| 153 | protected: |
| 154 | void runFunctor() override |
| 155 | { |
| 156 | std::apply(Resolver::invoke, std::move(data)); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | // helper to pack back the tuple into parameter pack |
| 161 | template<std::size_t... Is> |
| 162 | StoredFunctionCallWithPromise(DecayedTuple<Function, Args...> &&_data, |
| 163 | std::index_sequence<Is...>) |
| 164 | : StoredFunctionCallWithPromise(std::move(std::get<Is>(_data))...) |
| 165 | {} |
| 166 | |
| 167 | QPromise<PromiseType> prom; |
| 168 | DataType data; |
| 169 | }; |
| 170 | |
| 171 | template<typename...> |
| 172 | struct NonPromiseTaskResolver; |
| 173 | |
| 174 | template <typename Function, typename ... Args> |
| 175 | struct NonPromiseTaskResolver<Function, Args...> |
| 176 | { |
| 177 | using TaskWithArgs = DecayedTuple<Function, Args...>; |
| 178 | static auto run(TaskWithArgs &&args, const TaskStartParameters &startParameters) { |
| 179 | return (new StoredFunctionCall<Function, Args...>(std::move(args))) |
| 180 | ->start(startParameters); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | template<typename...> |
| 185 | struct PromiseTaskResolver; |
| 186 | |
| 187 | template <typename Function, typename ... Args> |
| 188 | struct PromiseTaskResolver<Function, Args...> |
| 189 | { |
| 190 | static_assert(QtPrivate::ArgResolver<Function>::IsPromise::value, |
| 191 | "The first argument of passed callable object isn't a QPromise<T> & type. " |
| 192 | "Did you intend to pass a callable which takes a QPromise<T> & type as a first argument? " |
| 193 | "Otherwise it's not possible to invoke the function with passed arguments." ); |
| 194 | using TaskWithArgs = DecayedTuple<Function, Args...>; |
| 195 | static auto run(TaskWithArgs &&args, const TaskStartParameters &startParameters) { |
| 196 | using PromiseType = typename QtPrivate::ArgResolver<Function>::PromiseType; |
| 197 | return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>(std::move(args))) |
| 198 | ->start(startParameters); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | template <class IsDirectlyInvocable, class Function, class... Args> |
| 203 | struct TaskResolverHelper; |
| 204 | |
| 205 | template <class Function, class... Args> |
| 206 | struct TaskResolverHelper<std::true_type, Function, Args...> |
| 207 | : public NonPromiseTaskResolver<Function, Args...> |
| 208 | { |
| 209 | }; |
| 210 | |
| 211 | template <class Function, class... Args> |
| 212 | struct TaskResolverHelper<std::false_type, Function, Args...> |
| 213 | : public PromiseTaskResolver<Function, Args...> |
| 214 | { |
| 215 | }; |
| 216 | |
| 217 | template <class Function, class... Args> |
| 218 | struct TaskResolver : public TaskResolverHelper<typename std::is_invocable<std::decay_t<Function>, |
| 219 | std::decay_t<Args>...>::type, Function, Args...> |
| 220 | { |
| 221 | }; |
| 222 | |
| 223 | } //namespace QtConcurrent |
| 224 | |
| 225 | #endif // Q_QDOC |
| 226 | |
| 227 | QT_END_NAMESPACE |
| 228 | |
| 229 | #endif // QT_NO_CONCURRENT |
| 230 | |
| 231 | #endif |
| 232 | |