annotate win64-msvc/include/kj/async-prelude.h @ 64:eccd51b72864

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