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