| 1 |
#ifndef INCLUDED_RECURSIVEGENERATOR_H_ |
| 2 |
#define INCLUDED_RECURSIVEGENERATOR_H_ |
| 3 |
|
| 4 |
#include "promisebase.h" |
| 5 |
#include "recursiveiterator.h" |
| 6 |
|
| 7 |
template<typename T> |
| 8 |
class [[nodiscard]] RecursiveGenerator |
| 9 |
{ |
| 10 |
class Promise: public PromiseBase // final |
| 11 |
{ |
| 12 |
using Handle = std::coroutine_handle<Promise>; |
| 13 |
|
| 14 |
class Awaitable |
| 15 |
{ |
| 16 |
Promise *d_childPromise; |
| 17 |
|
| 18 |
public: |
| 19 |
Awaitable(Promise *childPromise); |
| 20 |
bool await_ready() noexcept; |
| 21 |
void await_resume(); |
| 22 |
static void await_suspend(Handle) noexcept; |
| 23 |
}; |
| 24 |
|
| 25 |
// If this is the promise of the root Generator then this field |
| 26 |
// is a pointer to the leaf promise. |
| 27 |
// For non-root Generators this is a pointer to the parent |
| 28 |
// promise: |
| 29 |
Promise *d_parentOrLeaf; |
| 30 |
T *d_value = 0; |
| 31 |
std::exception_ptr d_exception = 0; |
| 32 |
Promise *d_root; |
| 33 |
|
| 34 |
public: |
| 35 |
Promise() noexcept; |
| 36 |
Promise(Promise const &) = delete; |
| 37 |
|
| 38 |
auto get_return_object() noexcept; |
| 39 |
void unhandled_exception() noexcept; |
| 40 |
|
| 41 |
std::suspend_always yield_value(T &&value) noexcept; |
| 42 |
std::suspend_always yield_value(T &value) noexcept; |
| 43 |
|
| 44 |
auto yield_value(RecursiveGenerator &generator) noexcept; |
| 45 |
auto yield_value(RecursiveGenerator &&generator) noexcept; |
| 46 |
|
| 47 |
void destroy() noexcept; |
| 48 |
void throw_if_exception(); |
| 49 |
bool is_complete() noexcept; |
| 50 |
T &value() noexcept; |
| 51 |
void pull() noexcept; |
| 52 |
|
| 53 |
// no 'co_await' inside the RecursiveGenerator coroutine. |
| 54 |
template<typename U> |
| 55 |
std::suspend_never await_transform(U&& value) = delete; |
| 56 |
|
| 57 |
private: |
| 58 |
void resume() noexcept; |
| 59 |
}; |
| 60 |
|
| 61 |
Promise *d_promise; |
| 62 |
|
| 63 |
public: |
| 64 |
using promise_type = Promise; |
| 65 |
|
| 66 |
RecursiveGenerator() noexcept = default; |
| 67 |
RecursiveGenerator(Promise &promise) noexcept; |
| 68 |
RecursiveGenerator(RecursiveGenerator &&tmp) noexcept; |
| 69 |
|
| 70 |
~RecursiveGenerator(); |
| 71 |
|
| 72 |
RecursiveGenerator &operator=(RecursiveGenerator &&other) |
| 73 |
noexcept; |
| 74 |
|
| 75 |
RecGenIterator<T, Promise> begin(); |
| 76 |
RecGenIterator<T, Promise> end() noexcept; |
| 77 |
|
| 78 |
void swap(RecursiveGenerator &other) noexcept; |
| 79 |
}; |
| 80 |
|
| 81 |
// Note: When applying fmap operator to a RecursiveGenerator we just yield |
| 82 |
// a non-recursive Generator since we generally won't be using the result |
| 83 |
// in a recursive context. |
| 84 |
template<typename FUNC, typename T> |
| 85 |
Generator< |
| 86 |
std::invoke_result_t< |
| 87 |
FUNC &, typename RecursiveGenerator<T>::RecGenIterator::reference |
| 88 |
> |
| 89 |
> fmap(FUNC func, RecursiveGenerator<T> source); |
| 90 |
|
| 91 |
#include "recursivepromise.imp" |
| 92 |
#include "recursivegenerator.imp" |
| 93 |
#include "recursiveiterator.imp" |
| 94 |
|
| 95 |
#endif |