Lely core libraries  2.2.5
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 restart() noexcept { co_restart(this); }
55 
57  bool is_ready() const noexcept { return ctx_.label == -1; }
58 
60  bool is_parent() const noexcept { return !is_child(); }
61 
63  bool is_child() const noexcept { return ctx_.label < -1; }
64 
65  private:
66  co_ctx_t ctx_ CO_CTX_INIT;
67 
68  friend co_ctx_t* to_co_ctx(Coroutine* coro) noexcept { return &coro->ctx_; }
69  friend co_ctx_t* to_co_ctx(Coroutine& coro) noexcept { return &coro.ctx_; }
70 };
71 
72 inline co_ctx_t* to_co_ctx(co_ctx_t* ctx) noexcept { return ctx; }
73 
74 } // namespace util
75 } // namespace lely
76 
77 #endif // !LELY_UTIL_COROUTINE_HPP_
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
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:54
bool is_parent() const noexcept
Returns true if the stackless coroutine is the parent of a fork.
Definition: coroutine.hpp:60
bool is_child() const noexcept
Returns true if the stackless coroutine is the child of a fork.
Definition: coroutine.hpp:63
Definition: buf.hpp:32
bool is_ready() const noexcept
Returns true if the stackless coroutine has finished.
Definition: coroutine.hpp:57