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