libcoro  1.0
Coroutine support library for C++20
prepared_coro.h
1 #pragma once
2 
3 #include <coroutine>
4 #include "trace.h"
5 
6 namespace coro {
7 
8 
10 
16 public:
17 
19  prepared_coro() = default;
21  prepared_coro(std::coroutine_handle<> h):_h(h) {}
22  template<typename X>
23  prepared_coro(std::coroutine_handle<X> h):_h(h) {}
25  prepared_coro(prepared_coro &&other):_h(other.release()) {}
28  if (this != &other) {
29  (*this)();
30  _h = other.release();
31  }
32  return *this;
33  }
34 
37  if (_h) {
38  trace::resume(_h);
39  }
40  }
41 
43  std::coroutine_handle<> release() {
44  auto h = _h;
45  _h = {};
46  return h;
47  }
48 
50 
53  std::coroutine_handle<> symmetric_transfer() {
54  if (_h) return release();
55  else return std::noop_coroutine();
56  }
57 
59  void operator()() {
60  auto h = release();
61  if (h) {
62  trace::resume(h);
63  }
64  }
65 
67  operator bool() const {return _h != nullptr;}
68 
69  bool done() const {return _h.done();}
70 
71 protected:
72  std::coroutine_handle<> _h;
73 
74 };
75 
76 
77 }
78 
79 
80 
81 
82 
prepared_coro(prepared_coro &&other)
move
Definition: prepared_coro.h:25
std::coroutine_handle symmetric_transfer()
release handle to be used in function await_suspend()
Definition: prepared_coro.h:53
prepared_coro & operator=(prepared_coro &&other)
move assign
Definition: prepared_coro.h:27
prepared_coro(std::coroutine_handle<> h)
construct with handle
Definition: prepared_coro.h:21
prepared_coro()=default
construct uninitialized object
~prepared_coro()
destructor - resumes coroutine if still in prepared state
Definition: prepared_coro.h:36
std::coroutine_handle release()
release handle
Definition: prepared_coro.h:43
void operator()()
object can be used as callable (you can pass it to differen thread)
Definition: prepared_coro.h:59
contains prepared coroutine (prepared to run)
Definition: prepared_coro.h:15
void resume(std::coroutine_handle<> h) noexcept
Record resumption of an coroutine.
Definition: trace.h:382
main namespace
Definition: aggregator.h:8