annotate win32-mingw/include/kj/async-prelude.h @ 50:37d53a7e8262

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