Debian Codegraph @

yo/coroutines/demo/fibocoro/fibo/fibo.h

raw
1 #ifndef INCLUDED_FIBO_
2 #define INCLUDED_FIBO_
3
4 #include <iostream>
5
6 #include <cstddef>
7 #include <coroutine>
8
9 class Fibo
10 {
11 class State
12 {
13 size_t d_value;
14
15 public:
16 Fibo get_return_object();
17
18 std::suspend_always yield_value(size_t value);
19
20 static std::suspend_always initial_suspend();
21 static std::suspend_always final_suspend() noexcept;
22 static void unhandled_exception();
23
24 size_t value() const;
25 };
26
27 std::coroutine_handle<State> d_handle;
28
29 public:
30 using promise_type = State;
31
32 explicit Fibo(std::coroutine_handle<State> handle);
33 ~Fibo();
34
35 size_t next();
36 };
37
38 inline std::suspend_always Fibo::State::initial_suspend()
39 {
40 return {};
41 }
42
43 inline std::suspend_always Fibo::State::final_suspend() noexcept
44 {
45 return {};
46 }
47
48 inline void Fibo::State::unhandled_exception()
49 {}
50
51 inline Fibo Fibo::State::get_return_object()
52 {
53 return Fibo{ std::coroutine_handle<State>::from_promise(*this) };
54 }
55
56 inline std::suspend_always Fibo::State::yield_value(size_t value)
57 {
58 d_value = value;
59 return {};
60 }
61
62 inline size_t Fibo::State::value() const
63 {
64 return d_value;
65 }
66
67 //------------------------------------------------------------
68
69 inline Fibo::Fibo(std::coroutine_handle<State> handle)
70 :
71 d_handle(handle)
72 {}
73
74 inline Fibo::~Fibo()
75 {
76 if (d_handle)
77 d_handle.destroy();
78 }
79
80
81 #endif