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

Search
Loading...
Searching...
No Matches
dpp::async< R > Class Template Reference

A co_await-able object handling an API call in parallel with the caller. More...

`#include <coro/async.h>`

Inheritance diagram for dpp::async< R >:
[legend]
Collaboration diagram for dpp::async< R >:
[legend]

## Public Types

using | result_type = R
| The return type of the API call. Defaults to confirmation_callback_t.

## Public Member Functions

| async (async &&)=default
| Move constructor, moves the awaitable async object.
| async (const async &)=delete
| Copy constructor is disabled.
template<typename Fun, typename... Args>
| async (Fun &&fun, Args &&... args)
| Construct an async object wrapping an invokeable object, the call is made immediately by forwarding to std::invoke and can be awaited later to retrieve the result.
template<typename Obj, typename Fun, typename... Args>
| async (Obj &&obj, Fun &&fun, Args &&... args)
| Construct an async object wrapping an object method, the call is made immediately by forwarding to std::invoke and can be awaited later to retrieve the result.
| ~async ()
| Destructor, signals to the callback that the async object is gone and shouldn't be notified of the result.
bool | await_ready () const
| Check whether or not co_await-ing this would suspend the caller, i.e. if we have the result or not.
void | if_this_causes_an_invalid_read_your_promise_was_destroyed_before_your_awaitable____check_your_promise_lifetime ()
| Title :).
async & | operator= (async &&)=default
| Move assignment operator, moves the awaitable async object.
async & | operator= (const async &)=delete
| Copy assignment operator is disabled.
auto | sync_wait ()
| Blocks this thread and waits for the awaitable to finish.
auto | sync_wait_for (const std::chrono::duration< Rep, Period > &duration)
| Blocks this thread and waits for the awaitable to finish.
auto | sync_wait_until (const std::chrono::time_point< Clock, Duration > &time)
| Blocks this thread and waits for the awaitable to finish.
bool | valid () const noexcept
| Check whether this awaitable refers to a valid promise.

## Protected Types

using | shared_state
using | state_flags

## Protected Member Functions

uint8_t | abandon ()
| Abandons the promise.
auto | sync_wait_impl (auto &&do_wait)
| Implementation for sync_wait. This is code used by sync_wait, sync_wait_for, sync_wait_until.

## Protected Attributes

shared_state * | state_ptr
| Non-owning pointer to the promise, which must be kept alive for the entire lifetime of the awaitable.

## Detailed Description

template<typename R>
class dpp::async< R >

A co_await-able object handling an API call in parallel with the caller.

This class is the return type of the dpp::cluster::co_* methods, but it can also be created manually to wrap any async call.

Remarks
  - The coroutine may be resumed in another thread, do not rely on thread_local variables.

Warning
  - This feature is EXPERIMENTAL. The API may change at any time and there may be bugs. Please report any to GitHub issues or to the D++ Discord server.

Template Parameters

  R | The return type of the API call. Defaults to confirmation_callback_t

## Member Typedef Documentation

## ◆ result_type

template<typename R>

using dpp::async< R >::result_type = R

The return type of the API call. Defaults to confirmation_callback_t.

## ◆ shared_state

using dpp::awaitable< R >::shared_state

protected inherited

## ◆ state_flags

using dpp::awaitable< R >::state_flags

protected inherited

## Constructor & Destructor Documentation

## ◆ async() [1/4]

template<typename R>
template<typename Obj, typename Fun, typename... Args>

dpp::async< R >::async | ( | Obj && | obj,
|  | Fun && | fun,
|  | Args &&... | args )

inline explicit

Construct an async object wrapping an object method, the call is made immediately by forwarding to std::invoke and can be awaited later to retrieve the result.

Parameters

  obj | The object to call the method on
  fun | The method of the object to call. Its last parameter must be a callback taking a parameter of type R
  args | Parameters to pass to the method, excluding the callback

## ◆ async() [2/4]

template<typename R>
template<typename Fun, typename... Args>

dpp::async< R >::async | ( | Fun && | fun,
|  | Args &&... | args )

inline explicit

Construct an async object wrapping an invokeable object, the call is made immediately by forwarding to std::invoke and can be awaited later to retrieve the result.

Parameters

  fun | The object to call using std::invoke. Its last parameter must be a callable taking a parameter of type R
  args | Parameters to pass to the object, excluding the callback

## ◆ async() [3/4]

template<typename R>

dpp::async< R >::async | ( | const async< R > & |  | ) |

delete

Copy constructor is disabled.

## ◆ async() [4/4]

template<typename R>

dpp::async< R >::async | ( | async< R > && |  | ) |

default

Move constructor, moves the awaitable async object.

## ◆ ~async()

template<typename R>

dpp::async< R >::~async | ( |  | ) |

inline

Destructor, signals to the callback that the async object is gone and shouldn't be notified of the result.

## Member Function Documentation

## ◆ abandon()

auto dpp::awaitable< R >::abandon | ( |  | ) |

protected inherited

Abandons the promise.

Set the promise's state to broken and unlinks this awaitable.

Returns
  uint8_t Flags previously held before setting them to broken

## ◆ await_ready()

bool dpp::awaitable< R >::await_ready | ( |  | ) | const

inherited

Check whether or not co_await-ing this would suspend the caller, i.e. if we have the result or not.

Returns
  bool Whether we already have the result or not

## ◆ if_this_causes_an_invalid_read_your_promise_was_destroyed_before_your_awaitable____check_your_promise_lifetime()

void dpp::awaitable< R >::if_this_causes_an_invalid_read_your_promise_was_destroyed_before_your_awaitable____check_your_promise_lifetime | ( |  | ) |

inline inherited

Title :).

We use this in the destructor

## ◆ operator=() [1/2]

template<typename R>

async & dpp::async< R >::operator= | ( | async< R > && |  | ) |

default

Move assignment operator, moves the awaitable async object.

## ◆ operator=() [2/2]

template<typename R>

async & dpp::async< R >::operator= | ( | const async< R > & |  | ) |

delete

Copy assignment operator is disabled.

## ◆ sync_wait()

auto dpp::basic_awaitable< Derived >::sync_wait | ( |  | ) |

inline inherited

Blocks this thread and waits for the awaitable to finish.

Attention
  This will BLOCK THE THREAD. It is likely you want to use co_await instead.

Returns
  If T is void, returns a boolean for which true means the awaitable completed, false means it timed out.
  If T is non-void, returns a std::optional<T> for which an absence of value means timed out.

## ◆ sync_wait_for()

auto dpp::basic_awaitable< Derived >::sync_wait_for | ( | const std::chrono::duration< Rep, Period > & | duration | ) |

inline inherited

Blocks this thread and waits for the awaitable to finish.

Attention
  This will BLOCK THE THREAD. It is likely you want to use co_await instead.

Parameters

  duration | Maximum duration to wait for

Returns
  If T is void, returns a boolean for which true means the awaitable completed, false means it timed out.
  If T is non-void, returns a std::optional<T> for which an absence of value means timed out.

## ◆ sync_wait_impl()

auto dpp::basic_awaitable< Derived >::sync_wait_impl | ( | auto && | do_wait | ) |

inline protected inherited

Implementation for sync_wait. This is code used by sync_wait, sync_wait_for, sync_wait_until.

Template Parameters

  Timed | Whether the wait function times out or not

Parameters

  do_wait | Function to do the actual wait on the cv

Returns
  If T is void, returns a boolean for which true means the awaitable completed, false means it timed out.
  If T is non-void, returns a std::optional<T> for which an absence of value means timed out.

## ◆ sync_wait_until()

auto dpp::basic_awaitable< Derived >::sync_wait_until | ( | const std::chrono::time_point< Clock, Duration > & | time | ) |

inline inherited

Blocks this thread and waits for the awaitable to finish.

Attention
  This will BLOCK THE THREAD. It is likely you want to use co_await instead.

Parameters

  time | Maximum time point to wait for

Returns
  If T is void, returns a boolean for which true means the awaitable completed, false means it timed out.
  If T is non-void, returns a std::optional<T> for which an absence of value means timed out.

## ◆ valid()

bool dpp::awaitable< R >::valid | ( |  | ) | const

noexcept inherited

Check whether this awaitable refers to a valid promise.

Returns
  bool Whether this awaitable refers to a valid promise or not

## Member Data Documentation

## ◆ state_ptr

shared_state* dpp::awaitable< R >::state_ptr

protected inherited

Non-owning pointer to the promise, which must be kept alive for the entire lifetime of the awaitable.

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