Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
asyncpp::promise< TResult > Class Template Reference

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.
 
promiseoperator= (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 {}
 

Detailed Description

template<typename TResult>
class asyncpp::promise< TResult >

Promise type that allows waiting for a result in both synchronous and asynchronous code.

Template Parameters
TResultType of the result

Member Function Documentation

◆ fulfill()

template<typename TResult >
void asyncpp::promise< TResult >::fulfill ( auto && value)
inline

Fulfill the promise with a value.

Exceptions
std::logic_errorif the promise is already fulfilled or rejected.
Parameters
valueThe value to store inside the promise
Note
Callbacks and waiting coroutines are resumed inside this call.

◆ get() [1/2]

template<typename TResult >
TResult & asyncpp::promise< TResult >::get ( ) const
inline

Synchronously get the result. If the promise is rejected the rejecting exception gets thrown.

Returns
TResult& Reference to the result value

◆ get() [2/2]

template<typename TResult >
template<class Rep , class Period >
TResult * asyncpp::promise< TResult >::get ( std::chrono::duration< Rep, Period > timeout) const
inline

Synchronously get the result with a timeout. If the promise is rejected the rejecting exception gets thrown.

Returns
TResult* Pointer to the result value or nullptr on timeout

◆ is_fulfilled()

template<typename TResult >
bool asyncpp::promise< TResult >::is_fulfilled ( ) const
inlinenoexcept

Check if the promise is fulfilled.

Note
Unlike is_pending() this value is permanent.
Returns
true if the promise contains a value.

◆ is_pending()

template<typename TResult >
bool asyncpp::promise< TResult >::is_pending ( ) const
inlinenoexcept

Check if the promise is pending.

Note
This is a temporary snapshot and should only be used for logging. Consider the returned value potentially invalid by the moment the call returns.

◆ is_rejected()

template<typename TResult >
bool asyncpp::promise< TResult >::is_rejected ( ) const
inlinenoexcept

Check if the promise is rejected.

Note
Unlike is_pending() this value is permanent.
Returns
true if the promise contains an exception.

◆ make_fulfilled()

template<typename TResult >
static promise asyncpp::promise< TResult >::make_fulfilled ( TResult && value)
inlinestatic

Get a fufilled promise with the specified value.

Parameters
valueValue for the fulfilled promise
Returns
A promise in its fulfilled state

◆ make_rejected() [1/2]

template<typename TResult >
template<typename TException , typename... Args>
static promise asyncpp::promise< TResult >::make_rejected ( Args &&... args)
inlinestatic

Get a rejected promise with the specified exception.

Template Parameters
TExceptionThe type of the exception to store
Parameters
argsParameters to pass to the exception type constructor
Returns
A promise in its rejected state

◆ make_rejected() [2/2]

template<typename TResult >
static promise asyncpp::promise< TResult >::make_rejected ( std::exception_ptr exception)
inlinestatic

Get a rejected promise with the specified exception.

Parameters
exceptionException to store in the rejected promise
Returns
A promise in its rejected state

◆ on_settle()

template<typename TResult >
void asyncpp::promise< TResult >::on_settle ( std::function< void()> settle_cb)
inline

Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed.

Parameters
cbCallback to invoke as soon as a result is available.

◆ operator co_await()

template<typename TResult >
auto asyncpp::promise< TResult >::operator co_await ( ) const
inlinenoexcept

Asynchronously get the result. If the promise is rejected the rejecting exception gets thrown.

Returns
TResult& Pointer to the result value

◆ reject() [1/2]

template<typename TResult >
template<typename TException , typename... Args>
void asyncpp::promise< TResult >::reject ( Args &&... args)
inline

Reject the promise with an exception.

Exceptions
std::logic_errorif the promise is already fulfilled or rejected
Template Parameters
TExceptionThe exception type to use for rejection
Parameters
argsArguments passed to the constructor of the exception type
Note
Callbacks and waiting coroutines are resumed inside this call

◆ reject() [2/2]

template<typename TResult >
void asyncpp::promise< TResult >::reject ( std::exception_ptr error)
inline

Reject the promise with an exception.

Exceptions
std::logic_errorif the promise is already fulfilled or rejected
Parameters
exThe exception to use for rejection
Note
Callbacks and waiting coroutines are resumed inside this call

◆ then()

template<typename TResult >
void asyncpp::promise< TResult >::then ( std::function< void(const TResult &)> then_cb,
std::function< void(const std::exception_ptr &)> catch_cb )
inline

Register a callback to be executed once a result is available. If the promise is not pending the callback is directly executed.

Parameters
cbCallback to invoke as soon as a result is available.

◆ try_fulfill()

template<typename TResult >
bool asyncpp::promise< TResult >::try_fulfill ( auto && value)
inline

Try to fulfill the promise with a value.

Returns
True if the promise was fulfilled, false if the promise was not pending
Parameters
valueThe value to store inside the promise
Note
Callbacks and waiting coroutines are resumed inside this call.

◆ try_get() [1/2]

template<typename TResult >
TResult * asyncpp::promise< TResult >::try_get ( ) const
inline

Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown.

Returns
TResult* Pointer to the result value or nullptr on timeout

◆ try_get() [2/2]

template<typename TResult >
std::pair< TResult *, std::exception_ptr > asyncpp::promise< TResult >::try_get ( std::nothrow_t ) const
inlinenoexcept

Synchronously try get the result. If the promise is rejected the rejecting exception gets thrown.

Returns
TResult* Pointer to the result value or nullptr on timeout

◆ try_reject() [1/2]

template<typename TResult >
template<typename TException , typename... Args>
bool asyncpp::promise< TResult >::try_reject ( Args &&... args)
inline

Try to reject the promise with an exception.

Returns
True if the promise was rejected, false if the promise was not pending
Template Parameters
TExceptionThe exception type to use for rejection
Parameters
argsArguments passed to the constructor of the exception type
Note
Callbacks and waiting coroutines are resumed inside this call

◆ try_reject() [2/2]

template<typename TResult >
bool asyncpp::promise< TResult >::try_reject ( std::exception_ptr error)
inline

Try to reject the promise with an exception.

Returns
True if the promise was rejected, false if the promise was not pending
Parameters
exThe exception to use for rejection
Note
Callbacks and waiting coroutines are resumed inside this call

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