| 1 |
#ifndef INCLUDED_RECURSIVE_ |
| 2 |
#define INCLUDED_RECURSIVE_ |
| 3 |
|
| 4 |
#include "../../promisebase/promisebase.h" |
| 5 |
|
| 6 |
class Recursive |
| 7 |
{ |
| 8 |
class State: public PromiseBase<Recursive, State> |
| 9 |
{ |
| 10 |
using Handle = std::coroutine_handle<State>; |
| 11 |
|
| 12 |
size_t d_level; |
| 13 |
size_t d_value; |
| 14 |
|
| 15 |
public: |
| 16 |
State(size_t level); |
| 17 |
static std::suspend_always initial_suspend(); |
| 18 |
std::suspend_always yield_value(size_t value); |
| 19 |
size_t value() const; |
| 20 |
size_t level() const; |
| 21 |
}; |
| 22 |
|
| 23 |
public: |
| 24 |
using Handle = std::coroutine_handle<State>; |
| 25 |
|
| 26 |
private: |
| 27 |
Handle d_handle; |
| 28 |
|
| 29 |
public: |
| 30 |
using promise_type = State; |
| 31 |
|
| 32 |
//iter |
| 33 |
class Iterator |
| 34 |
{ |
| 35 |
friend bool operator==(Iterator const &lhs, Iterator const &rhs); |
| 36 |
friend class Recursive; |
| 37 |
|
| 38 |
Handle d_handle; |
| 39 |
|
| 40 |
public: |
| 41 |
Iterator &operator++(); |
| 42 |
size_t operator*() const; |
| 43 |
|
| 44 |
private: |
| 45 |
Iterator(Handle handle); |
| 46 |
}; |
| 47 |
//= |
| 48 |
|
| 49 |
explicit Recursive(std::coroutine_handle<State> handle); |
| 50 |
~Recursive(); |
| 51 |
|
| 52 |
bool next(size_t *value); |
| 53 |
void resume(); |
| 54 |
|
| 55 |
Iterator begin(); |
| 56 |
Iterator end(); |
| 57 |
}; |
| 58 |
|
| 59 |
inline bool operator==(Recursive::Iterator const &lhs, |
| 60 |
Recursive::Iterator const &rhs) |
| 61 |
{ |
| 62 |
return lhs.d_handle.address() == rhs.d_handle.address(); |
| 63 |
} |
| 64 |
|
| 65 |
inline Recursive::Recursive(std::coroutine_handle<State> handle) |
| 66 |
: |
| 67 |
d_handle(handle) |
| 68 |
{} |
| 69 |
|
| 70 |
#endif |