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