| 1 |
template <typename T> |
| 2 |
Generator<T>::Generator(Generator &&tmp) noexcept |
| 3 |
: |
| 4 |
d_handle(tmp.d_handle) |
| 5 |
{ |
| 6 |
tmp.d_handle = nullptr; |
| 7 |
} |
| 8 |
|
| 9 |
template <typename T> |
| 10 |
Generator<T>::~Generator() |
| 11 |
{ |
| 12 |
if (d_handle) |
| 13 |
d_handle.destroy(); |
| 14 |
} |
| 15 |
|
| 16 |
template <typename T> |
| 17 |
Generator<T> &Generator<T>::operator=(Generator<T> &&tmp) noexcept |
| 18 |
{ |
| 19 |
swap(tmp); |
| 20 |
return *this; |
| 21 |
} |
| 22 |
|
| 23 |
template <typename T> |
| 24 |
Iterator<T> Generator<T>::begin() |
| 25 |
{ |
| 26 |
if (d_handle) |
| 27 |
{ |
| 28 |
d_handle.resume(); |
| 29 |
if (d_handle.done()) |
| 30 |
d_handle.promise().rethrow_if_exception(); |
| 31 |
} |
| 32 |
|
| 33 |
return Iterator<T>{ d_handle }; |
| 34 |
} |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
inline Sentinel Generator<T>::end() noexcept |
| 38 |
{ |
| 39 |
return Sentinel{}; |
| 40 |
} |
| 41 |
|
| 42 |
template <typename T> |
| 43 |
inline void Generator<T>::swap(Generator &other) noexcept |
| 44 |
{ |
| 45 |
std::swap(d_handle, other.d_handle); |
| 46 |
} |
| 47 |
|
| 48 |
template <typename T> |
| 49 |
inline |
| 50 |
Generator<T>::Generator(Handle handle) noexcept |
| 51 |
: |
| 52 |
d_handle(handle) |
| 53 |
{} |
| 54 |
|
| 55 |
//------------------------------------------------ |
| 56 |
|
| 57 |
template<typename FUNC, typename T> |
| 58 |
Generator< |
| 59 |
std::invoke_result_t< |
| 60 |
FUNC &, |
| 61 |
typename Generator<T>::iterator::reference> |
| 62 |
> |
| 63 |
fmap(FUNC func, Generator<T> source) |
| 64 |
{ |
| 65 |
for (auto &&value: source) |
| 66 |
co_yield std::invoke( |
| 67 |
func, |
| 68 |
static_cast<decltype(value)>(value) |
| 69 |
); |
| 70 |
} |
| 71 |
|