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