Mercurial > hg > sv-dependency-builds
comparison win64-msvc/include/kj/async-prelude.h @ 47:d93140aac40b
Current Capnp libs and headers from git
author | Chris Cannam |
---|---|
date | Thu, 20 Oct 2016 18:15:38 +0100 |
parents | |
children | 0f2d93caa50c |
comparison
equal
deleted
inserted
replaced
46:efe5b9f38b13 | 47:d93140aac40b |
---|---|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 // This file contains a bunch of internal declarations that must appear before async.h can start. | |
23 // We don't define these directly in async.h because it makes the file hard to read. | |
24 | |
25 #ifndef KJ_ASYNC_PRELUDE_H_ | |
26 #define KJ_ASYNC_PRELUDE_H_ | |
27 | |
28 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS | |
29 #pragma GCC system_header | |
30 #endif | |
31 | |
32 #include "exception.h" | |
33 #include "tuple.h" | |
34 | |
35 namespace kj { | |
36 | |
37 class EventLoop; | |
38 template <typename T> | |
39 class Promise; | |
40 class WaitScope; | |
41 | |
42 template <typename T> | |
43 Promise<Array<T>> joinPromises(Array<Promise<T>>&& promises); | |
44 Promise<void> joinPromises(Array<Promise<void>>&& promises); | |
45 | |
46 namespace _ { // private | |
47 | |
48 template <typename T> struct JoinPromises_ { typedef T Type; }; | |
49 template <typename T> struct JoinPromises_<Promise<T>> { typedef T Type; }; | |
50 | |
51 template <typename T> | |
52 using JoinPromises = typename JoinPromises_<T>::Type; | |
53 // If T is Promise<U>, resolves to U, otherwise resolves to T. | |
54 // | |
55 // TODO(cleanup): Rename to avoid confusion with joinPromises() call which is completely | |
56 // unrelated. | |
57 | |
58 class PropagateException { | |
59 // A functor which accepts a kj::Exception as a parameter and returns a broken promise of | |
60 // arbitrary type which simply propagates the exception. | |
61 public: | |
62 class Bottom { | |
63 public: | |
64 Bottom(Exception&& exception): exception(kj::mv(exception)) {} | |
65 | |
66 Exception asException() { return kj::mv(exception); } | |
67 | |
68 private: | |
69 Exception exception; | |
70 }; | |
71 | |
72 Bottom operator()(Exception&& e) { | |
73 return Bottom(kj::mv(e)); | |
74 } | |
75 Bottom operator()(const Exception& e) { | |
76 return Bottom(kj::cp(e)); | |
77 } | |
78 }; | |
79 | |
80 template <typename Func, typename T> | |
81 struct ReturnType_ { typedef decltype(instance<Func>()(instance<T>())) Type; }; | |
82 template <typename Func> | |
83 struct ReturnType_<Func, void> { typedef decltype(instance<Func>()()) Type; }; | |
84 | |
85 template <typename Func, typename T> | |
86 using ReturnType = typename ReturnType_<Func, T>::Type; | |
87 // The return type of functor Func given a parameter of type T, with the special exception that if | |
88 // T is void, this is the return type of Func called with no arguments. | |
89 | |
90 template <typename T> struct SplitTuplePromise_ { typedef Promise<T> Type; }; | |
91 template <typename... T> | |
92 struct SplitTuplePromise_<kj::_::Tuple<T...>> { | |
93 typedef kj::Tuple<Promise<JoinPromises<T>>...> Type; | |
94 }; | |
95 | |
96 template <typename T> | |
97 using SplitTuplePromise = typename SplitTuplePromise_<T>::Type; | |
98 // T -> Promise<T> | |
99 // Tuple<T> -> Tuple<Promise<T>> | |
100 | |
101 struct Void {}; | |
102 // Application code should NOT refer to this! See `kj::READY_NOW` instead. | |
103 | |
104 template <typename T> struct FixVoid_ { typedef T Type; }; | |
105 template <> struct FixVoid_<void> { typedef Void Type; }; | |
106 template <typename T> using FixVoid = typename FixVoid_<T>::Type; | |
107 // FixVoid<T> is just T unless T is void in which case it is _::Void (an empty struct). | |
108 | |
109 template <typename T> struct UnfixVoid_ { typedef T Type; }; | |
110 template <> struct UnfixVoid_<Void> { typedef void Type; }; | |
111 template <typename T> using UnfixVoid = typename UnfixVoid_<T>::Type; | |
112 // UnfixVoid is the opposite of FixVoid. | |
113 | |
114 template <typename In, typename Out> | |
115 struct MaybeVoidCaller { | |
116 // Calls the function converting a Void input to an empty parameter list and a void return | |
117 // value to a Void output. | |
118 | |
119 template <typename Func> | |
120 static inline Out apply(Func& func, In&& in) { | |
121 return func(kj::mv(in)); | |
122 } | |
123 }; | |
124 template <typename In, typename Out> | |
125 struct MaybeVoidCaller<In&, Out> { | |
126 template <typename Func> | |
127 static inline Out apply(Func& func, In& in) { | |
128 return func(in); | |
129 } | |
130 }; | |
131 template <typename Out> | |
132 struct MaybeVoidCaller<Void, Out> { | |
133 template <typename Func> | |
134 static inline Out apply(Func& func, Void&& in) { | |
135 return func(); | |
136 } | |
137 }; | |
138 template <typename In> | |
139 struct MaybeVoidCaller<In, Void> { | |
140 template <typename Func> | |
141 static inline Void apply(Func& func, In&& in) { | |
142 func(kj::mv(in)); | |
143 return Void(); | |
144 } | |
145 }; | |
146 template <typename In> | |
147 struct MaybeVoidCaller<In&, Void> { | |
148 template <typename Func> | |
149 static inline Void apply(Func& func, In& in) { | |
150 func(in); | |
151 return Void(); | |
152 } | |
153 }; | |
154 template <> | |
155 struct MaybeVoidCaller<Void, Void> { | |
156 template <typename Func> | |
157 static inline Void apply(Func& func, Void&& in) { | |
158 func(); | |
159 return Void(); | |
160 } | |
161 }; | |
162 | |
163 template <typename T> | |
164 inline T&& returnMaybeVoid(T&& t) { | |
165 return kj::fwd<T>(t); | |
166 } | |
167 inline void returnMaybeVoid(Void&& v) {} | |
168 | |
169 class ExceptionOrValue; | |
170 class PromiseNode; | |
171 class ChainPromiseNode; | |
172 template <typename T> | |
173 class ForkHub; | |
174 | |
175 class TaskSetImpl; | |
176 | |
177 class Event; | |
178 | |
179 class PromiseBase { | |
180 public: | |
181 kj::String trace(); | |
182 // Dump debug info about this promise. | |
183 | |
184 private: | |
185 Own<PromiseNode> node; | |
186 | |
187 PromiseBase() = default; | |
188 PromiseBase(Own<PromiseNode>&& node): node(kj::mv(node)) {} | |
189 | |
190 friend class kj::EventLoop; | |
191 friend class ChainPromiseNode; | |
192 template <typename> | |
193 friend class kj::Promise; | |
194 friend class TaskSetImpl; | |
195 template <typename U> | |
196 friend Promise<Array<U>> kj::joinPromises(Array<Promise<U>>&& promises); | |
197 friend Promise<void> kj::joinPromises(Array<Promise<void>>&& promises); | |
198 }; | |
199 | |
200 void detach(kj::Promise<void>&& promise); | |
201 void waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); | |
202 Promise<void> yield(); | |
203 Own<PromiseNode> neverDone(); | |
204 | |
205 class NeverDone { | |
206 public: | |
207 template <typename T> | |
208 operator Promise<T>() const { | |
209 return Promise<T>(false, neverDone()); | |
210 } | |
211 | |
212 KJ_NORETURN(void wait(WaitScope& waitScope) const); | |
213 }; | |
214 | |
215 } // namespace _ (private) | |
216 } // namespace kj | |
217 | |
218 #endif // KJ_ASYNC_PRELUDE_H_ |