|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include <memory>
-
- #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_;
- };
- constexpr rf_task_t<> rf_task;
-
- namespace detail {
-
- template<typename T>
- class promise_handler
- {
- public:
- using result_type_t = T;
- using state_type = resumef::state_t<result_type_t>;
-
- template<typename Allocator>
- promise_handler(const rf_task_t<Allocator> &)
- : state_(state_type::typename _Alloc_state<state_type>(true))
- {
- }
-
- 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)));
- }
- }
-
- resumef::counted_ptr<state_type> state_;
- };
-
- template<>
- class promise_handler<void>
- {
- public:
- using result_type_t = void;
- using state_type = resumef::state_t<result_type_t>;
-
- template<typename Allocator>
- promise_handler(const rf_task_t<Allocator> &)
- : state_(state_type::typename _Alloc_state<state_type>(true))
- {
- }
-
- 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)));
- }
- }
-
- resumef::counted_ptr<state_type> state_;
- };
-
- } // namespace detail
-
- template<typename T>
- class async_result<detail::promise_handler<T> >
- {
- public:
- typedef resumef::future_t<T> type;
-
- explicit async_result(detail::promise_handler<T> & h)
- : task_(std::move(h.state_))
- { }
-
- resumef::future_t<T> get() { return std::move(task_); }
- private:
- resumef::future_t<T> task_;
- };
-
- // 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"
|