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

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

#include <generator.h>

+ Inheritance diagram for wwa::coro::generator< Result >:
+ Collaboration diagram for wwa::coro::generator< Result >:

Classes

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

Public Member Functions

 generator () noexcept=default
 Default constructor.
 
 generator (generator &&other) noexcept
 Move constructor.
 
 ~generator ()
 Destructor.
 
iterator begin ()
 Returns an iterator to the current item of the generator.
 
iterator begin () const
 Returns a constant iterator to the current item of the generator.
 
iterator cbegin () const
 Returns a constant iterator to the current item of the generator.
 
iterator cend () const noexcept
 Returns a cinstant sentinel iterator.
 
constexpr iterator end () const noexcept
 Returns a sentinel iterator that marks the end of the generator.
 
generatoroperator= (generator &&other) noexcept
 Move assignment operator.
 

Detailed Description

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

An synchronous generator that produces values of type Result.

The generator class represents a coroutine generator that produces values of type ``.

Example:

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 << ' ';
}
Template Parameters
ResultThe type of the values produced by the generator.
Examples
advance_with_begin.cpp, and generator.cpp.

Definition at line 43 of file generator.h.

Constructor & Destructor Documentation

◆ generator() [1/2]

template<typename Result>
wwa::coro::generator< Result >::generator ( )
defaultnoexcept

Default constructor.

Constructs an empty generator. This constructor is not really useful except for testing.

◆ generator() [2/2]

template<typename Result>
wwa::coro::generator< Result >::generator ( generator< Result > && other)
inlinenoexcept

Move constructor.

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

Parameters
otherThe other generator to move from.

Definition at line 352 of file generator.h.

◆ ~generator()

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

Destructor.

Destroys the generator and the coroutine handle.

Definition at line 362 of file generator.h.

Member Function Documentation

◆ begin() [1/2]

template<typename Result>
iterator wwa::coro::generator< Result >::begin ( )
inlinenodiscard

Returns an iterator to the current item of the generator.

This method returns an iterator to the current item of the generator. It is called begin() only to allow the generator to be used in range-based for loops or with ranges.

Because generators cannot be restarted, this method can be used to iterate over the generator:

auto gen = sync_first_n(5);
auto it = gen.begin();
auto end = gen.end();
do {
std::cout << *it << " ";
} while (gen.begin() != end);
Returns
Iterator to the current item.

Definition at line 414 of file generator.h.

◆ begin() [2/2]

template<typename Result>
iterator wwa::coro::generator< Result >::begin ( ) const
inlinenodiscard

Returns a constant iterator to the current item of the generator.

See also
begin()
Returns
Constant iterator to the current item.

Definition at line 430 of file generator.h.

◆ cbegin()

template<typename Result>
iterator wwa::coro::generator< Result >::cbegin ( ) const
inlinenodiscard

Returns a constant iterator to the current item of the generator.

Alias for begin() const.

Returns
Constant iterator. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 457 of file generator.h.

◆ cend()

template<typename Result>
iterator wwa::coro::generator< Result >::cend ( ) const
inlinenodiscardnoexcept

Returns a cinstant sentinel iterator.

Alias for end() const.

Returns
Constant sentinel iterator.

Definition at line 468 of file generator.h.

◆ end()

template<typename Result>
iterator wwa::coro::generator< Result >::end ( ) const
inlinenodiscardconstexprnoexcept

Returns a sentinel iterator that marks the end of the generator.

This method returns a sentinel iterator that marks the end of the generator. An attempt to dereference this iterator or iterate past it will result in an exception being thrown.

Definition at line 447 of file generator.h.

◆ operator=()

template<typename Result>
generator & wwa::coro::generator< Result >::operator= ( 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 382 of file generator.h.


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