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