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 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#ifndef INCLUDED_FIBO_
#define INCLUDED_FIBO_
#include <iostream>
#include <cstddef>
#include <coroutine>
class Fibo
{
class State
{
size_t d_value;
public:
Fibo get_return_object();
std::suspend_always yield_value(size_t value);
static std::suspend_always initial_suspend();
static std::suspend_always final_suspend() noexcept;
static void unhandled_exception();
size_t value() const;
};
std::coroutine_handle<State> d_handle;
public:
using promise_type = State;
explicit Fibo(std::coroutine_handle<State> handle);
~Fibo();
size_t next();
};
inline std::suspend_always Fibo::State::initial_suspend()
{
return {};
}
inline std::suspend_always Fibo::State::final_suspend() noexcept
{
return {};
}
inline void Fibo::State::unhandled_exception()
{}
inline Fibo Fibo::State::get_return_object()
{
return Fibo{ std::coroutine_handle<State>::from_promise(*this) };
}
inline std::suspend_always Fibo::State::yield_value(size_t value)
{
d_value = value;
return {};
}
inline size_t Fibo::State::value() const
{
return d_value;
}
//------------------------------------------------------------
inline Fibo::Fibo(std::coroutine_handle<State> handle)
:
d_handle(handle)
{}
inline Fibo::~Fibo()
{
if (d_handle)
d_handle.destroy();
}
#endif
|