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

Synchronous generator. More...

#include <coroutine>
#include <exception>
#include <iterator>
#include <memory>
#include <ranges>
#include <type_traits>
#include <utility>
#include "detail.h"
#include "exceptions.h"
+ Include dependency graph for generator.h:

Go to the source code of this file.

Classes

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

Namespaces

namespace  wwa
 
namespace  wwa::coro
 Library namespace.
 

Detailed Description

Synchronous generator.

This file contains the definition of a synchronous generator class template. The generator template class allows for the creation of coroutine-based generators that produce values of a specified type. It provides an interface for iterating over the generated values using range-based for loops or manual iteration.

Example usage:

wwa::coro::generator<int> fibonacci(int n)
{
int a = 0;
int b = 1;
if (n > 0) {
co_yield a;
}
if (n > 1) {
co_yield b;
}
for (int i = 2; i < n; ++i) {
auto s = a + b;
co_yield s;
a = b;
b = s;
}
}
std::cout << "The first 10 Fibonacci numbers are: ";
for (auto n : fibonacci(10)) {
std::cout << n << ' ';
}

Definition in file generator.h.