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

Base class defining logic common to all promise types, aka the "write" end of an awaitable. More...

#include <awaitable.h>

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

Public Member Functions

promise_baseoperator= (const promise_base &)=delete
 Copy assignment is disabled. More...
 
promise_baseoperator= (promise_base &&rhs)=delete
 Move assignment is disabled. 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 = 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...
 
 promise_base ()=default
 Construct a new promise, with empty result. More...
 
 promise_base (const promise_base &)=delete
 Copy construction is disabled. More...
 
 promise_base (promise_base &&rhs)=delete
 Move construction is disabled. More...
 

Protected Attributes

storage_type value = std::monostate{}
 State of the result value. More...
 
std::atomic< uint8_t > state = 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...
 

Friends

class awaitable< T >
 

Detailed Description

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

Base class defining logic common to all promise types, aka the "write" end of an awaitable.

Member Typedef Documentation

◆ storage_type

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

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

Constructor & Destructor Documentation

◆ promise_base() [1/3]

template<typename T >
dpp::detail::promise::promise_base< T >::promise_base ( )
protecteddefault

Construct a new promise, with empty result.

◆ promise_base() [2/3]

template<typename T >
dpp::detail::promise::promise_base< T >::promise_base ( const promise_base< T > &  )
protecteddelete

Copy construction is disabled.

◆ promise_base() [3/3]

template<typename T >
dpp::detail::promise::promise_base< T >::promise_base ( promise_base< T > &&  rhs)
protecteddelete

Move construction is disabled.

awaitable hold a pointer to this object so moving is not possible.

Member Function Documentation

◆ get_awaitable()

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

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 ( )
inline

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

◆ operator=() [1/2]

template<typename T >
promise_base & dpp::detail::promise::promise_base< T >::operator= ( const promise_base< T > &  )
delete

Copy assignment is disabled.

◆ operator=() [2/2]

template<typename T >
promise_base & dpp::detail::promise::promise_base< T >::operator= ( promise_base< T > &&  rhs)
delete

Move assignment is disabled.

◆ release_awaiter()

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

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.

◆ set_exception()

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

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 ( )
inlineprotected

Check if the result is empty, throws otherwise.

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

Friends And Related Function Documentation

◆ awaitable< T >

template<typename T >
friend class awaitable< T >
friend

Member Data Documentation

◆ awaiter

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

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
protected

State of the awaitable tied to this promise.

◆ value

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

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 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ 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