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

#include <multipromise.h>

Collaboration diagram for uvco::MultiPromise< T >:

Classes

struct  MultiPromiseAwaiter_

Public Types

using promise_type = Generator<T>

Public Member Functions

 MultiPromise (MultiPromise< T > &&other) noexcept
 An unfulfilled MultiPromise.
MultiPromiseoperator= (const MultiPromise< T > &)=delete
MultiPromiseoperator= (MultiPromise< T > &&other) noexcept
 MultiPromise (const MultiPromise< T > &other)=delete
 ~MultiPromise ()
Promise< std::optional< T > > next ()
MultiPromiseAwaiter_ operator co_await () const
bool ready ()
void cancel ()

Protected Types

using PromiseCore_ = MultiPromiseCore<T>

Protected Member Functions

 MultiPromise (PromiseCore_ &core)

Protected Attributes

PromiseCore_core_

Friends

template<typename U>
class Generator

Detailed Description

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

A MultiPromise is like a Promise, except that it can resolve more than just once. A coroutine returning a MultiPromise (called a "generator coroutine") uses co_yield to return values to the awaiting coroutine. For example in the Udp class, a method exists which generates packets.

As an adapter, for example for SelectSet, the next() method returns a promise returning the next yielded value.

Member Typedef Documentation

◆ promise_type

template<typename T>
using uvco::MultiPromise< T >::promise_type = Generator<T>

Part of the coroutine protocol: the MultiPromise is both return type and promise type.

◆ PromiseCore_

template<typename T>
using uvco::MultiPromise< T >::PromiseCore_ = MultiPromiseCore<T>
protected

Constructor & Destructor Documentation

◆ MultiPromise() [1/3]

template<typename T>
uvco::MultiPromise< T >::MultiPromise ( MultiPromise< T > && other)
inlinenoexcept

An unfulfilled MultiPromise.

It doesn't make sense to yield void. For that, just ues a normal coroutine (Promise<void>) and repeatedly call it.

142 : core_{other.core_} {
143 other.core_ = nullptr;
144 }
Definition multipromise.h:127
PromiseCore_ * core_
Definition multipromise.h:273

◆ MultiPromise() [2/3]

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

◆ ~MultiPromise()

template<typename T>
uvco::MultiPromise< T >::~MultiPromise ( )
inline
158 {
159 if (core_ != nullptr) {
160 core_->cancelGenerator();
161 }
162 core_ = nullptr;
163 }

◆ MultiPromise() [3/3]

template<typename T>
uvco::MultiPromise< T >::MultiPromise ( PromiseCore_ & core)
inlineexplicitprotected
271: core_{&core} {}

Member Function Documentation

◆ cancel()

template<typename T>
void uvco::MultiPromise< T >::cancel ( )
inline

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

cancel() is called automatically once the (last) MultiPromise instance referring to a coroutine is destroyed.

(This can be solved by distinguishing between the returned object and the promise object, which may be part of a future refactor. In that case, the generator's promise object should only contain a weak reference to this MultiPromiseCore, and the caller contains a strong reference. The core's destructor cancels the generator once the caller doesn't require it anymore.)

198 {
199 if (core_ == nullptr) {
200 return;
201 }
202 core_->cancelGenerator();
203 core_ = nullptr;
204 }

◆ next()

template<typename T>
Promise< std::optional< T > > uvco::MultiPromise< T >::next ( )
inline

Obtain the next value yielded by a generator coroutine. This is less efficient than awaiting the multipromise directly.

167{ co_return (co_await *this); }

◆ operator co_await()

template<typename T>
MultiPromiseAwaiter_ uvco::MultiPromise< T >::operator co_await ( ) const
inline

Return an awaiter for this MultiPromise, which resumes the waiting coroutine once the generator yields its next value.

Used when co_awaiting a MultiPromise created by a generator coroutine. The awaiter handles the actual suspension and resumption.

174 {
176 }
Definition multipromise.h:210

◆ operator=() [1/2]

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

◆ operator=() [2/2]

template<typename T>
MultiPromise & uvco::MultiPromise< T >::operator= ( MultiPromise< T > && other)
inlinenoexcept
146 {
147 if (this == &other) {
148 return *this;
149 }
150 if (core_ != nullptr) {
151 core_->destroyCoroutine();
152 }
153 core_ = other.core_;
154 other.core_ = nullptr;
155 return *this;
156 }

◆ ready()

template<typename T>
bool uvco::MultiPromise< T >::ready ( )
inline

Returns true if a value is available, or the generator has returned or thrown.

180 {
181 return core_ == nullptr || core_->slot.has_value() || core_->isTerminated();
182 }

◆ Generator

template<typename T>
template<typename U>
friend class Generator
friend

Member Data Documentation

◆ core_

template<typename T>
PromiseCore_* uvco::MultiPromise< T >::core_
protected

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