wwa-coro 0.0.1
Yet Another C++20 Coroutine Library
wwa::coro::async_generator< Result > Class Template Reference

An asynchronous generator that produces values of type Result. More...

#include <async_generator.h>

+ Collaboration diagram for wwa::coro::async_generator< Result >:

Classes

class  iterator
 An input iterator that asynchronously produces values of type Result. More...
 
class  promise_type
 The promise type of the generator. More...
 

Public Member Functions

constexpr async_generator () noexcept=default
 Default constructor.
 
constexpr async_generator (async_generator &&other) noexcept
 Move constructor.
 
 ~async_generator ()
 Destructor.
 
auto begin () noexcept
 Returns an awaitable iterator to the current item of the generator.
 
constexpr auto end () const noexcept
 Returns a sentinel iterator.
 
async_generatoroperator= (async_generator &&other) noexcept
 Move assignment operator.
 

Detailed Description

template<typename Result>
class wwa::coro::async_generator< Result >

An asynchronous generator that produces values of type Result.

The generator is a coroutine that produces values of type Result asynchronously. Unlike synchronous counterparts, an asynchronous generator can use co_await in its body. The caller must use co_await on the begin() iterator and its increment operator.

Example:

wwa::coro::async_generator<int> async_first_n(int n)
{
int v = 0;
while (v < n) {
co_yield v;
// Asynchronous generators can use `co_await`; synchronous ones cannot
v = co_await get_next_value(v);
}
}
auto gen = async_first_n(5);
auto it = co_await gen.begin(); // IMPORTANT! co_await is required
auto end = gen.end();
while (it != end) {
std::cout << *it << "\n";
co_await ++it; // IMPORTANT! co_await is required
}
Template Parameters
ResultThe type of the values produced by the generator.
Examples
advance_with_begin.cpp, async_generator.cpp, and sync_generator_adapter.cpp.

Definition at line 47 of file async_generator.h.

Constructor & Destructor Documentation

◆ async_generator() [1/2]

template<typename Result>
wwa::coro::async_generator< Result >::async_generator ( )
constexprdefaultnoexcept

Default constructor.

Constructs an empty generator (begin() == end()).

◆ async_generator() [2/2]

template<typename Result>
wwa::coro::async_generator< Result >::async_generator ( async_generator< Result > && other)
inlineconstexprnoexcept

Move constructor.

Constructs a generator by moving the coroutine handle from another generator.

Parameters
otherThe other generator to move from.

Definition at line 512 of file async_generator.h.

◆ ~async_generator()

template<typename Result>
wwa::coro::async_generator< Result >::~async_generator ( )
inline

Destructor.

Destroys the generator and the coroutine handle.

Definition at line 524 of file async_generator.h.

Member Function Documentation

◆ begin()

template<typename Result>
auto wwa::coro::async_generator< Result >::begin ( )
inlinenodiscardnoexcept

Returns an awaitable iterator to the current item of the generator.

This method returns an awaitable iterator to the current item of the generator. Because generators cannot be restarted, this method can be used to iterate over the generator:

auto gen = async_first_n(5);
auto it = co_await gen.begin();
auto end = gen.end();
do {
std::cout << *it << " ";
} while (co_await gen.begin() != end);
Attention
The caller must use co_await on the return value of this method.
Returns
Awaitable iterator.

Definition at line 576 of file async_generator.h.

◆ end()

template<typename Result>
auto wwa::coro::async_generator< Result >::end ( ) const
inlinenodiscardconstexprnoexcept

Returns a sentinel iterator.

This method returns a sentinel iterator that marks the end of the generator.

Returns
Sentinel iterator.

Definition at line 592 of file async_generator.h.

◆ operator=()

template<typename Result>
async_generator & wwa::coro::async_generator< Result >::operator= ( async_generator< Result > && other)
inlinenoexcept

Move assignment operator.

Assigns the contents of another generator to this generator by moving them.

Parameters
otherThe other generator to move.
Returns
Reference to this generator.

Definition at line 543 of file async_generator.h.


The documentation for this class was generated from the following file: