|
uvco 0.1
|
#include <promise.h>

Classes | |
| struct | PromiseAwaiter_ |
Public Types | |
| using | promise_type = Coroutine<T> |
Public Member Functions | |
| Promise (Promise< T > &&other) noexcept | |
| Promise & | operator= (const Promise< T > &other)=delete |
| A promise can be copied at low cost. | |
| Promise & | operator= (Promise< T > &&other) noexcept |
| Promise (const Promise< T > &other)=delete | |
| ~Promise () | |
| PromiseAwaiter_ | operator co_await () const |
| bool | ready () const |
| Returns if promise has been fulfilled. | |
| T | unwrap () |
Protected Types | |
| using | PromiseCore_ = PromiseCore<T> |
| PromiseCore_ handles the inner mechanics of resumption and suspension. | |
Protected Member Functions | |
| Promise (PromiseCore_ &core) | |
| PromiseCore_ * | core () |
Protected Attributes | |
| PromiseCore_ * | core_ |
Friends | |
| template<typename U> | |
| class | Coroutine |
| template<typename... Ts> | |
| class | SelectSet |
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 (and it can't); 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.
NOTE: due to inlining (visibility of the promise code) and other factors, Promise<int> (for example) can be 20% faster than Promise<void>, and also cause fewer allocations. This happens especially when the compiler can elide the coroutine frame allocation for Promise<T>, which it can't do with Promise<void> (which is implemented in a .cc file).
| 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_).
|
protected |
PromiseCore_ handles the inner mechanics of resumption and suspension.
|
inlinenoexcept |
|
delete |
|
inline |
|
inlineexplicitprotected |
|
inlineprotected |
|
inline |
Part of the coroutine protocol: called by co_await p where p is a Promise<T>. The returned object is awaited on.
|
delete |
A promise can be copied at low cost.
|
inlinenoexcept |
|
inlinenodiscard |
Returns if promise has been fulfilled.
|
inline |
|
protected |