Async++ unknown
Async (co_await/co_return) code for C++
|
Promise type that allows waiting for a result in both synchronous and asynchronous code. More...
#include <asyncpp/promise.h>
Public Types | |
using | result_type = TResult |
Public Member Functions | |
promise () | |
Construct a new promise object in its pending state. | |
promise (const promise &other) | |
Copy constructor. | |
promise & | operator= (const promise &other) |
Copy assignment. | |
bool | is_pending () const noexcept |
Check if the promise is pending. | |
bool | is_fulfilled () const noexcept |
Check if the promise is fulfilled. | |
bool | is_rejected () const noexcept |
Check if the promise is rejected. | |
void | fulfill (auto &&value) |
Fulfill the promise with a value. | |
bool | try_fulfill (auto &&value) |
Try to fulfill the promise with a value. | |
void | reject (std::exception_ptr error) |
Reject the promise with an exception. | |
bool | try_reject (std::exception_ptr error) |
Try to reject the promise with an exception. | |
template<typename TException , typename... Args> | |
void | reject (Args &&... args) |
Reject the promise with an exception. | |
template<typename TException , typename... Args> | |
bool | try_reject (Args &&... args) |
Try to reject the promise with an exception. | |
void | then (std::function< void(const TResult &)> then_cb, std::function< void(const std::exception_ptr &)> catch_cb) |
Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed. | |
void | on_settle (std::function< void()> settle_cb) |
Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed. | |
TResult & | get () const |
Synchronously get the result. If the promise is rejected the rejecting exception gets thrown. | |
template<class Rep , class Period > | |
TResult * | get (std::chrono::duration< Rep, Period > timeout) const |
Synchronously get the result with a timeout. If the promise is rejected the rejecting exception gets thrown. | |
std::pair< TResult *, std::exception_ptr > | try_get (std::nothrow_t) const noexcept |
Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown. | |
TResult * | try_get () const |
Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown. | |
auto | operator co_await () const noexcept |
Asynchronously get the result. If the promise is rejected the rejecting exception gets thrown. | |
Static Public Member Functions | |
static promise | make_fulfilled (TResult &&value) |
Get a fufilled promise with the specified value. | |
static promise | make_rejected (std::exception_ptr exception) |
Get a rejected promise with the specified exception. | |
template<typename TException , typename... Args> | |
static promise | make_rejected (Args &&... args) |
Get a rejected promise with the specified exception. | |
Protected Types | |
using | state = detail::promise_state<TResult, std::exception_ptr> |
Protected Attributes | |
ref< state > | m_state {} |
Promise type that allows waiting for a result in both synchronous and asynchronous code.
TResult | Type of the result |
|
inline |
Fulfill the promise with a value.
std::logic_error | if the promise is already fulfilled or rejected. |
value | The value to store inside the promise |
|
inline |
Synchronously get the result. If the promise is rejected the rejecting exception gets thrown.
|
inline |
Synchronously get the result with a timeout. If the promise is rejected the rejecting exception gets thrown.
|
inlinenoexcept |
Check if the promise is fulfilled.
|
inlinenoexcept |
Check if the promise is pending.
|
inlinenoexcept |
Check if the promise is rejected.
|
inlinestatic |
Get a fufilled promise with the specified value.
value | Value for the fulfilled promise |
|
inlinestatic |
Get a rejected promise with the specified exception.
TException | The type of the exception to store |
args | Parameters to pass to the exception type constructor |
|
inlinestatic |
Get a rejected promise with the specified exception.
exception | Exception to store in the rejected promise |
|
inline |
Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed.
cb | Callback to invoke as soon as a result is available. |
|
inlinenoexcept |
Asynchronously get the result. If the promise is rejected the rejecting exception gets thrown.
|
inline |
Reject the promise with an exception.
std::logic_error | if the promise is already fulfilled or rejected |
TException | The exception type to use for rejection |
args | Arguments passed to the constructor of the exception type |
|
inline |
Reject the promise with an exception.
std::logic_error | if the promise is already fulfilled or rejected |
ex | The exception to use for rejection |
|
inline |
Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed.
cb | Callback to invoke as soon as a result is available. |
|
inline |
Try to fulfill the promise with a value.
value | The value to store inside the promise |
|
inline |
Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown.
|
inlinenoexcept |
Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown.
|
inline |
Try to reject the promise with an exception.
TException | The exception type to use for rejection |
args | Arguments passed to the constructor of the exception type |
|
inline |
Try to reject the promise with an exception.
ex | The exception to use for rejection |