uvco 0.1
Loading...
Searching...
No Matches
uvco::MultiPromiseCore< T > Class Template Reference

#include <multipromise.h>

Inheritance diagram for uvco::MultiPromiseCore< T >:
Collaboration diagram for uvco::MultiPromiseCore< T >:

Public Member Functions

 MultiPromiseCore ()=default
 MultiPromiseCore (const MultiPromiseCore &)=delete
 MultiPromiseCore (MultiPromiseCore &&)=delete
MultiPromiseCoreoperator= (const MultiPromiseCore &)=delete
MultiPromiseCoreoperator= (MultiPromiseCore &&)=delete
 ~MultiPromiseCore () override=default
void setHandle (std::coroutine_handle<> handle) override
void resumeGenerator ()
void suspendGenerator ()
void cancelGenerator ()
void setTerminated ()
bool isTerminated () const
 Check if the generator has been cancelled or has returned.
Public Member Functions inherited from uvco::PromiseCore< T >
 PromiseCore ()=default
 PromiseCore (const PromiseCore &)=delete
 PromiseCore (PromiseCore &&)=delete
PromiseCoreoperator= (const PromiseCore &)=delete
PromiseCoreoperator= (PromiseCore &&)=delete
virtual ~PromiseCore ()=default
 PromiseCore (T &&value)
void setRunning (std::coroutine_handle<> handle)
 Used by Coroutine<T> to set the producing coroutine handle.
void resetHandle ()
bool isAwaited ()
 Checks if a coroutine is waiting on a promise belonging to this core.
bool ready () const
 Checks if a value is present in the slot.
bool stale () const
void resume ()
void destroyCoroutine ()
void except (const std::exception_ptr &exc)

Private Attributes

bool suspended_ = false
 True if the generator is suspended at a co_yield point.
bool terminated_ = false

Additional Inherited Members

Public Attributes inherited from uvco::PromiseCore< T >
std::optional< std::variant< T, std::exception_ptr > > slot
 The slot contains the result once obtained.
Protected Attributes inherited from uvco::PromiseCore< T >
std::coroutine_handle coroutine_
std::coroutine_handle waitingHandle_
PromiseState state_ = PromiseState::init

Detailed Description

template<typename T>
class uvco::MultiPromiseCore< T >

A MultiPromiseCore works like a PromiseCore, but with an adapted state machine: it can transition from finished back to waitedOn, and therefore yield more than one value. It is used by MultiPromise, a generator-like type.

Constructor & Destructor Documentation

◆ MultiPromiseCore() [1/3]

template<typename T>
uvco::MultiPromiseCore< T >::MultiPromiseCore ( )
default

◆ MultiPromiseCore() [2/3]

template<typename T>
uvco::MultiPromiseCore< T >::MultiPromiseCore ( const MultiPromiseCore< T > & )
delete

◆ MultiPromiseCore() [3/3]

template<typename T>
uvco::MultiPromiseCore< T >::MultiPromiseCore ( MultiPromiseCore< T > && )
delete

◆ ~MultiPromiseCore()

template<typename T>
uvco::MultiPromiseCore< T >::~MultiPromiseCore ( )
overridedefault

Member Function Documentation

◆ cancelGenerator()

template<typename T>
void uvco::MultiPromiseCore< T >::cancelGenerator ( )
inline

Cancel the generator coroutine. This will drop all stack variables inside the generator (and run their destructors), and ensure that the generatorHandle_ will never resume from the currently yielded value.

Called by tje MultiPromise destructor and MultiPromise::cancel().

89 {
91 if (coroutine_ != nullptr) {
93 coroutine_ = nullptr;
95 // Careful: within this function, this class' dtor is called!
96 coroutine.destroy();
97 }
98 }
static void cancel(std::coroutine_handle<> handle)
Definition loop.cc:104
Definition multipromise.h:24
void setTerminated()
Definition multipromise.h:104
std::coroutine_handle coroutine_
Definition promise_core.h:129

◆ isTerminated()

template<typename T>
bool uvco::MultiPromiseCore< T >::isTerminated ( ) const
inlinenodiscard

Check if the generator has been cancelled or has returned.

107{ return terminated_; }
bool terminated_
Definition multipromise.h:114

◆ operator=() [1/2]

template<typename T>
MultiPromiseCore & uvco::MultiPromiseCore< T >::operator= ( const MultiPromiseCore< T > & )
delete

◆ operator=() [2/2]

template<typename T>
MultiPromiseCore & uvco::MultiPromiseCore< T >::operator= ( MultiPromiseCore< T > && )
delete

◆ resumeGenerator()

template<typename T>
void uvco::MultiPromiseCore< T >::resumeGenerator ( )
inline

Resume the generator from its last co_yield point, so it can yield the next value.

Called by a MultiPromiseAwaiter_ when the MultiPromise is co_awaited, after a value has been successfully awaited.

65 {
67 if (suspended_) {
68 BOOST_ASSERT(coroutine_ != nullptr);
69 suspended_ = false;
71 }
72 }
static void enqueue(std::coroutine_handle<> handle)
Definition loop.cc:94
bool suspended_
True if the generator is suspended at a co_yield point.
Definition multipromise.h:111

◆ setHandle()

template<typename T>
void uvco::MultiPromiseCore< T >::setHandle ( std::coroutine_handle<> handle)
inlineoverridevirtual

See PromiseCore::setHandle. Called by a MultiPromise when it is co_awaited.

In contrast to a PromiseCore, a finished multipromise core can be reset to the waiting state, in order to yield the next value, when the MultiPromise is co_awaited again.

Reimplemented from uvco::PromiseCore< T >.

42 {
43 // Once an external scheduler works, Promises will not be nested anymore
44 // (resume called by resume down in the stack)
45 //
46 // BOOST_ASSERT(PromiseCore<T>::state_
47 // == PromiseState::init || PromiseCore<T>::state_ ==
48 // PromiseState::finished);
49 //
50 // state is init or running (latter can occur if setHandle is called from a
51 // stack originating at resume()).
53 "MultiPromise must be co_awaited before next yield");
55 "MultiPromise must be co_awaited before next yield");
58 }

◆ setTerminated()

template<typename T>
void uvco::MultiPromiseCore< T >::setTerminated ( )
inline

Mark generator as finished, yielding no more values. Called from within the MultiPromise upon return_value and unhandled_exception. From hereon awaiting the generator will either rethrow the thrown exception, or yield nullopt.

104{ terminated_ = true; }

◆ suspendGenerator()

template<typename T>
void uvco::MultiPromiseCore< T >::suspendGenerator ( )
inline

Suspend the generator coroutine at the current co_yield point by storing the handle for a later resumption.

Called by the MultiPromise<...>::yield_value method when co_yield is invoked inside the generator.

79 {
81 suspended_ = true;
82 }

Member Data Documentation

◆ suspended_

template<typename T>
bool uvco::MultiPromiseCore< T >::suspended_ = false
private

True if the generator is suspended at a co_yield point.

◆ terminated_

template<typename T>
bool uvco::MultiPromiseCore< T >::terminated_ = false
private

Set to true once the generator coroutine has returned or has been cancelled.


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