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