D++ (DPP)
C++ Discord API Bot Library
dpp::detail::task::promise_base< R > Struct Template Reference

Base implementation of task::promise_t, without the logic that would depend on the return type. Meant to be inherited from. More...

+ Inheritance diagram for dpp::detail::task::promise_base< R >:
+ Collaboration diagram for dpp::detail::task::promise_base< R >:

Classes

struct  proxy_awaiter
 Proxy awaitable that wraps any co_await inside the task and checks for cancellation on resumption. More...
 

Public Member Functions

std_coroutine::suspend_never initial_suspend () const noexcept
 Function called by the standard library when the coroutine is created. More...
 
void unhandled_exception ()
 Function called by the standard library when an exception is thrown and not caught in the coroutine. More...
 
template<awaitable_type T>
auto await_transform (T &&expr) const noexcept(noexcept(co_await_resolve(std::forward< T >(expr))))
 Function called whenever co_await is used inside of the task. More...
 
 requires (std::constructible_from< R, Args... >) void emplace_value(Args &&... args)
 Construct the result in place by forwarding the arguments, and by default resume any awaiter. More...
 
 requires (std::convertible_to< const U &, R >) void set_value(const U &v)
 Construct the result by copy, and resume any awaiter. More...
 
 requires (std::convertible_to< U &&, R >) void set_value(U &&v)
 Construct the result by move, and resume any awaiter. More...
 
 requires (std::is_void_v< R >) void set_value()
 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...
 

Public Attributes

std::atomic< boolcancelled = false
 Whether the task is cancelled or not. More...
 

Protected Types

using storage_type = result_t< T >
 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 ()
 Unlinks this promise from its currently linked awaiter and returns it. More...
 

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 R>
struct dpp::detail::task::promise_base< R >

Base implementation of task::promise_t, without the logic that would depend on the return type. Meant to be inherited from.

Member Typedef Documentation

◆ storage_type

template<typename T >
using dpp::detail::promise::promise_base< T >::storage_type = result_t<T>
protectedinherited

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

Member Function Documentation

◆ await_transform()

template<typename R >
template<awaitable_type T>
auto dpp::detail::task::promise_base< R >::await_transform ( T &&  expr) const
inlinenoexcept

Function called whenever co_await is used inside of the task.

Exceptions
dpp::task_cancelled_exceptionOn resumption if the task was cancelled
Returns
proxy_awaiter Returns a proxy awaiter that will check for cancellation on resumption

◆ 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.

◆ initial_suspend()

template<typename R >
std_coroutine::suspend_never dpp::detail::task::promise_base< R >::initial_suspend ( ) const
inlinenoexcept

Function called by the standard library when the coroutine is created.

Returns
std::suspend_never Don't suspend, the coroutine starts immediately.

◆ 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.

Note
This may resume the coroutine on the current thread.
Exceptions
?Any exception thrown by the coroutine if resumed will propagate

◆ release_awaiter()

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

Unlinks this promise from its currently linked awaiter and returns it.

At the time of writing this is only used in the case of a serious internal error in dpp::task. Avoid using this as this will crash if the promise is used after this.

◆ requires() [1/4]

dpp::basic_promise< R >::requires ( std::constructible_from< R , Args... >  ) &&
inlineinherited

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.

◆ requires() [2/4]

dpp::basic_promise< R >::requires ( std::convertible_to< const U &, R >  ) const &
inlineinherited

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.

◆ requires() [3/4]

dpp::basic_promise< R >::requires ( std::convertible_to< U &&, R >  ) &&
inlineinherited

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.

◆ requires() [4/4]

dpp::basic_promise< R >::requires ( std::is_void_v< R >  )
inlineinherited

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.

◆ 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.
?Any exception thrown by the coroutine if resumed will propagate

◆ 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.

◆ unhandled_exception()

template<typename R >
void dpp::detail::task::promise_base< R >::unhandled_exception ( )
inline

Function called by the standard library when an exception is thrown and not caught in the coroutine.

Stores the exception pointer to rethrow on co_await. If the task object is destroyed and was not cancelled, throw instead

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.

◆ cancelled

template<typename R >
std::atomic<bool> dpp::detail::task::promise_base< R >::cancelled = false

Whether the task is cancelled or not.

◆ 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
Note
use .index() instead of std::holds_alternative to support promise_base<std::exception_ptr> and promise_base<std::monostate> :)
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