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