Debian Codegraph @

yo/coroutines/demo/fibocoroawaiter/fibo/fibo.h

raw symbols SCIP
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 const *d_cycle;
14 size_t d_value;
15
16 public:
17 Fibo get_return_object();
18
19 void setCycle(size_t const &cycle);
20 std::suspend_always yield_value(size_t value);
21
22 static std::suspend_always initial_suspend();
23 static std::suspend_always final_suspend() noexcept;
24 static void unhandled_exception();
25
26 size_t value() const;
27 };
28
29 std::coroutine_handle<State> d_handle;
30
31 public:
32 using promise_type = State;
33 using Handle = decltype(d_handle);
34
35 explicit Fibo(std::coroutine_handle<State> handle);
36 ~Fibo();
37
38 size_t next();
39 };
40
41 //cycle
42 inline void Fibo::State::setCycle(size_t const &cycle)
43 {
44 d_cycle = &cycle;
45 }
46 //=
47
48 inline std::suspend_always Fibo::State::initial_suspend()
49 {
50 return {};
51 }
52
53 inline std::suspend_always Fibo::State::final_suspend() noexcept
54 {
55 return {};
56 }
57
58 inline void Fibo::State::unhandled_exception()
59 {}
60
61 inline Fibo Fibo::State::get_return_object()
62 {
63 return Fibo{ std::coroutine_handle<State>::from_promise(*this) };
64 }
65
66 inline size_t Fibo::State::value() const
67 {
68 return d_value;
69 }
70
71 //------------------------------------------------------------
72
73 inline Fibo::Fibo(std::coroutine_handle<State> handle)
74 :
75 d_handle(handle)
76 {}
77
78 inline Fibo::~Fibo()
79 {
80 if (d_handle)
81 d_handle.destroy();
82 }
83
84
85 #endif