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