Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/src/qtpromise/qpromise.h
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/src/qtpromise/qpromise.h
- This file was added.
1 | #ifndef QTPROMISE_QPROMISE_H | ||||
---|---|---|---|---|---|
2 | #define QTPROMISE_QPROMISE_H | ||||
3 | | ||||
4 | #include "qpromise_p.h" | ||||
5 | #include "qpromiseexceptions.h" | ||||
6 | #include "qpromiseglobal.h" | ||||
7 | #include "qpromiseresolver.h" | ||||
8 | | ||||
9 | // Qt | ||||
10 | #include <QExplicitlySharedDataPointer> | ||||
11 | | ||||
12 | namespace QtPromise { | ||||
13 | | ||||
14 | template <typename T> | ||||
15 | class QPromiseBase | ||||
16 | { | ||||
17 | public: | ||||
18 | using Type = T; | ||||
19 | | ||||
20 | template <typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 1, int>::type = 0> | ||||
21 | inline QPromiseBase(F resolver); | ||||
22 | | ||||
23 | template <typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count != 1, int>::type = 0> | ||||
24 | inline QPromiseBase(F resolver); | ||||
25 | | ||||
26 | QPromiseBase(const QPromiseBase<T>& other): m_d(other.m_d) {} | ||||
27 | QPromiseBase(const QPromise<T>& other): m_d(other.m_d) {} | ||||
28 | QPromiseBase(QPromiseBase<T>&& other) Q_DECL_NOEXCEPT { swap(other); } | ||||
29 | | ||||
30 | virtual ~QPromiseBase() { } | ||||
31 | | ||||
32 | QPromiseBase<T>& operator=(const QPromiseBase<T>& other) { m_d = other.m_d; return *this;} | ||||
33 | QPromiseBase<T>& operator=(QPromiseBase<T>&& other) Q_DECL_NOEXCEPT | ||||
34 | { QPromiseBase<T>(std::move(other)).swap(*this); return *this; } | ||||
35 | | ||||
36 | bool operator==(const QPromiseBase<T>& other) const { return (m_d == other.m_d); } | ||||
37 | bool operator!=(const QPromiseBase<T>& other) const { return (m_d != other.m_d); } | ||||
38 | | ||||
39 | void swap(QPromiseBase<T>& other) Q_DECL_NOEXCEPT { qSwap(m_d, other.m_d); } | ||||
40 | | ||||
41 | bool isFulfilled() const { return m_d->isFulfilled(); } | ||||
42 | bool isRejected() const { return m_d->isRejected(); } | ||||
43 | bool isPending() const { return m_d->isPending(); } | ||||
44 | | ||||
45 | template <typename TFulfilled, typename TRejected> | ||||
46 | inline typename QtPromisePrivate::PromiseHandler<T, TFulfilled>::Promise | ||||
47 | then(const TFulfilled& fulfilled, const TRejected& rejected) const; | ||||
48 | | ||||
49 | template <typename TFulfilled> | ||||
50 | inline typename QtPromisePrivate::PromiseHandler<T, TFulfilled>::Promise | ||||
51 | then(TFulfilled&& fulfilled) const; | ||||
52 | | ||||
53 | template <typename TRejected> | ||||
54 | inline typename QtPromisePrivate::PromiseHandler<T, std::nullptr_t>::Promise | ||||
55 | fail(TRejected&& rejected) const; | ||||
56 | | ||||
57 | template <typename THandler> | ||||
58 | inline QPromise<T> finally(THandler handler) const; | ||||
59 | | ||||
60 | template <typename THandler> | ||||
61 | inline QPromise<T> tap(THandler handler) const; | ||||
62 | | ||||
63 | template <typename THandler> | ||||
64 | inline QPromise<T> tapFail(THandler handler) const; | ||||
65 | | ||||
66 | template <typename E = QPromiseTimeoutException> | ||||
67 | inline QPromise<T> timeout(int msec, E&& error = E()) const; | ||||
68 | | ||||
69 | inline QPromise<T> delay(int msec) const; | ||||
70 | inline QPromise<T> wait() const; | ||||
71 | | ||||
72 | public: // STATIC | ||||
73 | template <typename E> | ||||
74 | inline static QPromise<T> reject(E&& error); | ||||
75 | | ||||
76 | protected: | ||||
77 | friend struct QtPromisePrivate::PromiseFulfill<QPromise<T>>; | ||||
78 | friend class QtPromisePrivate::PromiseResolver<T>; | ||||
79 | friend struct QtPromisePrivate::PromiseInspect; | ||||
80 | | ||||
81 | QExplicitlySharedDataPointer<QtPromisePrivate::PromiseData<T>> m_d; | ||||
82 | }; | ||||
83 | | ||||
84 | template <typename T> | ||||
85 | class QPromise : public QPromiseBase<T> | ||||
86 | { | ||||
87 | public: | ||||
88 | template <typename F> | ||||
89 | QPromise(F&& resolver): QPromiseBase<T>(std::forward<F>(resolver)) { } | ||||
90 | | ||||
91 | template <typename Functor> | ||||
92 | inline QPromise<T> | ||||
93 | each(Functor fn); | ||||
94 | | ||||
95 | template <typename Functor> | ||||
96 | inline QPromise<T> | ||||
97 | filter(Functor fn); | ||||
98 | | ||||
99 | template <typename Functor> | ||||
100 | inline typename QtPromisePrivate::PromiseMapper<T, Functor>::PromiseType | ||||
101 | map(Functor fn); | ||||
102 | | ||||
103 | template <typename Functor, typename Input> | ||||
104 | inline typename QtPromisePrivate::PromiseDeduce<Input>::Type | ||||
105 | reduce(Functor fn, Input initial); | ||||
106 | | ||||
107 | template <typename Functor, typename U = T> | ||||
108 | inline typename QtPromisePrivate::PromiseDeduce<typename U::value_type>::Type | ||||
109 | reduce(Functor fn); | ||||
110 | | ||||
111 | public: // STATIC | ||||
112 | | ||||
113 | // DEPRECATED (remove at version 1) | ||||
114 | template <template <typename, typename...> class Sequence = QVector, typename ...Args> | ||||
115 | Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<QVector<T>> | ||||
116 | all(const Sequence<QPromise<T>, Args...>& promises); | ||||
117 | | ||||
118 | inline static QPromise<T> resolve(const T& value); | ||||
119 | inline static QPromise<T> resolve(T&& value); | ||||
120 | | ||||
121 | private: | ||||
122 | friend class QPromiseBase<T>; | ||||
123 | }; | ||||
124 | | ||||
125 | template <> | ||||
126 | class QPromise<void> : public QPromiseBase<void> | ||||
127 | { | ||||
128 | public: | ||||
129 | template <typename F> | ||||
130 | QPromise(F&& resolver): QPromiseBase<void>(std::forward<F>(resolver)) { } | ||||
131 | | ||||
132 | public: // STATIC | ||||
133 | | ||||
134 | // DEPRECATED (remove at version 1) | ||||
135 | template <template <typename, typename...> class Sequence = QVector, typename ...Args> | ||||
136 | Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<void> | ||||
137 | all(const Sequence<QPromise<void>, Args...>& promises); | ||||
138 | | ||||
139 | inline static QPromise<void> resolve(); | ||||
140 | | ||||
141 | private: | ||||
142 | friend class QPromiseBase<void>; | ||||
143 | }; | ||||
144 | | ||||
145 | } // namespace QtPromise | ||||
146 | | ||||
147 | #include "qpromise.inl" | ||||
148 | | ||||
149 | #endif // QTPROMISE_QPROMISE_H |