Chris@47: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: // This file contains a bunch of internal declarations that must appear before async.h can start. Chris@47: // We don't define these directly in async.h because it makes the file hard to read. Chris@47: Chris@47: #ifndef KJ_ASYNC_PRELUDE_H_ Chris@47: #define KJ_ASYNC_PRELUDE_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include "exception.h" Chris@47: #include "tuple.h" Chris@47: Chris@47: namespace kj { Chris@47: Chris@47: class EventLoop; Chris@47: template Chris@47: class Promise; Chris@47: class WaitScope; Chris@47: Chris@47: template Chris@47: Promise> joinPromises(Array>&& promises); Chris@47: Promise joinPromises(Array>&& promises); Chris@47: Chris@47: namespace _ { // private Chris@47: Chris@47: template struct JoinPromises_ { typedef T Type; }; Chris@47: template struct JoinPromises_> { typedef T Type; }; Chris@47: Chris@47: template Chris@47: using JoinPromises = typename JoinPromises_::Type; Chris@47: // If T is Promise, resolves to U, otherwise resolves to T. Chris@47: // Chris@47: // TODO(cleanup): Rename to avoid confusion with joinPromises() call which is completely Chris@47: // unrelated. Chris@47: Chris@47: class PropagateException { Chris@47: // A functor which accepts a kj::Exception as a parameter and returns a broken promise of Chris@47: // arbitrary type which simply propagates the exception. Chris@47: public: Chris@47: class Bottom { Chris@47: public: Chris@47: Bottom(Exception&& exception): exception(kj::mv(exception)) {} Chris@47: Chris@47: Exception asException() { return kj::mv(exception); } Chris@47: Chris@47: private: Chris@47: Exception exception; Chris@47: }; Chris@47: Chris@47: Bottom operator()(Exception&& e) { Chris@47: return Bottom(kj::mv(e)); Chris@47: } Chris@47: Bottom operator()(const Exception& e) { Chris@47: return Bottom(kj::cp(e)); Chris@47: } Chris@47: }; Chris@47: Chris@47: template Chris@47: struct ReturnType_ { typedef decltype(instance()(instance())) Type; }; Chris@47: template Chris@47: struct ReturnType_ { typedef decltype(instance()()) Type; }; Chris@47: Chris@47: template Chris@47: using ReturnType = typename ReturnType_::Type; Chris@47: // The return type of functor Func given a parameter of type T, with the special exception that if Chris@47: // T is void, this is the return type of Func called with no arguments. Chris@47: Chris@47: template struct SplitTuplePromise_ { typedef Promise Type; }; Chris@47: template Chris@47: struct SplitTuplePromise_> { Chris@47: typedef kj::Tuple>...> Type; Chris@47: }; Chris@47: Chris@47: template Chris@47: using SplitTuplePromise = typename SplitTuplePromise_::Type; Chris@47: // T -> Promise Chris@47: // Tuple -> Tuple> Chris@47: Chris@47: struct Void {}; Chris@47: // Application code should NOT refer to this! See `kj::READY_NOW` instead. Chris@47: Chris@47: template struct FixVoid_ { typedef T Type; }; Chris@47: template <> struct FixVoid_ { typedef Void Type; }; Chris@47: template using FixVoid = typename FixVoid_::Type; Chris@47: // FixVoid is just T unless T is void in which case it is _::Void (an empty struct). Chris@47: Chris@47: template struct UnfixVoid_ { typedef T Type; }; Chris@47: template <> struct UnfixVoid_ { typedef void Type; }; Chris@47: template using UnfixVoid = typename UnfixVoid_::Type; Chris@47: // UnfixVoid is the opposite of FixVoid. Chris@47: Chris@47: template Chris@47: struct MaybeVoidCaller { Chris@47: // Calls the function converting a Void input to an empty parameter list and a void return Chris@47: // value to a Void output. Chris@47: Chris@47: template Chris@47: static inline Out apply(Func& func, In&& in) { Chris@47: return func(kj::mv(in)); Chris@47: } Chris@47: }; Chris@47: template Chris@47: struct MaybeVoidCaller { Chris@47: template Chris@47: static inline Out apply(Func& func, In& in) { Chris@47: return func(in); Chris@47: } Chris@47: }; Chris@47: template Chris@47: struct MaybeVoidCaller { Chris@47: template Chris@47: static inline Out apply(Func& func, Void&& in) { Chris@47: return func(); Chris@47: } Chris@47: }; Chris@47: template Chris@47: struct MaybeVoidCaller { Chris@47: template Chris@47: static inline Void apply(Func& func, In&& in) { Chris@47: func(kj::mv(in)); Chris@47: return Void(); Chris@47: } Chris@47: }; Chris@47: template Chris@47: struct MaybeVoidCaller { Chris@47: template Chris@47: static inline Void apply(Func& func, In& in) { Chris@47: func(in); Chris@47: return Void(); Chris@47: } Chris@47: }; Chris@47: template <> Chris@47: struct MaybeVoidCaller { Chris@47: template Chris@47: static inline Void apply(Func& func, Void&& in) { Chris@47: func(); Chris@47: return Void(); Chris@47: } Chris@47: }; Chris@47: Chris@47: template Chris@47: inline T&& returnMaybeVoid(T&& t) { Chris@47: return kj::fwd(t); Chris@47: } Chris@47: inline void returnMaybeVoid(Void&& v) {} Chris@47: Chris@47: class ExceptionOrValue; Chris@47: class PromiseNode; Chris@47: class ChainPromiseNode; Chris@47: template Chris@47: class ForkHub; Chris@47: Chris@47: class TaskSetImpl; Chris@47: Chris@47: class Event; Chris@47: Chris@47: class PromiseBase { Chris@47: public: Chris@47: kj::String trace(); Chris@47: // Dump debug info about this promise. Chris@47: Chris@47: private: Chris@47: Own node; Chris@47: Chris@47: PromiseBase() = default; Chris@47: PromiseBase(Own&& node): node(kj::mv(node)) {} Chris@47: Chris@47: friend class kj::EventLoop; Chris@47: friend class ChainPromiseNode; Chris@47: template Chris@47: friend class kj::Promise; Chris@47: friend class TaskSetImpl; Chris@47: template Chris@47: friend Promise> kj::joinPromises(Array>&& promises); Chris@47: friend Promise kj::joinPromises(Array>&& promises); Chris@47: }; Chris@47: Chris@47: void detach(kj::Promise&& promise); Chris@47: void waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); Chris@47: Promise yield(); Chris@47: Own neverDone(); Chris@47: Chris@47: class NeverDone { Chris@47: public: Chris@47: template Chris@47: operator Promise() const { Chris@47: return Promise(false, neverDone()); Chris@47: } Chris@47: Chris@47: KJ_NORETURN(void wait(WaitScope& waitScope) const); Chris@47: }; Chris@47: Chris@47: } // namespace _ (private) Chris@47: } // namespace kj Chris@47: Chris@47: #endif // KJ_ASYNC_PRELUDE_H_