uvco 0.1
|
#include <promise.h>
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 | |
Promise & | operator= (const Promise< T > &other) |
A promise can be copied at low cost. | |
Promise & | operator= (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. | |
T | 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 |
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.
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.
|
protected |
|
inline |
Unfulfilled, empty promise.
|
inlineexplicit |
Fulfilled promise; resolves immediately.
|
inlinenoexcept |
|
inline |
|
inline |
|
inlineexplicitprotected |
|
inlineprotected |
|
inline |
Return a handle that can be used to cancel the coroutine.
|
inline |
|
inline |
|
inlinenoexcept |
|
inline |
Returns if promise has been fulfilled.
|
inline |
|
protected |