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