Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtconcurrentstoredfunctioncall.h
Go to the documentation of this file.
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
16QT_BEGIN_NAMESPACE
17
18#ifndef Q_QDOC
19
20namespace QtConcurrent {
21
22template<typename...>
24
25template <class Function, class PromiseType, class... Args>
26struct NonMemberFunctionResolver<Function, PromiseType, Args...>
27{
30 "It's not possible to invoke the function with passed arguments.");
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
45template<typename...>
47
48template <typename Function, typename PromiseType, typename Arg, typename ... Args>
49struct MemberFunctionResolver<Function, PromiseType, Arg, Args...>
50{
53 "It's not possible to invoke the function with passed arguments.");
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
68template <class IsMember, class Function, class PromiseType, class... Args>
70
71template <class Function, class PromiseType, class... Args>
76
77template <class Function, class PromiseType, class... Args>
82
83template <class Function, class PromiseType, class... Args>
89
90template <class Function, class ...Args>
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
97};
98
99template <class Function, class ...Args>
100using InvokeResultType = typename InvokeResult<Function, Args...>::Type;
101
102template <class ...Types>
104
105template <class Function, class ...Args>
107{
108 StoredFunctionCall(DecayedTuple<Function, Args...> &&_data)
109 : data(std::move(_data))
110 {}
111
112protected:
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...>>) {
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>)
128 else if constexpr (std::is_copy_constructible_v<T>)
130 }
131 }
132
133private:
135};
136
137template <class Function, class PromiseType, class ...Args>
139{
140 using Resolver = FunctionResolver<Function, PromiseType, Args...>;
141 using DataType = typename Resolver::Type;
142 StoredFunctionCallWithPromise(Function &&f, Args &&...args)
143 : prom(this->promise),
145 std::forward<Args>(args)...)))
146 {}
147
148 StoredFunctionCallWithPromise(DecayedTuple<Function, Args...> &&_data)
151 {}
152
153protected:
155 {
157 }
158
159private:
160 // helper to pack back the tuple into parameter pack
161 template<std::size_t... Is>
163 std::index_sequence<Is...>)
165 {}
166
168 DataType data;
169};
170
171template<typename...>
173
174template <typename Function, typename ... Args>
175struct NonPromiseTaskResolver<Function, Args...>
176{
178 static auto run(TaskWithArgs &&args, const TaskStartParameters &startParameters) {
179 return (new StoredFunctionCall<Function, Args...>(std::move(args)))
180 ->start(startParameters);
181 }
182};
183
184template<typename...>
186
187template <typename Function, typename ... Args>
188struct 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.");
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
202template <class IsDirectlyInvocable, class Function, class... Args>
204
205template <class Function, class... Args>
210
211template <class Function, class... Args>
216
217template <class Function, class... Args>
219 std::decay_t<Args>...>::type, Function, Args...>
220{
221};
222
223} //namespace QtConcurrent
224
225#endif // Q_QDOC
226
227QT_END_NAMESPACE
228
229#endif // QT_NO_CONCURRENT
230
231#endif
\inmodule QtConcurrent
typename InvokeResult< Function, Args... >::Type InvokeResultType
static Type initData(Function &&f, QPromise< PromiseType > &promise, Arg &&fa, Args &&...args)
static constexpr void invoke(std::decay_t< Function > function, std::decay_t< Arg > object, QPromise< PromiseType > &promise, std::decay_t< Args >... args)
static constexpr void invoke(std::decay_t< Function > function, QPromise< PromiseType > &promise, std::decay_t< Args >... args)
static Type initData(Function &&f, QPromise< PromiseType > &promise, Args &&...args)
static auto run(TaskWithArgs &&args, const TaskStartParameters &startParameters)
static auto run(TaskWithArgs &&args, const TaskStartParameters &startParameters)
FunctionResolver< Function, PromiseType, Args... > Resolver