uvco 0.1
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Friends | List of all members
uvco::Promise< T > Class Template Reference

#include <promise.h>

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

Classes

struct  PromiseAwaiter_
 

Public Types

using promise_type = Coroutine<T>
 

Public Member Functions

 Promise ()
 Unfulfilled, empty promise.
 
 Promise (T &&result)
 Fulfilled promise; resolves immediately.
 
 Promise (Promise< T > &&other) noexcept
 
Promiseoperator= (const Promise< T > &other)
 A promise can be copied at low cost.
 
Promiseoperator= (Promise< T > &&other) noexcept
 
 Promise (const Promise< T > &other)
 
 ~Promise ()
 
PromiseHandle< T > handle ()
 Return a handle that can be used to cancel the coroutine.
 
PromiseAwaiter_ operator co_await () const
 
bool ready () const
 Returns if promise has been fulfilled.
 
unwrap ()
 

Protected Types

using PromiseCore_ = PromiseCore<T>
 PromiseCore_ handles the inner mechanics of resumption and suspension.
 
using SharedCore_ = PromiseCore_ *
 

Protected Member Functions

 Promise (SharedCore_ core)
 
SharedCore_core ()
 

Protected Attributes

SharedCore_ core_
 

Friends

template<typename U >
class Coroutine
 
template<typename... Ts>
class SelectSet
 

Detailed Description

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

A Promise is the core type of uvco, and returned from coroutines. A coroutine is a function containing either of co_await, co_yield, or co_return. The Promise type defines Coroutine to be the promise type used within a coroutine. The Promise object itself acts as awaitable for awaiting coroutines.

A Promise doesn't need to be constructed directly; it is always returned from a coroutine function. Declare a function with a return type of Promise<T> and use co_return to return a value - that's it! Inside the coroutine, you can use co_await etc.

When a Promise is awaited using co_await, the awaiting coroutine is suspended until the promise is resolved. Once the promise is resolved, the suspended coroutine is scheduled to be resumed by Loop at a later time.

The internal state is held in a PromiseCore_ shared by all copies of the same Promise. However, only one coroutine can await a (shared) promise at a time.

See README.md for some examples of how to use coroutines and promises.

Member Typedef Documentation

◆ promise_type

template<typename T >
using uvco::Promise< T >::promise_type = Coroutine<T>

Part of the coroutine protocol: specifies which class will define the coroutine behavior. In this case, the Coroutine class implements the promise protocol (don't get confused!). This split is useful so that coroutines can use Promise objects as function arguments without implicit construction of their promise objects, which can easily cuase bugs.

Note that the awaiter type is separate (PromiseAwaiter_).

◆ PromiseCore_

template<typename T >
using uvco::Promise< T >::PromiseCore_ = PromiseCore<T>
protected

PromiseCore_ handles the inner mechanics of resumption and suspension.

◆ SharedCore_

template<typename T >
using uvco::Promise< T >::SharedCore_ = PromiseCore_ *
protected

Constructor & Destructor Documentation

◆ Promise() [1/5]

template<typename T >
uvco::Promise< T >::Promise ( )
inline

Unfulfilled, empty promise.

94: core_{makeRefCounted<PromiseCore_>()} {}
SharedCore_ core_
Definition promise.h:218

◆ Promise() [2/5]

template<typename T >
uvco::Promise< T >::Promise ( T &&  result)
inlineexplicit

Fulfilled promise; resolves immediately.

97 : core_{makeRefCounted<PromiseCore_>(std::move(result))} {}

◆ Promise() [3/5]

template<typename T >
uvco::Promise< T >::Promise ( Promise< T > &&  other)
inlinenoexcept
99 : core_{other.core_} {
100 other.core_ = nullptr;
101 }

◆ Promise() [4/5]

template<typename T >
uvco::Promise< T >::Promise ( const Promise< T > &  other)
inline
122: core_{other.core_->addRef()} {}
virtual T * addRef()
Definition internal_utils.h:109

◆ ~Promise()

template<typename T >
uvco::Promise< T >::~Promise ( )
inline
123 {
124 if (core_ != nullptr) {
125 core_->delRef();
126 }
127 }
virtual void delRef()
Definition internal_utils.h:117

◆ Promise() [5/5]

template<typename T >
uvco::Promise< T >::Promise ( SharedCore_  core)
inlineexplicitprotected
215: core_{core->addRef()} {}
SharedCore_ & core()
Definition promise.h:216

Member Function Documentation

◆ core()

template<typename T >
SharedCore_ & uvco::Promise< T >::core ( )
inlineprotected
216{ return core_; }

◆ handle()

template<typename T >
PromiseHandle< T > uvco::Promise< T >::handle ( )
inline

Return a handle that can be used to cancel the coroutine.

130{ return PromiseHandle<T>{core_}; }

◆ operator co_await()

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

Part of the coroutine protocol: called by co_await p where p is a Promise<T>. The returned object is awaited on.

134{ return PromiseAwaiter_{*core_}; }

◆ operator=() [1/2]

template<typename T >
Promise & uvco::Promise< T >::operator= ( const Promise< T > &  other)
inline

A promise can be copied at low cost.

103 {
104 if (this == &other) {
105 return *this;
106 }
107 core_ = other.core_->addRef();
108 return *this;
109 }

◆ operator=() [2/2]

template<typename T >
Promise & uvco::Promise< T >::operator= ( Promise< T > &&  other)
inlinenoexcept
110 {
111 if (this == &other) {
112 return *this;
113 }
114 if (core_ != nullptr) {
115 core_->delRef();
116 }
117 core_ = other.core_;
118 other.core_ = nullptr;
119 return *this;
120 }

◆ ready()

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

Returns if promise has been fulfilled.

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

◆ unwrap()

template<typename T >
T uvco::Promise< T >::unwrap ( )
inline
139 {
140 if (ready()) {
141 auto &slot = core_->slot.value();
142 switch (slot.index()) {
143 case 0: {
144 T value = std::move(std::get<0>(slot));
145 core_->slot.reset();
146 return std::move(value);
147 }
148 case 1: {
149 std::rethrow_exception(std::get<1>(slot));
150 }
151 default:
152 throw UvcoException("PromiseAwaiter_::await_resume: invalid slot");
153 }
154 }
155 throw UvcoException("unwrap called on unfulfilled promise");
156 }
std::optional< std::variant< T, std::exception_ptr > > slot
The slot contains the result once obtained.
Definition promise_core.h:184
bool ready() const
Returns if promise has been fulfilled.
Definition promise.h:137

Friends And Related Symbol Documentation

◆ Coroutine

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

◆ SelectSet

template<typename T >
template<typename... Ts>
friend class SelectSet
friend

Member Data Documentation

◆ core_

template<typename T >
SharedCore_ uvco::Promise< T >::core_
protected

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