D++ (DPP)
C++ Discord API Bot Library
dpp::when_any< Args > Class Template Reference

Experimental class to co_await on a bunch of awaitable objects, resuming when the first one completes. On completion, returns a result object that contains the index of the awaitable that finished first. A user can call result::index() and result::get<N>() on the result object to get the result, similar to std::variant. More...

#include <coro/when_any.h>

Classes

struct  awaiter
 Object returned by operator co_await(). Meant to be used by the standard library, not by a user. More...
 
class  result
 Object returned by operator co_await() on resumption. Can be moved but not copied. More...
 

Public Member Functions

 when_any ()=default
 Default constructor. A when_any object created this way holds no state. More...
 
template<typename... Args_>
 when_any (Args_ &&... args)
 Constructor from awaitable objects. Each awaitable is executed immediately and the when_any object can then be co_await-ed later. More...
 
 when_any (const when_any &)=delete
 This object is not copyable. More...
 
 when_any (when_any &&) noexcept=default
 Move constructor. More...
 
 ~when_any ()
 On destruction the when_any will try to call dpp::task::cancel() cancel() on each of its awaitable if they have such a method. More...
 
when_anyoperator= (const when_any &)=delete
 This object is not copyable. More...
 
when_anyoperator= (when_any &&) noexcept=default
 Move assignment operator. More...
 
bool await_ready () const noexcept
 Check whether a call to co_await would suspend. More...
 
awaiter operator co_await () noexcept
 Suspend the caller until any of the awaitables completes. More...
 

Detailed Description

template<typename... Args>
class dpp::when_any< Args >

Experimental class to co_await on a bunch of awaitable objects, resuming when the first one completes. On completion, returns a result object that contains the index of the awaitable that finished first. A user can call result::index() and result::get<N>() on the result object to get the result, similar to std::variant.

See also
when_any::result
Template Parameters
Args...Type of each awaitable to await on

Constructor & Destructor Documentation

◆ when_any() [1/4]

template<typename... Args>
dpp::when_any< Args >::when_any ( )
default

Default constructor. A when_any object created this way holds no state.

◆ when_any() [2/4]

template<typename... Args>
template<typename... Args_>
dpp::when_any< Args >::when_any ( Args_ &&...  args)
inline

Constructor from awaitable objects. Each awaitable is executed immediately and the when_any object can then be co_await-ed later.

Exceptions
???Any exception thrown by the start of each awaitable will propagate to the caller.
Parameters
argsArguments to construct each awaitable from. The when_any object will construct an awaitable for each, it is recommended to pass rvalues or std::move.

◆ when_any() [3/4]

template<typename... Args>
dpp::when_any< Args >::when_any ( const when_any< Args > &  )
delete

This object is not copyable.

◆ when_any() [4/4]

template<typename... Args>
dpp::when_any< Args >::when_any ( when_any< Args > &&  )
defaultnoexcept

Move constructor.

◆ ~when_any()

template<typename... Args>
dpp::when_any< Args >::~when_any ( )
inline

On destruction the when_any will try to call dpp::task::cancel() cancel() on each of its awaitable if they have such a method.

Note
If you are looking to use a custom type with when_any and want it to cancel on its destruction, make sure it has a cancel() method, which will trigger an await_resume() throwing a dpp::task_cancelled_exception. This object will swallow the exception and return cleanly. Any other exception will be thrown back to the resumer.

Member Function Documentation

◆ await_ready()

template<typename... Args>
bool dpp::when_any< Args >::await_ready ( ) const
inlinenoexcept

Check whether a call to co_await would suspend.

Note
This can change from false to true at any point, but not the other way around.
Returns
bool Whether co_await would suspend

◆ operator co_await()

template<typename... Args>
awaiter dpp::when_any< Args >::operator co_await ( )
inlinenoexcept

Suspend the caller until any of the awaitables completes.

See also
result
Exceptions
???On resumption, throws any exception caused by the construction of the result.
Returns
result On resumption, this object returns an object that allows to retrieve the index and result of the awaitable.

◆ operator=() [1/2]

template<typename... Args>
when_any & dpp::when_any< Args >::operator= ( const when_any< Args > &  )
delete

This object is not copyable.

◆ operator=() [2/2]

template<typename... Args>
when_any & dpp::when_any< Args >::operator= ( when_any< Args > &&  )
defaultnoexcept

Move assignment operator.

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