// // impl/use_future.hpp // ~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef ASIO_IMPL_USE_FUTURE_HPP #define ASIO_IMPL_USE_FUTURE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include #include "asio/async_result.hpp" #include "asio/detail/memory.hpp" #include "asio/dispatch.hpp" #include "asio/error_code.hpp" #include "asio/execution.hpp" #include "asio/packaged_task.hpp" #include "asio/system_error.hpp" #include "asio/system_executor.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template inline void promise_invoke_and_set(std::promise& p, F& f, ASIO_MOVE_ARG(Args)... args) { #if !defined(ASIO_NO_EXCEPTIONS) try #endif // !defined(ASIO_NO_EXCEPTIONS) { p.set_value(f(ASIO_MOVE_CAST(Args)(args)...)); } #if !defined(ASIO_NO_EXCEPTIONS) catch (...) { p.set_exception(std::current_exception()); } #endif // !defined(ASIO_NO_EXCEPTIONS) } template inline void promise_invoke_and_set(std::promise& p, F& f, ASIO_MOVE_ARG(Args)... args) { #if !defined(ASIO_NO_EXCEPTIONS) try #endif // !defined(ASIO_NO_EXCEPTIONS) { f(ASIO_MOVE_CAST(Args)(args)...); p.set_value(); } #if !defined(ASIO_NO_EXCEPTIONS) catch (...) { p.set_exception(std::current_exception()); } #endif // !defined(ASIO_NO_EXCEPTIONS) } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) template inline void promise_invoke_and_set(std::promise& p, F& f) { #if !defined(ASIO_NO_EXCEPTIONS) try #endif // !defined(ASIO_NO_EXCEPTIONS) { p.set_value(f()); } #if !defined(ASIO_NO_EXCEPTIONS) catch (...) { p.set_exception(std::current_exception()); } #endif // !defined(ASIO_NO_EXCEPTIONS) } template inline void promise_invoke_and_set(std::promise& p, F& f) { #if !defined(ASIO_NO_EXCEPTIONS) try #endif // !defined(ASIO_NO_EXCEPTIONS) { f(); p.set_value(); #if !defined(ASIO_NO_EXCEPTIONS) } catch (...) { p.set_exception(std::current_exception()); } #endif // !defined(ASIO_NO_EXCEPTIONS) } #if defined(ASIO_NO_EXCEPTIONS) #define ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \ template \ inline void promise_invoke_and_set(std::promise& p, \ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ p.set_value(f(ASIO_VARIADIC_MOVE_ARGS(n))); \ } \ \ template \ inline void promise_invoke_and_set(std::promise& p, \ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ f(ASIO_VARIADIC_MOVE_ARGS(n)); \ p.set_value(); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_INVOKE_DEF) #undef ASIO_PRIVATE_PROMISE_INVOKE_DEF #else // defined(ASIO_NO_EXCEPTIONS) #define ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \ template \ inline void promise_invoke_and_set(std::promise& p, \ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ try \ { \ p.set_value(f(ASIO_VARIADIC_MOVE_ARGS(n))); \ } \ catch (...) \ { \ p.set_exception(std::current_exception()); \ } \ } \ \ template \ inline void promise_invoke_and_set(std::promise& p, \ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ try \ { \ f(ASIO_VARIADIC_MOVE_ARGS(n)); \ p.set_value(); \ } \ catch (...) \ { \ p.set_exception(std::current_exception()); \ } \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_INVOKE_DEF) #undef ASIO_PRIVATE_PROMISE_INVOKE_DEF #endif // defined(ASIO_NO_EXCEPTIONS) #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) // A function object adapter to invoke a nullary function object and capture // any exception thrown into a promise. template class promise_invoker { public: promise_invoker(const shared_ptr >& p, ASIO_MOVE_ARG(F) f) : p_(p), f_(ASIO_MOVE_CAST(F)(f)) { } void operator()() { #if !defined(ASIO_NO_EXCEPTIONS) try #endif // !defined(ASIO_NO_EXCEPTIONS) { f_(); } #if !defined(ASIO_NO_EXCEPTIONS) catch (...) { p_->set_exception(std::current_exception()); } #endif // !defined(ASIO_NO_EXCEPTIONS) } private: shared_ptr > p_; typename decay::type f_; }; // An executor that adapts the system_executor to capture any exeption thrown // by a submitted function object and save it into a promise. template class promise_executor { public: explicit promise_executor(const shared_ptr >& p) : p_(p) { } static ASIO_CONSTEXPR Blocking query(execution::blocking_t) { return Blocking(); } promise_executor require(execution::blocking_t::possibly_t) const { return promise_executor(p_); } promise_executor require(execution::blocking_t::never_t) const { return promise_executor(p_); } template void execute(ASIO_MOVE_ARG(F) f) const { execution::execute( asio::require(system_executor(), Blocking()), promise_invoker(p_, ASIO_MOVE_CAST(F)(f))); } #if !defined(ASIO_NO_TS_EXECUTORS) execution_context& context() const ASIO_NOEXCEPT { return system_executor().context(); } void on_work_started() const ASIO_NOEXCEPT {} void on_work_finished() const ASIO_NOEXCEPT {} template void dispatch(ASIO_MOVE_ARG(F) f, const A&) const { promise_invoker(p_, ASIO_MOVE_CAST(F)(f))(); } template void post(ASIO_MOVE_ARG(F) f, const A& a) const { system_executor().post( promise_invoker(p_, ASIO_MOVE_CAST(F)(f)), a); } template void defer(ASIO_MOVE_ARG(F) f, const A& a) const { system_executor().defer( promise_invoker(p_, ASIO_MOVE_CAST(F)(f)), a); } #endif // !defined(ASIO_NO_TS_EXECUTORS) friend bool operator==(const promise_executor& a, const promise_executor& b) ASIO_NOEXCEPT { return a.p_ == b.p_; } friend bool operator!=(const promise_executor& a, const promise_executor& b) ASIO_NOEXCEPT { return a.p_ != b.p_; } private: shared_ptr > p_; }; // The base class for all completion handlers that create promises. template class promise_creator { public: typedef promise_executor executor_type; executor_type get_executor() const ASIO_NOEXCEPT { return executor_type(p_); } typedef std::future future_type; future_type get_future() { return p_->get_future(); } protected: template void create_promise(const Allocator& a) { ASIO_REBIND_ALLOC(Allocator, char) b(a); p_ = std::allocate_shared>(b, std::allocator_arg, b); } shared_ptr > p_; }; // For completion signature void(). class promise_handler_0 : public promise_creator { public: void operator()() { this->p_->set_value(); } }; // For completion signature void(error_code). class promise_handler_ec_0 : public promise_creator { public: void operator()(const asio::error_code& ec) { if (ec) { this->p_->set_exception( std::make_exception_ptr( asio::system_error(ec))); } else { this->p_->set_value(); } } }; // For completion signature void(exception_ptr). class promise_handler_ex_0 : public promise_creator { public: void operator()(const std::exception_ptr& ex) { if (ex) { this->p_->set_exception(ex); } else { this->p_->set_value(); } } }; // For completion signature void(T). template class promise_handler_1 : public promise_creator { public: template void operator()(ASIO_MOVE_ARG(Arg) arg) { this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg)); } }; // For completion signature void(error_code, T). template class promise_handler_ec_1 : public promise_creator { public: template void operator()(const asio::error_code& ec, ASIO_MOVE_ARG(Arg) arg) { if (ec) { this->p_->set_exception( std::make_exception_ptr( asio::system_error(ec))); } else this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg)); } }; // For completion signature void(exception_ptr, T). template class promise_handler_ex_1 : public promise_creator { public: template void operator()(const std::exception_ptr& ex, ASIO_MOVE_ARG(Arg) arg) { if (ex) this->p_->set_exception(ex); else this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg)); } }; // For completion signature void(T1, ..., Tn); template class promise_handler_n : public promise_creator { public: #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(ASIO_MOVE_ARG(Args)... args) { this->p_->set_value( std::forward_as_tuple( ASIO_MOVE_CAST(Args)(args)...)); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_CALL_OP_DEF(n) \ template \ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \ {\ this->p_->set_value( \ std::forward_as_tuple( \ ASIO_VARIADIC_MOVE_ARGS(n))); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF) #undef ASIO_PRIVATE_CALL_OP_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) }; // For completion signature void(error_code, T1, ..., Tn); template class promise_handler_ec_n : public promise_creator { public: #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(const asio::error_code& ec, ASIO_MOVE_ARG(Args)... args) { if (ec) { this->p_->set_exception( std::make_exception_ptr( asio::system_error(ec))); } else { this->p_->set_value( std::forward_as_tuple( ASIO_MOVE_CAST(Args)(args)...)); } } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_CALL_OP_DEF(n) \ template \ void operator()(const asio::error_code& ec, \ ASIO_VARIADIC_MOVE_PARAMS(n)) \ {\ if (ec) \ { \ this->p_->set_exception( \ std::make_exception_ptr( \ asio::system_error(ec))); \ } \ else \ { \ this->p_->set_value( \ std::forward_as_tuple( \ ASIO_VARIADIC_MOVE_ARGS(n))); \ } \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF) #undef ASIO_PRIVATE_CALL_OP_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) }; // For completion signature void(exception_ptr, T1, ..., Tn); template class promise_handler_ex_n : public promise_creator { public: #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(const std::exception_ptr& ex, ASIO_MOVE_ARG(Args)... args) { if (ex) this->p_->set_exception(ex); else { this->p_->set_value( std::forward_as_tuple( ASIO_MOVE_CAST(Args)(args)...)); } } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_CALL_OP_DEF(n) \ template \ void operator()(const std::exception_ptr& ex, \ ASIO_VARIADIC_MOVE_PARAMS(n)) \ {\ if (ex) \ this->p_->set_exception(ex); \ else \ { \ this->p_->set_value( \ std::forward_as_tuple( \ ASIO_VARIADIC_MOVE_ARGS(n))); \ } \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF) #undef ASIO_PRIVATE_CALL_OP_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) }; // Helper template to choose the appropriate concrete promise handler // implementation based on the supplied completion signature. template class promise_handler_selector; template <> class promise_handler_selector : public promise_handler_0 {}; template <> class promise_handler_selector : public promise_handler_ec_0 {}; template <> class promise_handler_selector : public promise_handler_ex_0 {}; template class promise_handler_selector : public promise_handler_1 {}; template class promise_handler_selector : public promise_handler_ec_1 {}; template class promise_handler_selector : public promise_handler_ex_1 {}; #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template class promise_handler_selector : public promise_handler_n > {}; template class promise_handler_selector : public promise_handler_ec_n > {}; template class promise_handler_selector : public promise_handler_ex_n > {}; #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_PROMISE_SELECTOR_DEF(n) \ template \ class promise_handler_selector< \ void(Arg, ASIO_VARIADIC_TARGS(n))> \ : public promise_handler_n< \ std::tuple > {}; \ \ template \ class promise_handler_selector< \ void(asio::error_code, Arg, ASIO_VARIADIC_TARGS(n))> \ : public promise_handler_ec_n< \ std::tuple > {}; \ \ template \ class promise_handler_selector< \ void(std::exception_ptr, Arg, ASIO_VARIADIC_TARGS(n))> \ : public promise_handler_ex_n< \ std::tuple > {}; \ /**/ ASIO_VARIADIC_GENERATE_5(ASIO_PRIVATE_PROMISE_SELECTOR_DEF) #undef ASIO_PRIVATE_PROMISE_SELECTOR_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) // Completion handlers produced from the use_future completion token, when not // using use_future::operator(). template class promise_handler : public promise_handler_selector { public: typedef Allocator allocator_type; typedef void result_type; promise_handler(use_future_t u) : allocator_(u.get_allocator()) { this->create_promise(allocator_); } allocator_type get_allocator() const ASIO_NOEXCEPT { return allocator_; } private: Allocator allocator_; }; template struct promise_function_wrapper { explicit promise_function_wrapper(Function& f) : function_(ASIO_MOVE_CAST(Function)(f)) { } explicit promise_function_wrapper(const Function& f) : function_(f) { } void operator()() { function_(); } Function function_; }; #if !defined(ASIO_NO_DEPRECATED) template inline void asio_handler_invoke(Function& f, promise_handler* h) { typename promise_handler::executor_type ex(h->get_executor()); asio::dispatch(ex, promise_function_wrapper(f)); } template inline void asio_handler_invoke(const Function& f, promise_handler* h) { typename promise_handler::executor_type ex(h->get_executor()); asio::dispatch(ex, promise_function_wrapper(f)); } #endif // !defined(ASIO_NO_DEPRECATED) // Helper base class for async_result specialisation. template class promise_async_result { public: typedef promise_handler completion_handler_type; typedef typename completion_handler_type::future_type return_type; explicit promise_async_result(completion_handler_type& h) : future_(h.get_future()) { } return_type get() { return ASIO_MOVE_CAST(return_type)(future_); } private: return_type future_; }; // Return value from use_future::operator(). template class packaged_token { public: packaged_token(Function f, const Allocator& a) : function_(ASIO_MOVE_CAST(Function)(f)), allocator_(a) { } //private: Function function_; Allocator allocator_; }; // Completion handlers produced from the use_future completion token, when // using use_future::operator(). template class packaged_handler : public promise_creator { public: typedef Allocator allocator_type; typedef void result_type; packaged_handler(packaged_token t) : function_(ASIO_MOVE_CAST(Function)(t.function_)), allocator_(t.allocator_) { this->create_promise(allocator_); } allocator_type get_allocator() const ASIO_NOEXCEPT { return allocator_; } #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(ASIO_MOVE_ARG(Args)... args) { (promise_invoke_and_set)(*this->p_, function_, ASIO_MOVE_CAST(Args)(args)...); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) void operator()() { (promise_invoke_and_set)(*this->p_, function_); } #define ASIO_PRIVATE_CALL_OP_DEF(n) \ template \ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \ {\ (promise_invoke_and_set)(*this->p_, \ function_, ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF) #undef ASIO_PRIVATE_CALL_OP_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) private: Function function_; Allocator allocator_; }; #if !defined(ASIO_NO_DEPRECATED) template inline void asio_handler_invoke(Function& f, packaged_handler* h) { typename packaged_handler::executor_type ex(h->get_executor()); asio::dispatch(ex, promise_function_wrapper(f)); } template inline void asio_handler_invoke(const Function& f, packaged_handler* h) { typename packaged_handler::executor_type ex(h->get_executor()); asio::dispatch(ex, promise_function_wrapper(f)); } #endif // !defined(ASIO_NO_DEPRECATED) // Helper base class for async_result specialisation. template class packaged_async_result { public: typedef packaged_handler completion_handler_type; typedef typename completion_handler_type::future_type return_type; explicit packaged_async_result(completion_handler_type& h) : future_(h.get_future()) { } return_type get() { return ASIO_MOVE_CAST(return_type)(future_); } private: return_type future_; }; } // namespace detail template template inline detail::packaged_token::type, Allocator> use_future_t::operator()(ASIO_MOVE_ARG(Function) f) const { return detail::packaged_token::type, Allocator>( ASIO_MOVE_CAST(Function)(f), allocator_); } #if !defined(GENERATING_DOCUMENTATION) #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template class async_result, Result(Args...)> : public detail::promise_async_result< void(typename decay::type...), Allocator> { public: explicit async_result( typename detail::promise_async_result::type...), Allocator>::completion_handler_type& h) : detail::promise_async_result< void(typename decay::type...), Allocator>(h) { } }; template class async_result, Result(Args...)> : public detail::packaged_async_result::type> { public: explicit async_result( typename detail::packaged_async_result::type>::completion_handler_type& h) : detail::packaged_async_result::type>(h) { } }; #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) template class async_result, Result()> : public detail::promise_async_result { public: explicit async_result( typename detail::promise_async_result< void(), Allocator>::completion_handler_type& h) : detail::promise_async_result(h) { } }; template class async_result, Result()> : public detail::packaged_async_result::type> { public: explicit async_result( typename detail::packaged_async_result::type>::completion_handler_type& h) : detail::packaged_async_result::type>(h) { } }; #define ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \ template \ class async_result, \ Result(ASIO_VARIADIC_TARGS(n))> \ : public detail::promise_async_result< \ void(ASIO_VARIADIC_DECAY(n)), Allocator> \ { \ public: \ explicit async_result( \ typename detail::promise_async_result< \ void(ASIO_VARIADIC_DECAY(n)), \ Allocator>::completion_handler_type& h) \ : detail::promise_async_result< \ void(ASIO_VARIADIC_DECAY(n)), Allocator>(h) \ { \ } \ }; \ \ template \ class async_result, \ Result(ASIO_VARIADIC_TARGS(n))> \ : public detail::packaged_async_result::type> \ { \ public: \ explicit async_result( \ typename detail::packaged_async_result::type \ >::completion_handler_type& h) \ : detail::packaged_async_result::type>(h) \ { \ } \ }; \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_RESULT_DEF) #undef ASIO_PRIVATE_ASYNC_RESULT_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) namespace traits { #if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT) template struct equality_comparable< asio::detail::promise_executor > { ASIO_STATIC_CONSTEXPR(bool, is_valid = true); ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true); }; #endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT) #if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) template struct execute_member< asio::detail::promise_executor, Function> { ASIO_STATIC_CONSTEXPR(bool, is_valid = true); ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false); typedef void result_type; }; #endif // !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) #if !defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_TRAIT) template struct query_static_constexpr_member< asio::detail::promise_executor, Property, typename asio::enable_if< asio::is_convertible< Property, asio::execution::blocking_t >::value >::type > { ASIO_STATIC_CONSTEXPR(bool, is_valid = true); ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true); typedef Blocking result_type; static ASIO_CONSTEXPR result_type value() ASIO_NOEXCEPT { return Blocking(); } }; #endif // !defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_TRAIT) #if !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT) template struct require_member< asio::detail::promise_executor, execution::blocking_t::possibly_t > { ASIO_STATIC_CONSTEXPR(bool, is_valid = true); ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true); typedef asio::detail::promise_executor result_type; }; template struct require_member< asio::detail::promise_executor, execution::blocking_t::never_t > { ASIO_STATIC_CONSTEXPR(bool, is_valid = true); ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true); typedef asio::detail::promise_executor result_type; }; #endif // !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT) } // namespace traits #endif // !defined(GENERATING_DOCUMENTATION) } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_IMPL_USE_FUTURE_HPP