2020-05-09 11:00:46 +08:00
|
|
|
#include <memory>
|
2019-04-25 11:35:23 +08:00
|
|
|
|
|
|
|
#include "asio/detail/push_options.hpp"
|
|
|
|
|
|
|
|
namespace asio {
|
|
|
|
|
|
|
|
template<typename Allocator = std::allocator<int> >
|
|
|
|
class rf_task_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Allocator allocator_type;
|
|
|
|
constexpr rf_task_t() {}
|
|
|
|
explicit rf_task_t(const Allocator& allocator) : allocator_(allocator) {}
|
|
|
|
|
|
|
|
template<typename OtherAllocator>
|
|
|
|
rf_task_t<OtherAllocator> operator[](const OtherAllocator& allocator) const {
|
|
|
|
return rf_task_t<OtherAllocator>(allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator_type get_allocator() const { return allocator_; }
|
|
|
|
private:
|
|
|
|
Allocator allocator_;
|
|
|
|
};
|
2020-02-19 16:29:56 +08:00
|
|
|
constexpr rf_task_t<> rf_task;
|
2019-04-25 11:35:23 +08:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class promise_handler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using result_type_t = T;
|
2021-11-01 17:59:08 +08:00
|
|
|
using state_type = librf::state_t<result_type_t>;
|
2019-04-25 11:35:23 +08:00
|
|
|
|
|
|
|
template<typename Allocator>
|
|
|
|
promise_handler(const rf_task_t<Allocator> &)
|
2020-03-23 22:11:35 +08:00
|
|
|
: state_(state_type::typename _Alloc_state<state_type>(true))
|
2019-04-25 11:35:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(T t) const
|
|
|
|
{
|
|
|
|
state_->set_value(std::move(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const asio::error_code& ec, T t) const
|
|
|
|
{
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
state_->set_value(std::move(t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
librf::counted_ptr<state_type> state_;
|
2019-04-25 11:35:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class promise_handler<void>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using result_type_t = void;
|
2021-11-01 17:59:08 +08:00
|
|
|
using state_type = librf::state_t<result_type_t>;
|
2019-04-25 11:35:23 +08:00
|
|
|
|
|
|
|
template<typename Allocator>
|
|
|
|
promise_handler(const rf_task_t<Allocator> &)
|
2020-03-23 22:11:35 +08:00
|
|
|
: state_(state_type::typename _Alloc_state<state_type>(true))
|
2019-04-25 11:35:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()() const
|
|
|
|
{
|
|
|
|
state_->set_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const asio::error_code& ec) const
|
|
|
|
{
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
state_->set_value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
librf::counted_ptr<state_type> state_;
|
2019-04-25 11:35:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class async_result<detail::promise_handler<T> >
|
|
|
|
{
|
|
|
|
public:
|
2021-11-01 17:59:08 +08:00
|
|
|
typedef librf::future_t<T> type;
|
2019-04-25 11:35:23 +08:00
|
|
|
|
|
|
|
explicit async_result(detail::promise_handler<T> & h)
|
|
|
|
: task_(std::move(h.state_))
|
|
|
|
{ }
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
librf::future_t<T> get() { return std::move(task_); }
|
2019-04-25 11:35:23 +08:00
|
|
|
private:
|
2021-11-01 17:59:08 +08:00
|
|
|
librf::future_t<T> task_;
|
2019-04-25 11:35:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Handler type specialisation for zero arg.
|
|
|
|
template<typename Allocator, typename ReturnType>
|
|
|
|
struct handler_type<rf_task_t<Allocator>, ReturnType()> {
|
|
|
|
typedef detail::promise_handler<void> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handler type specialisation for one arg.
|
|
|
|
template<typename Allocator, typename ReturnType, typename Arg1>
|
|
|
|
struct handler_type<rf_task_t<Allocator>, ReturnType(Arg1)> {
|
|
|
|
typedef detail::promise_handler<Arg1> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handler type specialisation for two arg.
|
|
|
|
template<typename Allocator, typename ReturnType, typename Arg2>
|
|
|
|
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code, Arg2)> {
|
|
|
|
typedef detail::promise_handler<Arg2> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Allocator, typename ReturnType>
|
|
|
|
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code)> {
|
|
|
|
typedef detail::promise_handler<void> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace asio
|
|
|
|
|
|
|
|
#include "asio/detail/pop_options.hpp"
|