librf
rf_task.h
1 #pragma once
2 
3 namespace resumef
4 {
5 #ifndef DOXYGEN_SKIP_PROPERTY
6 
9  struct task_base_t
10  {
11  task_base_t() = default;
12  virtual ~task_base_t();
13 
14  protected:
15  friend scheduler_t;
16  counted_ptr<state_base_t> _state;
17  };
18 #endif
19 
20  //----------------------------------------------------------------------------------------------
21 
33  template<class _Ty, class = std::void_t<>>
34  struct task_t;
35 
36 #ifndef DOXYGEN_SKIP_PROPERTY
37  template<class _Ty>
38  struct task_t<_Ty, std::void_t<traits::is_future<std::remove_reference_t<_Ty>>>> : public task_base_t
39  {
40  using future_type = std::remove_reference_t<_Ty>;
41  using value_type = typename future_type::value_type;
42  using state_type = state_t<value_type>;
43 
44  task_t() = default;
45  task_t(future_type& f)
46  {
47  initialize(f);
48  }
49  protected:
50  void initialize(future_type& f)
51  {
52  _state = f._state.get();
53  }
54  };
55 
56  template<class _Ty>
57  struct task_t<generator_t<_Ty>> : public task_base_t
58  {
59  using value_type = _Ty;
60  using future_type = generator_t<value_type>;
61  using state_type = state_generator_t;
62 
63  task_t() = default;
64  task_t(future_type& f)
65  {
66  initialize(f);
67  }
68  protected:
69  void initialize(future_type& f)
70  {
71  _state = f.detach_state();
72  }
73  };
74 
75  //----------------------------------------------------------------------------------------------
76 
77  //ctx_task_t接受的是一个'函数对象'
78  //这个'函数对象'被调用后,返回generator<_Ty>/future_t<_Ty>类型
79  //然后'函数对象'作为异步执行的上下文状态保存起来
80  template<class _Ctx>
81  struct ctx_task_t : public task_t<remove_cvref_t<decltype(std::declval<_Ctx>()())>>
82  {
83  using context_type = _Ctx;
84 
85  context_type _context;
86 
87  ctx_task_t(context_type ctx)
88  : _context(std::move(ctx))
89  {
90  decltype(auto) f = _context();
91  this->initialize(f);
92  }
93  };
94 #endif //DOXYGEN_SKIP_PROPERTY
95 }
resumef::state_t
专用于future_t<>的state类。
Definition: state.h:217
resumef::task_t
协程任务类。
Definition: rf_task.h:34