#include #include "asio/detail/push_options.hpp" namespace asio { /** * @brief 用于指示asio相关异步函数,返回librf::future_t<>的类型,从而变成支持 librf 的协程函数。 */ template struct rf_task_t { ASIO_CONSTEXPR rf_task_t() {} }; /** * @brief 用于指示asio相关异步函数,返回librf::future_t<>的常量,从而变成支持 librf 的协程函数。 */ constexpr rf_task_t<> rf_task; namespace librf { template struct promise_handler_base { public: typedef T result_type; typedef librf::state_t state_type; promise_handler_base() : state_(librf::state_future_t::_Alloc_state(true)) { } librf::counted_ptr state_; promise_handler_base(promise_handler_base &&) = default; promise_handler_base(const promise_handler_base &) = default; promise_handler_base & operator = (promise_handler_base &&) = default; promise_handler_base & operator = (const promise_handler_base &) = default; }; template struct promise_handler; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; void operator()() const { this->state_->set_value(); } }; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; void operator()(const asio::error_code& ec) const { if (!ec) this->state_->set_value(); else this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec))); } }; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; void operator()(std::exception_ptr ex) const { if (!ex) this->state_->set_value(); else this->state_->set_exception(ex); } }; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; template void operator()(Arg&& arg) const { this->state_->set_value(std::forward(arg)); } }; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; template void operator()(const asio::error_code& ec, Arg&& arg) const { if (!ec) this->state_->set_value(std::forward(arg)); else this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec))); } }; template struct promise_handler : public promise_handler_base { using promise_handler_base::promise_handler_base; template void operator()(std::exception_ptr ex, Arg&& arg) const { if (!ex) this->state_->set_value(std::forward(arg)); else this->state_->set_exception(ex); } }; template struct promise_handler : public promise_handler_base> { using promise_handler_base>::promise_handler_base; template void operator()(Args&&... args) const { this->state_->set_value(std::make_tuple(std::forward(args)...)); } }; template struct promise_handler : public promise_handler_base> { using promise_handler_base>::promise_handler_base; template void operator()(const asio::error_code& ec, Args&&... args) const { if (!ec) this->state_->set_value(std::make_tuple(std::forward(args)...)); else this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec))); } }; template struct promise_handler : public promise_handler_base> { using promise_handler_base>::promise_handler_base; template void operator()(std::exception_ptr ex, Args&&... args) const { if (!ex) this->state_->set_value(std::make_tuple(std::forward(args)...)); else this->state_->set_exception(ex); } }; } // namespace librf template class async_result, R(Args...)> { public: typedef librf::promise_handler handler_type; typedef typename handler_type::result_type result_type; typedef librf::future_t return_type; template static return_type initiate(ASIO_MOVE_ARG(Initiation) initiation, rf_task_t, ASIO_MOVE_ARG(InitArgs)... args) { handler_type handler{}; return_type future{ handler.state_ }; std::move(initiation)(std::move(handler), std::move(args)...); return std::move(future); } }; } // namespace asio #include "asio/detail/pop_options.hpp"