librf
awaitable.h
1 #pragma once
2 
3 namespace resumef
4 {
5 
11  template<class _Ty>
13  {
14  using value_type = _Ty;
17  using lock_type = typename state_type::lock_type;
18  using _Alloc_char = typename state_type::_Alloc_char;
19 
20  awaitable_impl_t() {}
21  awaitable_impl_t(const awaitable_impl_t&) = default;
23 
24  awaitable_impl_t& operator = (const awaitable_impl_t&) = default;
25  awaitable_impl_t& operator = (awaitable_impl_t&&) = default;
26 
31  void set_exception(std::exception_ptr e) const
32  {
33  this->_state->set_exception(std::move(e));
34  this->_state = nullptr;
35  }
36 
40  template<class _Exp>
41  void throw_exception(_Exp e) const
42  {
43  set_exception(std::make_exception_ptr(std::move(e)));
44  }
45 
50  {
51  return future_type{ this->_state };
52  }
53 
57  mutable counted_ptr<state_type> _state = state_future_t::_Alloc_state<state_type>(true);
58  };
59 
76  template<class _Ty>
77  struct [[nodiscard]] awaitable_t : public awaitable_impl_t<_Ty>
78  {
79  using typename awaitable_impl_t<_Ty>::value_type;
81 
88  template<class U>
89  void set_value(U&& value) const
90  {
91  this->_state->set_value(std::forward<U>(value));
92  this->_state = nullptr;
93  }
94  };
95 
96 #ifndef DOXYGEN_SKIP_PROPERTY
97  template<class _Ty>
98  struct [[nodiscard]] awaitable_t<_Ty&> : public awaitable_impl_t<_Ty&>
99  {
100  using typename awaitable_impl_t<_Ty&>::value_type;
101  using awaitable_impl_t<_Ty&>::awaitable_impl_t;
102 
103  void set_value(_Ty& value) const
104  {
105  this->_state->set_value(value);
106  this->_state = nullptr;
107  }
108  };
109 
110  template<>
111  struct [[nodiscard]] awaitable_t<void> : public awaitable_impl_t<void>
112  {
113  using awaitable_impl_t<void>::awaitable_impl_t;
114 
115  void set_value() const
116  {
117  this->_state->set_value();
118  this->_state = nullptr;
119  }
120  };
121 #endif //DOXYGEN_SKIP_PROPERTY
122 }
resumef::awaitable_impl_t::get_future
future_type get_future() noexcept
获得与之关联的future_t<>对象,作为可等待函数(awaitable function)的返回值。
Definition: awaitable.h:49
resumef::future_t
用于resumef协程的返回值。
Definition: future.h:14
resumef::awaitable_t
用于包装‘异步函数’为‘可等待函数(awaitable function)’。
Definition: awaitable.h:77
resumef::awaitable_impl_t::throw_exception
void throw_exception(_Exp e) const
在协程内部,重新抛出之前设置的异常。
Definition: awaitable.h:41
resumef::awaitable_impl_t::set_exception
void set_exception(std::exception_ptr e) const
发生了异常后,设置异常。
Definition: awaitable.h:31
resumef::counted_ptr< state_type >
resumef::spinlock
一个自旋锁实现。
Definition: spinlock.h:14
resumef::awaitable_impl_t
awaitable_t<>的公共实现部分,用于减少awaitable_t<>的重复代码。
Definition: awaitable.h:12
resumef::awaitable_t::set_value
void set_value(U &&value) const
设置可等待函数的返回值。
Definition: awaitable.h:89
resumef::state_t
专用于future_t<>的state类。
Definition: state.h:217
resumef::awaitable_impl_t::_state
counted_ptr< state_type > _state
管理的state_t<>对象。
Definition: awaitable.h:57