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_FUNCTIONWRAPPERS_H
5#define QTCONCURRENT_FUNCTIONWRAPPERS_H
6
7#include <QtConcurrent/qtconcurrentcompilertest.h>
8#include <QtConcurrent/qtconcurrentreducekernel.h>
9#include <QtCore/qfuture.h>
10
11#include <tuple>
12
13#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
14
15QT_BEGIN_NAMESPACE
16
17namespace QtPrivate {
18
19struct PushBackWrapper
20{
21 template <class C, class U>
22 inline void operator()(C &c, const U &u) const
23 {
24 return c.push_back(u);
25 }
26
27 template <class C, class U>
28 inline void operator()(C &c, U &&u) const
29 {
30 return c.push_back(u);
31 }
32};
33
34// -- MapResultType
35
36template <class T, class Enable = void>
37struct Argument
38{
39 using Type = void;
40};
41
42template <class Sequence>
43struct Argument<Sequence, typename std::enable_if<IsIterableValue<Sequence>>::type>
44{
45 using Type = std::decay_t<decltype(*std::declval<Sequence>().begin())>;
46};
47
48template <class Iterator>
49struct Argument<Iterator, typename std::enable_if<IsDereferenceableValue<Iterator>>::type>
50{
51 using Type = std::decay_t<decltype(*std::declval<Iterator>())>;
52};
53
54template <class T>
55using ArgumentType = typename Argument<T>::Type;
56
57template <class T, class MapFunctor>
58struct MapResult
59{
60 static_assert(std::is_invocable_v<std::decay_t<MapFunctor>, ArgumentType<T>>,
61 "It's not possible to invoke the function with passed argument.");
62 using Type = std::invoke_result_t<std::decay_t<MapFunctor>, ArgumentType<T>>;
63};
64
65template <class T, class MapFunctor>
66using MapResultType = typename MapResult<T, MapFunctor>::Type;
67
68// -- ReduceResultType
69
70template <class T>
71struct ReduceResultType;
72
73template <class U, class V>
74struct ReduceResultType<void(*)(U&,V)>
75{
76 using ResultType = U;
77};
78
79template <class T, class C, class U>
80struct ReduceResultType<T(C::*)(U)>
81{
82 using ResultType = C;
83};
84
85template <class U, class V>
86struct ReduceResultType<std::function<void(U&, V)>>
87{
88 using ResultType = U;
89};
90
91template <typename R, typename ...A>
92struct ReduceResultType<R(*)(A...)>
93{
94 using ResultType = typename std::tuple_element<0, std::tuple<A...>>::type;
95};
96
97template <class U, class V>
98struct ReduceResultType<void(*)(U&,V) noexcept>
99{
100 using ResultType = U;
101};
102
103template <class T, class C, class U>
104struct ReduceResultType<T(C::*)(U) noexcept>
105{
106 using ResultType = C;
107};
108
109template<class T, class Enable = void>
110inline constexpr bool hasCallOperator_v = false;
111
112template<class T>
113inline constexpr bool hasCallOperator_v<T, std::void_t<decltype(&T::operator())>> = true;
114
115template<class T, class Enable = void>
116inline constexpr bool isIterator_v = false;
117
118template<class T>
119inline constexpr bool isIterator_v<T, std::void_t<typename std::iterator_traits<T>::value_type>> =
120 true;
121
122template <class Callable, class Sequence>
123using isInvocable = std::is_invocable<Callable, typename std::decay_t<Sequence>::value_type>;
124
125template <class InitialValueType, class ResultType>
126inline constexpr bool isInitialValueCompatible_v = std::conjunction_v<
127 std::is_convertible<InitialValueType, ResultType>,
128 std::negation<std::is_same<std::decay_t<InitialValueType>, QtConcurrent::ReduceOption>>>;
129
130template<class Callable, class Enable = void>
131struct ReduceResultTypeHelper
132{
133};
134
135template <class Callable>
136struct ReduceResultTypeHelper<Callable,
137 typename std::enable_if_t<std::is_function_v<std::remove_pointer_t<std::decay_t<Callable>>>
138 || std::is_member_function_pointer_v<std::decay_t<Callable>>>>
139{
140 using type = typename QtPrivate::ReduceResultType<std::decay_t<Callable>>::ResultType;
141};
142
143template <class Callable>
144struct ReduceResultTypeHelper<Callable,
145 typename std::enable_if_t<!std::is_function_v<std::remove_pointer_t<std::decay_t<Callable>>>
146 && hasCallOperator_v<std::decay_t<Callable>>>>
147{
148 using type = std::decay_t<typename QtPrivate::ArgResolver<Callable>::First>;
149};
150
151// -- MapSequenceResultType
152
153template <class InputSequence, class MapFunctor>
154struct MapSequenceResultType
155{
156 static_assert(std::is_same_v<typename InputSequence::value_type,
157 QtPrivate::MapResultType<InputSequence, MapFunctor>>,
158 "Couldn't deduce the output sequence type, you must specify it explicitly.");
159 typedef InputSequence ResultType;
160};
161
162#ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS
163
164template <template <typename...> class InputSequence, typename MapFunctor, typename ...T>
165struct MapSequenceResultType<InputSequence<T...>, MapFunctor>
166{
167 typedef InputSequence<QtPrivate::MapResultType<InputSequence<T...>, MapFunctor>> ResultType;
168};
169
170#endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER
171
172} // namespace QtPrivate.
173
174
175QT_END_NAMESPACE
176
177#endif // QT_NO_CONCURRENT
178
179#endif
180

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