Debian Codegraph @

yo/coroutines/demo/readbinary/writer/writer.h

raw
1 #ifndef INCLUDED_WRITER_
2 #define INCLUDED_WRITER_
3
4 // use return_value if an expression is passed to co_return
5
6 #include <iostream>
7 #include <optional>
8
9 #include "../../promisebase/promisebase.h"
10
11 //writer
12 struct Writer
13 {
14 using ValueType = std::optional<std::string>;
15
16 private:
17 class State: public PromiseBase<Writer, State>
18 {
19 ValueType d_value;
20
21 public:
22 std::suspend_always yield_value(ValueType &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 Writer(std::coroutine_handle<State> handle);
33 ~Writer();
34
35 ValueType const &next();
36 };
37
38 inline std::suspend_always Writer::State::yield_value(ValueType &value)
39 {
40 d_value = std::move(value);
41 return {};
42 }
43 //=
44
45 inline void Writer::State::return_void()
46 {
47 d_value = ValueType{};
48 }
49
50 inline Writer::ValueType const &Writer::State::value() const
51 {
52 return d_value;
53 }
54
55 inline Writer::Writer(std::coroutine_handle<State> handle)
56 :
57 d_handle(handle)
58 {}
59
60 inline Writer::~Writer()
61 {
62 if (d_handle)
63 d_handle.destroy();
64 }
65
66
67 #endif