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

#include <promise_core.h>

Inheritance diagram for uvco::PromiseCore< T >:

Public Member Functions

 PromiseCore ()=default
 PromiseCore (const PromiseCore &)=delete
 PromiseCore (PromiseCore &&)=delete
PromiseCoreoperator= (const PromiseCore &)=delete
PromiseCoreoperator= (PromiseCore &&)=delete
virtual ~PromiseCore ()=default
 PromiseCore (T &&value)
virtual void setHandle (std::coroutine_handle<> handle)
 Set the coroutine to be resumed once a result is ready.
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)

Public Attributes

std::optional< std::variant< T, std::exception_ptr > > slot
 The slot contains the result once obtained.

Protected Attributes

std::coroutine_handle coroutine_
std::coroutine_handle waitingHandle_
PromiseState state_ = PromiseState::init

Detailed Description

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

A PromiseCore is part of the Coroutine<T> frame; a reference to it is held by the Promise referring to the coroutine.

Constructor & Destructor Documentation

◆ PromiseCore() [1/4]

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

◆ PromiseCore() [2/4]

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

◆ PromiseCore() [3/4]

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

◆ ~PromiseCore()

template<typename T>
virtual uvco::PromiseCore< T >::~PromiseCore ( )
virtualdefault

◆ PromiseCore() [4/4]

template<typename T>
uvco::PromiseCore< T >::PromiseCore ( T && value)
inlineexplicit
Definition promise_core.h:45
PromiseState state_
Definition promise_core.h:135
std::optional< std::variant< T, std::exception_ptr > > slot
The slot contains the result once obtained.
Definition promise_core.h:124

Member Function Documentation

◆ destroyCoroutine()

template<typename T>
void uvco::PromiseCore< T >::destroyCoroutine ( )
inline
114 {
115 if (coroutine_) {
117 coroutine_.destroy();
118 }
119 }
static void cancel(std::coroutine_handle<> handle)
Definition loop.cc:104
std::coroutine_handle coroutine_
Definition promise_core.h:129

◆ except()

template<typename T>
void uvco::PromiseCore< T >::except ( const std::exception_ptr & exc)
inline
121{ slot = exc; }

◆ isAwaited()

template<typename T>
bool uvco::PromiseCore< T >::isAwaited ( )
inline

Checks if a coroutine is waiting on a promise belonging to this core.

85{ return waitingHandle_ != nullptr; }
std::coroutine_handle waitingHandle_
Definition promise_core.h:132

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ ready()

template<typename T>
bool uvco::PromiseCore< T >::ready ( ) const
inlinenodiscard

Checks if a value is present in the slot.

88{ return slot.has_value(); }

◆ resetHandle()

template<typename T>
void uvco::PromiseCore< T >::resetHandle ( )
inline

Reset the handle, so that the coroutine is not resumed anymore. This is required for SelectSet.

◆ resume()

template<typename T>
void uvco::PromiseCore< T >::resume ( )
inline

Resume a suspended coroutine waiting on the associated coroutine by enqueuing it in the global event loop.

A promise core can only be resumed once.

100 {
101 if (waitingHandle_) {
104 waitingHandle_ = nullptr;
106 } else {
107 // This occurs if no co_await has occured until resume. Either the
108 // promise was not co_awaited, or the producing coroutine immediately
109 // returned a value. (await_ready() == true)
110 }
112 }
static void enqueue(std::coroutine_handle<> handle)
Definition loop.cc:94
void resume()
Definition promise_core.h:100

◆ setHandle()

template<typename T>
void uvco::PromiseCore< T >::setHandle ( std::coroutine_handle<> handle)
inlinevirtual

Set the coroutine to be resumed once a result is ready.

Reimplemented in uvco::MultiPromiseCore< T >.

58 {
60 throw UvcoException("PromiseCore is already awaited or has finished");
61 }
64 }

◆ setRunning()

template<typename T>
void uvco::PromiseCore< T >::setRunning ( std::coroutine_handle<> handle)
inline

Used by Coroutine<T> to set the producing coroutine handle.

◆ stale()

template<typename T>
bool uvco::PromiseCore< T >::stale ( ) const
inlinenodiscard

Returns true if the promise has completed, and its results have been fetched.

92 {
93 return state_ == PromiseState::finished && !ready();
94 }
bool ready() const
Checks if a value is present in the slot.
Definition promise_core.h:88

Member Data Documentation

◆ coroutine_

template<typename T>
std::coroutine_handle uvco::PromiseCore< T >::coroutine_
protected

Set to the coroutine producing this promise. Used to destroy it after completion or when the associated Promise is dropped.

◆ slot

template<typename T>
std::optional<std::variant<T, std::exception_ptr> > uvco::PromiseCore< T >::slot

The slot contains the result once obtained.

◆ state_

template<typename T>
PromiseState uvco::PromiseCore< T >::state_ = PromiseState::init
protected

◆ waitingHandle_

template<typename T>
std::coroutine_handle uvco::PromiseCore< T >::waitingHandle_
protected

Set to the coroutine awaiting this promise if state_ == awaitedOn. May be nullptr.


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