uvco 0.1
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
uvco::Promise< T >::PromiseAwaiter_ Struct Reference

#include <promise.h>

Collaboration diagram for uvco::Promise< T >::PromiseAwaiter_:
Collaboration graph
[legend]

Public Member Functions

 PromiseAwaiter_ (PromiseCore_ &core)
 
 PromiseAwaiter_ (PromiseAwaiter_ &&)=delete
 
 PromiseAwaiter_ (const PromiseAwaiter_ &)=delete
 
PromiseAwaiter_operator= (PromiseAwaiter_ &&)=delete
 
PromiseAwaiter_operator= (const PromiseAwaiter_ &)=delete
 
 ~PromiseAwaiter_ ()=default
 
bool await_ready () const
 
bool await_suspend (std::coroutine_handle<> handle) const
 
await_resume () const
 

Public Attributes

PromiseCore_core_
 

Detailed Description

template<typename T>
struct uvco::Promise< T >::PromiseAwaiter_

Returned as awaiter object when co_awaiting a promise.

Handles suspension of current coroutine and resumption upon fulfillment of the awaited promise.

Constructor & Destructor Documentation

◆ PromiseAwaiter_() [1/3]

template<typename T >
uvco::Promise< T >::PromiseAwaiter_::PromiseAwaiter_ ( PromiseCore_ core)
inlineexplicit

The core is shared with the promise and contains the resumption handle, and ultimately the returned value. Because the awaiter object is only used while a coroutine is waiting on a co_await suspension point, we can use a reference to the PromiseCore_ object.

168: core_{core} {}
SharedCore_ & core()
Definition promise.h:216
PromiseCore_ & core_
Definition promise.h:209

◆ PromiseAwaiter_() [2/3]

template<typename T >
uvco::Promise< T >::PromiseAwaiter_::PromiseAwaiter_ ( PromiseAwaiter_ &&  )
delete

◆ PromiseAwaiter_() [3/3]

template<typename T >
uvco::Promise< T >::PromiseAwaiter_::PromiseAwaiter_ ( const PromiseAwaiter_ )
delete

◆ ~PromiseAwaiter_()

template<typename T >
uvco::Promise< T >::PromiseAwaiter_::~PromiseAwaiter_ ( )
default

Member Function Documentation

◆ await_ready()

template<typename T >
bool uvco::Promise< T >::PromiseAwaiter_::await_ready ( ) const
inline

Part of the coroutine protocol: returns true if the promise is already fulfilled.

177{ return core_.ready(); }
bool ready() const
Checks if a value is present in the slot.
Definition promise_core.h:115

◆ await_resume()

template<typename T >
T uvco::Promise< T >::PromiseAwaiter_::await_resume ( ) const
inline

Part of the coroutine protocol: extracts the resulting value from the promise core and returns it.

188 {
189 if (core_.stale()) {
190 throw UvcoException(
191 "co_await called on previously finished promise (T)");
192 }
193 if (core_.slot.has_value()) {
194 switch (core_.slot->index()) {
195 case 0: {
196 T result = std::move(std::get<0>(core_.slot.value()));
197 core_.slot.reset();
198 return std::move(result);
199 }
200 case 1:
201 std::rethrow_exception(std::get<1>(core_.slot.value()));
202 default:
203 throw UvcoException("PromiseAwaiter_::await_resume: invalid slot");
204 }
205 }
206 throw UvcoException("await_resume called on unfulfilled promise (bug?)");
207 }
bool stale() const
Definition promise_core.h:118
std::optional< std::variant< T, std::exception_ptr > > slot
The slot contains the result once obtained.
Definition promise_core.h:184

◆ await_suspend()

template<typename T >
bool uvco::Promise< T >::PromiseAwaiter_::await_suspend ( std::coroutine_handle<>  handle) const
inline

Part of the coroutine protocol: returns if suspension is desired (always true), and stores the awaiting coroutine state in the PromiseCore.

180 {
181 BOOST_ASSERT_MSG(!core_.willResume(),
182 "promise is already being waited on!");
184 return true;
185 }
bool willResume()
Checks if a coroutine is waiting on a promise belonging to this core.
Definition promise_core.h:113
virtual void setHandle(std::coroutine_handle<> handle)
Set the coroutine to be resumed once a result is ready.
Definition promise_core.h:74
PromiseHandle< T > handle()
Return a handle that can be used to cancel the coroutine.
Definition promise.h:130

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

Member Data Documentation

◆ core_

template<typename T >
PromiseCore_& uvco::Promise< T >::PromiseAwaiter_::core_

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