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