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