| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
dpp::basic_promise< T > Class Template Reference

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

`#include <awaitable.h>`

Inheritance diagram for dpp::basic_promise< T >:
[legend]
Collaboration diagram for dpp::basic_promise< T >:
[legend]

## 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.
awaitable< T > | get_awaitable ()
| Get an awaitable object for this promise.
void | notify_awaiter ()
| Notify a currently awaiting coroutine that the result is ready.
template<bool Notify = true>
void | set_exception (std::exception_ptr ptr)
| Set this promise to an exception and resume any awaiter.
template<bool Notify = true> requires (std::is_void_v<T>)
void | set_value ()
| Construct a void result, and resume any awaiter.
template<bool Notify = true, typename U = T> requires (std::convertible_to<U&&, T>)
void | set_value (U &&v)
| Construct the result by forwarding reference, and resume any awaiter.

## Protected Types

using | storage_type = result_t<T>
| Variant representing one of either 3 states of the result value : empty, result, exception.

## Protected Member Functions

std_coroutine::coroutine_handle | release_awaiter ()
| Unlinks this promise from its currently linked awaiter and returns it.
void | throw_if_not_empty ()
| Check if the result is empty, throws otherwise.

## Protected Attributes

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

## Detailed Description

template<typename T>
class dpp::basic_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

  T | Type of the asynchronous value

See also
  awaitable

## Member Typedef Documentation

## ◆ storage_type

template<typename T>

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

protected inherited

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

## Member Function Documentation

## ◆ emplace_value()

template<typename T>
template<bool Notify = true, typename... Args>
requires (std::constructible_from<T, Args...>)

void dpp::basic_promise< T >::emplace_value | ( | Args &&... | args | ) |

inline

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

Template Parameters

  Notify | Whether to resume any awaiter or not.

Exceptions

  dpp::logic_exception | if the promise is not empty.

## ◆ get_awaitable()

template<typename T>

awaitable< T > dpp::detail::promise::promise_base< T >::get_awaitable | ( |  | ) |

inline inherited

Get an awaitable object for this promise.

Exceptions

  dpp::logic_exception | if 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 inherited

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

inline protected inherited

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 inherited

Set this promise to an exception and resume any awaiter.

Template Parameters

  Notify | Whether to resume any awaiter or not.

Exceptions

  dpp::logic_exception | if the promise is not empty.
  ? | Any exception thrown by the coroutine if resumed will propagate

## ◆ set_value() [1/2]

template<typename T>
template<bool Notify = true>
requires (std::is_void_v<T>)

void dpp::basic_promise< T >::set_value | ( |  | ) |

inline

Construct a void result, and resume any awaiter.

Template Parameters

  Notify | Whether to resume any awaiter or not.

Exceptions

  dpp::logic_exception | if the promise is not empty.

## ◆ set_value() [2/2]

template<typename T>
template<bool Notify = true, typename U = T>
requires (std::convertible_to<U&&, T>)

void dpp::basic_promise< T >::set_value | ( | U && | v | ) |

inline

Construct the result by forwarding reference, and resume any awaiter.

Template Parameters

  Notify | Whether to resume any awaiter or not.

Exceptions

  dpp::logic_exception | if the promise is not empty.

## ◆ throw_if_not_empty()

template<typename T>

void dpp::detail::promise::promise_base< T >::throw_if_not_empty | ( |  | ) |

inline protected inherited

Check if the result is empty, throws otherwise.

Exceptions

  dpp::logic_exception | if the result isn't empty.

## Member Data Documentation

## ◆ awaiter

template<typename T>

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

protected inherited

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 inherited

State of the awaitable tied to this promise.

## ◆ value

template<typename T>

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

protected inherited

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.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0