1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-01 15:57:07 +08:00
librf/asio/asio_task_1.12.0.inl

197 lines
5.5 KiB
Plaintext
Raw Normal View History

#include <memory>
2019-04-25 11:35:23 +08:00
#include "asio/detail/push_options.hpp"
namespace asio {
2020-03-30 23:01:07 +08:00
/**
2021-11-01 17:59:08 +08:00
* @brief 用于指示asio相关异步函数返回librf::future_t<>的类型,从而变成支持 librf 的协程函数。
2020-03-30 23:01:07 +08:00
*/
2019-04-25 11:35:23 +08:00
template <typename Executor = executor>
struct rf_task_t
{
ASIO_CONSTEXPR rf_task_t() {}
};
2020-03-30 23:01:07 +08:00
/**
2021-11-01 17:59:08 +08:00
* @brief 用于指示asio相关异步函数返回librf::future_t<>的常量,从而变成支持 librf 的协程函数。
2020-03-30 23:01:07 +08:00
*/
2019-04-25 11:35:23 +08:00
constexpr rf_task_t<> rf_task;
2019-04-25 11:42:51 +08:00
namespace librf {
2019-04-25 11:35:23 +08:00
template <typename Executor, typename T>
struct promise_handler_base
2019-04-25 11:35:23 +08:00
{
public:
typedef T result_type;
2021-11-01 17:59:08 +08:00
typedef librf::state_t<result_type> state_type;
2019-04-25 11:35:23 +08:00
promise_handler_base()
2021-11-01 17:59:08 +08:00
: state_(librf::state_future_t::_Alloc_state<state_type>(true))
{
}
2019-04-25 11:35:23 +08:00
2021-11-01 17:59:08 +08:00
librf::counted_ptr<state_type> 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;
2019-04-25 11:35:23 +08:00
};
template <typename, typename...>
2019-04-25 11:42:51 +08:00
struct promise_handler;
2019-04-25 11:35:23 +08:00
template <typename Executor>
struct promise_handler<Executor, void> : public promise_handler_base<Executor, void>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, void>::promise_handler_base;
2019-04-25 11:35:23 +08:00
void operator()() const
{
2020-02-15 13:32:14 +08:00
this->state_->set_value();
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor>
struct promise_handler<Executor, asio::error_code> : public promise_handler_base<Executor, void>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, void>::promise_handler_base;
2019-04-25 11:35:23 +08:00
void operator()(const asio::error_code& ec) const
{
if (!ec)
2020-02-15 13:32:14 +08:00
this->state_->set_value();
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor>
struct promise_handler<Executor, std::exception_ptr> : public promise_handler_base<Executor, void>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, void>::promise_handler_base;
2019-04-25 11:35:23 +08:00
void operator()(std::exception_ptr ex) const
{
if (!ex)
2020-02-15 13:32:14 +08:00
this->state_->set_value();
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(ex);
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename T>
struct promise_handler<Executor, T> : public promise_handler_base<Executor, T>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, T>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename Arg>
void operator()(Arg&& arg) const
{
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::forward<Arg>(arg));
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename T>
struct promise_handler<Executor, asio::error_code, T> : public promise_handler_base<Executor, T>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, T>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename Arg>
void operator()(const asio::error_code& ec, Arg&& arg) const
{
if (!ec)
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::forward<Arg>(arg));
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename T>
struct promise_handler<Executor, std::exception_ptr, T> : public promise_handler_base<Executor, T>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, T>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename Arg>
void operator()(std::exception_ptr ex, Arg&& arg) const
{
if (!ex)
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::forward<Arg>(arg));
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(ex);
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename... Ts>
struct promise_handler : public promise_handler_base<Executor, std::tuple<Ts...>>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, std::tuple<Ts...>>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename... Args>
void operator()(Args&&... args) const
{
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::make_tuple(std::forward<Args>(args)...));
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename... Ts>
struct promise_handler<Executor, asio::error_code, Ts...> : public promise_handler_base<Executor, std::tuple<Ts...>>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, std::tuple<Ts...>>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename... Args>
void operator()(const asio::error_code& ec, Args&&... args) const
{
if (!ec)
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::make_tuple(std::forward<Args>(args)...));
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
2019-04-25 11:35:23 +08:00
}
};
template <typename Executor, typename... Ts>
struct promise_handler<Executor, std::exception_ptr, Ts...> : public promise_handler_base<Executor, std::tuple<Ts...>>
2019-04-25 11:35:23 +08:00
{
using promise_handler_base<Executor, std::tuple<Ts...>>::promise_handler_base;
2019-04-25 11:35:23 +08:00
template <typename... Args>
void operator()(std::exception_ptr ex, Args&&... args) const
{
if (!ex)
2020-02-15 13:32:14 +08:00
this->state_->set_value(std::make_tuple(std::forward<Args>(args)...));
2019-04-25 11:35:23 +08:00
else
2020-02-15 13:32:14 +08:00
this->state_->set_exception(ex);
2019-04-25 11:35:23 +08:00
}
};
2019-04-25 11:42:51 +08:00
} // namespace librf
2019-04-25 11:35:23 +08:00
template <typename Executor, typename R, typename... Args>
class async_result<rf_task_t<Executor>, R(Args...)>
{
public:
2019-04-25 11:42:51 +08:00
typedef librf::promise_handler<Executor, Args...> handler_type;
2019-04-25 11:35:23 +08:00
typedef typename handler_type::result_type result_type;
2021-11-01 17:59:08 +08:00
typedef librf::future_t<result_type> return_type;
2019-04-25 11:35:23 +08:00
template <typename Initiation, typename... InitArgs>
static return_type initiate(ASIO_MOVE_ARG(Initiation) initiation,
rf_task_t<Executor>, 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"