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
4#ifndef QTCONCURRENT_RUN_H
5#define QTCONCURRENT_RUN_H
6
7#if 0
8#pragma qt_class(QtConcurrentRun)
9#endif
10
11#include <QtConcurrent/qtconcurrentcompilertest.h>
12
13#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
14
15#include <QtConcurrent/qtconcurrentrunbase.h>
16#include <QtConcurrent/qtconcurrentstoredfunctioncall.h>
17
18QT_BEGIN_NAMESPACE
19
20#ifdef Q_QDOC
21
22typedef int Function;
23
24namespace QtConcurrent {
25
26 template <typename T>
27 QFuture<T> run(Function function, ...);
28
29 template <typename T>
30 QFuture<T> run(QThreadPool *pool, Function function, ...);
31
32} // namespace QtConcurrent
33
34#else
35
36namespace QtConcurrent {
37
38template <class Function, class ...Args>
39[[nodiscard]]
40auto run(QThreadPool *pool, Function &&f, Args &&...args)
41{
42 DecayedTuple<Function, Args...> tuple { std::forward<Function>(f),
43 std::forward<Args>(args)... };
44 return TaskResolver<std::decay_t<Function>, std::decay_t<Args>...>::run(
45 std::move(tuple), TaskStartParameters { .threadPool: pool });
46}
47
48template <class Function, class ...Args>
49[[nodiscard]]
50auto run(QThreadPool *pool, std::reference_wrapper<const Function> &&functionWrapper,
51 Args &&...args)
52{
53 return run(pool, std::forward<const Function>(functionWrapper.get()),
54 std::forward<Args>(args)...);
55}
56
57template <class Function, class ...Args>
58[[nodiscard]]
59auto run(Function &&f, Args &&...args)
60{
61 return run(QThreadPool::globalInstance(), std::forward<Function>(f),
62 std::forward<Args>(args)...);
63}
64
65// overload with a Promise Type hint, takes thread pool
66template <class PromiseType, class Function, class ...Args>
67[[nodiscard]]
68auto run(QThreadPool *pool, Function &&f, Args &&...args)
69{
70 return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>(
71 std::forward<Function>(f), std::forward<Args>(args)...))->start(pool);
72}
73
74// overload with a Promise Type hint, uses global thread pool
75template <class PromiseType, class Function, class ...Args>
76[[nodiscard]]
77auto run(Function &&f, Args &&...args)
78{
79 return run<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f),
80 std::forward<Args>(args)...);
81}
82
83} //namespace QtConcurrent
84
85#endif // Q_QDOC
86
87QT_END_NAMESPACE
88
89#endif // QT_NO_CONCURRENT
90
91#endif
92

source code of qtbase/src/concurrent/qtconcurrentrun.h