wwa-coro 0.0.1
Yet Another C++20 Coroutine Library
async_generator.h File Reference

Asynchronous generator. More...

#include <coroutine>
#include <exception>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
#include "detail.h"
#include "exceptions.h"
+ Include dependency graph for async_generator.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  wwa::coro::async_generator< Result >
 An asynchronous generator that produces values of type Result. More...
 
class  wwa::coro::async_generator< Result >::iterator
 An input iterator that asynchronously produces values of type Result. More...
 
class  wwa::coro::async_generator< Result >::promise_type
 The promise type of the generator. More...
 

Namespaces

namespace  wwa
 
namespace  wwa::coro
 Library namespace.
 

Detailed Description

Asynchronous generator.

This file contains the definition of an asynchronous generator class template. The async_generator template class allows for the creation of coroutine-based generators that produce values of a specified type asynchronously. It provides an interface for iterating over the generated values using co_await and asynchronous iteration.

Example usage:

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
}

With the help of sync_generator_adapter, you can adapt an asynchronous generator to use it in a synchronous context (e.g., range-based for loops or views).

Definition in file async_generator.h.