Lely core libraries  2.3.4
coroutine.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_UTIL_COROUTINE_HPP_
25 #define LELY_UTIL_COROUTINE_HPP_
26 
27 #include <lely/util/coroutine.h>
28 
29 namespace lely {
30 namespace util {
31 
32 #undef co_restart
33 #define co_restart(ctx) co_restart__(to_co_ctx(ctx))
34 
35 #undef co_is_ready
36 #define co_is_ready(ctx) co_is_ready__(to_co_ctx(ctx))
37 
38 #undef co_reenter
39 #define co_reenter(ctx) co_reenter__(to_co_ctx(ctx))
40 
48 class Coroutine {
49  public:
54  void
55  restart() noexcept {
56  co_restart(this);
57  }
58 
60  bool
61  is_ready() const noexcept {
62  return ctx_.label == -1;
63  }
64 
66  bool
67  is_parent() const noexcept {
68  return !is_child();
69  }
70 
72  bool
73  is_child() const noexcept {
74  return ctx_.label < -1;
75  }
76 
77  private:
78  co_ctx_t ctx_ CO_CTX_INIT;
79 
80  friend co_ctx_t*
81  to_co_ctx(Coroutine* coro) noexcept {
82  return &coro->ctx_;
83  }
84  friend co_ctx_t*
85  to_co_ctx(Coroutine& coro) noexcept {
86  return &coro.ctx_;
87  }
88 };
89 
90 inline co_ctx_t*
91 to_co_ctx(co_ctx_t* ctx) noexcept {
92  return ctx;
93 }
94 
95 } // namespace util
96 } // namespace lely
97 
98 #endif // !LELY_UTIL_COROUTINE_HPP_
The parent class for function objects used as stackless coroutines.
Definition: coroutine.hpp:48
void restart() noexcept
Resets the stackless coroutine so the next invocation starts at the beginning.
Definition: coroutine.hpp:55
bool is_ready() const noexcept
Returns true if the stackless coroutine has finished.
Definition: coroutine.hpp:61
bool is_child() const noexcept
Returns true if the stackless coroutine is the child of a fork.
Definition: coroutine.hpp:73
bool is_parent() const noexcept
Returns true if the stackless coroutine is the parent of a fork.
Definition: coroutine.hpp:67
This header file is part of the utilities libraru; it contains a stackless coroutine implementation.
#define co_restart(ctx)
Resets a stackless coroutine so the next invocation starts at the beginning.
Definition: coroutine.h:64
The type holding the context (i.e., program counter) of a stackless coroutine.
Definition: coroutine.h:53