D++ (DPP)
C++ Discord API Bot Library
dpp::detail::promise::promise< T > Class Template Reference

Generic promise class, represents the owning potion of an asynchronous value. More...

+ Inheritance diagram for dpp::detail::promise::promise< T >:
+ Collaboration diagram for dpp::detail::promise::promise< T >:

Public Member Functions

template<bool Notify = true, typename... Args>
 requires (std::constructible_from< T, Args... >) void emplace_value(Args &&... args)
 Construct the result in place by forwarding the arguments, and by default resume any awaiter. More...
 
template<bool Notify = true>
void set_value (const T &v) requires(std
 Construct the result by copy, and resume any awaiter. More...
 
template<bool Notify = true>
void set_value (T &&v) requires(std
 Construct the result by move, and resume any awaiter. More...
 
template<bool Notify = true>
void set_exception (std::exception_ptr ptr)
 Set this promise to an exception and resume any awaiter. More...
 
void notify_awaiter ()
 Notify a currently awaiting coroutine that the result is ready. More...
 
awaitable< T > get_awaitable ()
 Get an awaitable object for this promise. More...
 

Protected Types

using storage_type = std::variant< std::monostate, std::conditional_t< std::is_void_v< T >, empty, T >, std::exception_ptr >
 Variant representing one of either 3 states of the result value : empty, result, exception. More...
 

Protected Member Functions

void throw_if_not_empty ()
 Check if the result is empty, throws otherwise. More...
 
std_coroutine::coroutine_handle release_awaiter ()
 

Protected Attributes

storage_type value = std::monostate{}
 State of the result value. More...
 
std::atomic< uint8_tstate = sf_none
 State of the awaitable tied to this promise. More...
 
std_coroutine::coroutine_handle awaiter = nullptr
 Coroutine handle currently awaiting the completion of this promise. More...
 

Detailed Description

template<typename T>
class dpp::detail::promise::promise< T >

Generic promise class, represents the owning potion of an asynchronous value.

This class is roughly equivalent to std::promise, with the crucial distinction that the promise IS the shared state. As such, the promise needs to be kept alive for the entire time a value can be retrieved.

Template Parameters
TType of the asynchronous value
See also
awaitable

Member Typedef Documentation

◆ storage_type

template<typename T >
using dpp::detail::promise::promise_base< T >::storage_type = std::variant<std::monostate, std::conditional_t<std::is_void_v<T>, empty, T>, std::exception_ptr>
protectedinherited

Variant representing one of either 3 states of the result value : empty, result, exception.

Member Function Documentation

◆ get_awaitable()

template<typename T >
awaitable<T> dpp::detail::promise::promise_base< T >::get_awaitable ( )
inlineinherited

Get an awaitable object for this promise.

Exceptions
dpp::logic_exceptionif get_awaitable has already been called on this object.
Returns
awaitable<T> An object that can be co_await-ed to retrieve the value of this promise.

◆ notify_awaiter()

template<typename T >
void dpp::detail::promise::promise_base< T >::notify_awaiter ( )
inlineinherited

Notify a currently awaiting coroutine that the result is ready.

◆ release_awaiter()

template<typename T >
std_coroutine::coroutine_handle dpp::detail::promise::promise_base< T >::release_awaiter ( )
inlineprotectedinherited

◆ requires()

template<typename T >
template<bool Notify = true, typename... Args>
dpp::detail::promise::promise< T >::requires ( std::constructible_from< T, Args... >  ) &&
inline

Construct the result in place by forwarding the arguments, and by default resume any awaiter.

Template Parameters
NotifyWhether to resume any awaiter or not.
Exceptions
dpp::logic_exceptionif the promise is not empty.

◆ set_exception()

template<typename T >
template<bool Notify = true>
void dpp::detail::promise::promise_base< T >::set_exception ( std::exception_ptr  ptr)
inlineinherited

Set this promise to an exception and resume any awaiter.

Template Parameters
NotifyWhether to resume any awaiter or not.
Exceptions
dpp::logic_exceptionif the promise is not empty.

◆ set_value() [1/2]

template<typename T >
template<bool Notify = true>
void dpp::detail::promise::promise< T >::set_value ( const T &  v)
inline

Construct the result by copy, and resume any awaiter.

Template Parameters
NotifyWhether to resume any awaiter or not.
Exceptions
dpp::logic_exceptionif the promise is not empty.

◆ set_value() [2/2]

template<typename T >
template<bool Notify = true>
void dpp::detail::promise::promise< T >::set_value ( T &&  v)
inline

Construct the result by move, and resume any awaiter.

Template Parameters
NotifyWhether to resume any awaiter or not.
Exceptions
dpp::logic_exceptionif the promise is not empty.

◆ throw_if_not_empty()

template<typename T >
void dpp::detail::promise::promise_base< T >::throw_if_not_empty ( )
inlineprotectedinherited

Check if the result is empty, throws otherwise.

Exceptions
dpp::logic_exceptionif the result isn't empty.

Member Data Documentation

◆ awaiter

template<typename T >
std_coroutine::coroutine_handle dpp::detail::promise::promise_base< T >::awaiter = nullptr
protectedinherited

Coroutine handle currently awaiting the completion of this promise.

◆ state

template<typename T >
std::atomic<uint8_t> dpp::detail::promise::promise_base< T >::state = sf_none
protectedinherited

State of the awaitable tied to this promise.

◆ value

template<typename T >
storage_type dpp::detail::promise::promise_base< T >::value = std::monostate{}
protectedinherited

State of the result value.

See also
storage_type
D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0