CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::Generator< T > Class Template Referencefinal

A generator represents a coroutine type that produces a sequence of values of type T, where values are produced lazily and synchronously. More...

#include <CeresEngine/Foundation/Generator.hpp>

Classes

class  Iterator
 An iterator type that allows the caller to iterate over the results of a generator. More...
 
class  Promise
 The promise type used to implement to coroutine. More...
 

Public Types

using promise_type = Promise
 A type required by the coroutine implementation.
 

Public Member Functions

 Generator () noexcept
 Creates a new empty generator.
 
 Generator (const Generator &other)=delete
 Disables copy constructor.
 
Generatoroperator= (const Generator &other)=delete
 Disables copy assignment operator.
 
 Generator (Generator &&other) noexcept
 Creates a new generator by moving the contents of another.
 
Generatoroperator= (Generator &&other) noexcept
 Assigns the generator by moving the contents of another.
 
 ~Generator ()
 Destroys the generator and it's corresponding coroutine.
 
Iterator begin ()
 
Iterator end () noexcept
 

Private Types

using CoroutineHandle = coroutine_handle< Promise >
 The coroutine handle type.
 
using suspend_always = CeresEngine::suspend_always
 An alias to std::suspend_always.
 
using suspend_never = CeresEngine::suspend_never
 An alias to std::suspend_never.
 

Private Member Functions

 Generator (CoroutineHandle coroutine) noexcept
 Creates a new Generator from an existing coroutine handle.
 

Private Attributes

CoroutineHandle mCoroutine
 

Detailed Description

template<typename T>
class CeresEngine::Generator< T >

A generator represents a coroutine type that produces a sequence of values of type T, where values are produced lazily and synchronously.

The coroutine body is able to yield values of type T using the co_yield keyword. Note, however, that the coroutine body is not able to use the co_await keyword; values must be produced synchronously.

When a coroutine function returning a Generator is called the coroutine is created initially suspended. Execution of the coroutine enters the coroutine body when the Generator::begin() method is called and continues until either the first co_yield statement is reached or the coroutine runs to completion.

If the returned iterator is not equal to the end() iterator then dereferencing the iterator will return a reference to the value passed to the co_yield statement.

Calling operator++() on the iterator will resume execution of the coroutine and continue until either the next co_yield point is reached or the coroutine runs to completion.

Any unhandled exceptions thrown by the coroutine will propagate out of the begin() or operator++() calls to the caller.

Template Parameters
Tthe value type returned by the generator.

Member Typedef Documentation

◆ CoroutineHandle

template<typename T >
using CeresEngine::Generator< T >::CoroutineHandle = coroutine_handle<Promise>
private

The coroutine handle type.

◆ promise_type

template<typename T >
using CeresEngine::Generator< T >::promise_type = Promise

A type required by the coroutine implementation.

◆ suspend_always

template<typename T >
using CeresEngine::Generator< T >::suspend_always = CeresEngine::suspend_always
private

An alias to std::suspend_always.

◆ suspend_never

template<typename T >
using CeresEngine::Generator< T >::suspend_never = CeresEngine::suspend_never
private

An alias to std::suspend_never.

Constructor & Destructor Documentation

◆ Generator() [1/4]

template<typename T >
CeresEngine::Generator< T >::Generator ( )
inlinenoexcept

Creates a new empty generator.

This generator will always return an empty set of elements.

◆ Generator() [2/4]

template<typename T >
CeresEngine::Generator< T >::Generator ( const Generator< T > &  other)
delete

Disables copy constructor.

◆ Generator() [3/4]

template<typename T >
CeresEngine::Generator< T >::Generator ( Generator< T > &&  other)
inlinenoexcept

Creates a new generator by moving the contents of another.

Parameters
otherThe generator to move from.

◆ ~Generator()

template<typename T >
CeresEngine::Generator< T >::~Generator ( )
inline

Destroys the generator and it's corresponding coroutine.

◆ Generator() [4/4]

template<typename T >
CeresEngine::Generator< T >::Generator ( CoroutineHandle  coroutine)
inlineexplicitprivatenoexcept

Creates a new Generator from an existing coroutine handle.

Parameters
coroutineThe coroutine handle to create the generator with

Member Function Documentation

◆ begin()

template<typename T >
Iterator CeresEngine::Generator< T >::begin ( )
inline
Returns
An iterator that points to the first generated value

◆ end()

template<typename T >
Iterator CeresEngine::Generator< T >::end ( )
inlinenoexcept
Returns
An iterator object that represents the end of the generated sequence.

◆ operator=() [1/2]

template<typename T >
Generator & CeresEngine::Generator< T >::operator= ( const Generator< T > &  other)
delete

Disables copy assignment operator.

◆ operator=() [2/2]

template<typename T >
Generator & CeresEngine::Generator< T >::operator= ( Generator< T > &&  other)
inlinenoexcept

Assigns the generator by moving the contents of another.

Parameters
otherThe generator to move from.
Returns
The newly moved generator

Member Data Documentation

◆ mCoroutine

template<typename T >
CoroutineHandle CeresEngine::Generator< T >::mCoroutine
private

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