基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asio_task_1.10.0.inl 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <memory>
  2. #include "asio/detail/push_options.hpp"
  3. namespace asio {
  4. template<typename Allocator = std::allocator<int> >
  5. class rf_task_t
  6. {
  7. public:
  8. typedef Allocator allocator_type;
  9. constexpr rf_task_t() {}
  10. explicit rf_task_t(const Allocator& allocator) : allocator_(allocator) {}
  11. template<typename OtherAllocator>
  12. rf_task_t<OtherAllocator> operator[](const OtherAllocator& allocator) const {
  13. return rf_task_t<OtherAllocator>(allocator);
  14. }
  15. allocator_type get_allocator() const { return allocator_; }
  16. private:
  17. Allocator allocator_;
  18. };
  19. constexpr rf_task_t<> rf_task;
  20. namespace detail {
  21. template<typename T>
  22. class promise_handler
  23. {
  24. public:
  25. using result_type_t = T;
  26. using state_type = librf::state_t<result_type_t>;
  27. template<typename Allocator>
  28. promise_handler(const rf_task_t<Allocator> &)
  29. : state_(state_type::typename _Alloc_state<state_type>(true))
  30. {
  31. }
  32. void operator()(T t) const
  33. {
  34. state_->set_value(std::move(t));
  35. }
  36. void operator()(const asio::error_code& ec, T t) const
  37. {
  38. if (!ec)
  39. {
  40. state_->set_value(std::move(t));
  41. }
  42. else
  43. {
  44. state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
  45. }
  46. }
  47. librf::counted_ptr<state_type> state_;
  48. };
  49. template<>
  50. class promise_handler<void>
  51. {
  52. public:
  53. using result_type_t = void;
  54. using state_type = librf::state_t<result_type_t>;
  55. template<typename Allocator>
  56. promise_handler(const rf_task_t<Allocator> &)
  57. : state_(state_type::typename _Alloc_state<state_type>(true))
  58. {
  59. }
  60. void operator()() const
  61. {
  62. state_->set_value();
  63. }
  64. void operator()(const asio::error_code& ec) const
  65. {
  66. if (!ec)
  67. {
  68. state_->set_value();
  69. }
  70. else
  71. {
  72. state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
  73. }
  74. }
  75. librf::counted_ptr<state_type> state_;
  76. };
  77. } // namespace detail
  78. template<typename T>
  79. class async_result<detail::promise_handler<T> >
  80. {
  81. public:
  82. typedef librf::future_t<T> type;
  83. explicit async_result(detail::promise_handler<T> & h)
  84. : task_(std::move(h.state_))
  85. { }
  86. librf::future_t<T> get() { return std::move(task_); }
  87. private:
  88. librf::future_t<T> task_;
  89. };
  90. // Handler type specialisation for zero arg.
  91. template<typename Allocator, typename ReturnType>
  92. struct handler_type<rf_task_t<Allocator>, ReturnType()> {
  93. typedef detail::promise_handler<void> type;
  94. };
  95. // Handler type specialisation for one arg.
  96. template<typename Allocator, typename ReturnType, typename Arg1>
  97. struct handler_type<rf_task_t<Allocator>, ReturnType(Arg1)> {
  98. typedef detail::promise_handler<Arg1> type;
  99. };
  100. // Handler type specialisation for two arg.
  101. template<typename Allocator, typename ReturnType, typename Arg2>
  102. struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code, Arg2)> {
  103. typedef detail::promise_handler<Arg2> type;
  104. };
  105. template<typename Allocator, typename ReturnType>
  106. struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code)> {
  107. typedef detail::promise_handler<void> type;
  108. };
  109. } // namespace asio
  110. #include "asio/detail/pop_options.hpp"