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