Debian Codegraph @

yo/coroutines/demo/readbinary/reader/reader.h

raw
1 #ifndef INCLUDED_READER_
2 #define INCLUDED_READER_
3
4 // use return_value if an expression is passed to co_return
5
6 //interface
7 #include <iostream>
8 #include <optional>
9
10 #include "../../promisebase/promisebase.h"
11
12 struct Reader
13 {
14 using ValueType = std::optional<float>;
15
16 private:
17 class State: public PromiseBase<Reader, State>
18 {
19 ValueType d_value;
20
21 public:
22 std::suspend_always yield_value(float value);
23 void return_void();
24 ValueType const &value() const;
25 };
26
27 std::coroutine_handle<State> d_handle;
28
29 public:
30 using promise_type = State;
31
32 explicit Reader(std::coroutine_handle<State> handle);
33 ~Reader();
34
35 ValueType const &next();
36 };
37 //=
38
39 // OK (declaration) std::suspend_always await_transform(float value);
40
41 // OK inline std::suspend_always Reader::State::await_transform(float value)
42 // OK {
43 // OK d_value = value;
44 // OK return {};
45 // OK }
46
47
48 //defs
49 inline std::suspend_always Reader::State::yield_value(float value)
50 {
51 d_value = value;
52 return {};
53 }
54
55 inline void Reader::State::return_void()
56 {
57 d_value = ValueType{};
58 }
59
60 inline Reader::ValueType const &Reader::State::value() const
61 {
62 return d_value;
63 }
64
65 inline Reader::Reader(std::coroutine_handle<State> handle)
66 :
67 d_handle(handle)
68 {}
69
70 inline Reader::~Reader()
71 {
72 if (d_handle)
73 d_handle.destroy();
74 }
75 //=
76
77 #endif