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