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

A coroutine task. It starts immediately on construction and can be co_await-ed, making it perfect for parallel coroutines returning a value. More...

#include <coro/task.h>

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

Public Member Functions

 task ()=default
 Default constructor, creates a task not bound to a coroutine. More...
 
 task (const task &)=delete
 Copy constructor is disabled. More...
 
 task (task &&other) noexcept
 Move constructor, grabs another task's coroutine handle. More...
 
 ~task ()
 Destructor. More...
 
taskoperator= (const task &)=delete
 Copy assignment is disabled. More...
 
taskoperator= (task &&other) noexcept
 Move assignment, grabs another task's coroutine handle. More...
 
bool done () const noexcept
 Function to check if the task has finished its execution entirely. More...
 
dpp::task< R > & cancel () &noexcept
 Cancel the task, it will stop the next time it uses co_await. On co_await-ing this task, throws dpp::task_cancelled_exception. More...
 
bool await_ready () const
 Check whether or not a call to co_await will suspend the caller. More...
 
auto & operator co_await () &noexcept
 Suspend the current coroutine until the task completes. More...
 
const auto & operator co_await () const &noexcept
 Suspend the current coroutine until the task completes. More...
 
auto && operator co_await () &&noexcept
 Suspend the current coroutine until the task completes. More...
 

Private Member Functions

dpp::task< R > && cancel () &&noexcept
 Cancel the task, it will stop the next time it uses co_await. On co_await-ing this task, throws dpp::task_cancelled_exception. More...
 

Friends

class detail::task::task_base< R >
 Internal use only base class containing common logic between task<R> and task<void>. It also serves to prevent await_suspend and await_resume from being used directly. More...
 

Detailed Description

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

A coroutine task. It starts immediately on construction and can be co_await-ed, making it perfect for parallel coroutines returning a value.

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 our Discord Server.
Template Parameters
RReturn type of the task. Cannot be a reference but can be void.

Constructor & Destructor Documentation

◆ task() [1/3]

template<typename R >
dpp::task< R >::task ( )
default

Default constructor, creates a task not bound to a coroutine.

◆ task() [2/3]

template<typename R >
dpp::task< R >::task ( const task< R > &  )
delete

Copy constructor is disabled.

◆ task() [3/3]

template<typename R >
dpp::task< R >::task ( task< R > &&  other)
noexcept

Move constructor, grabs another task's coroutine handle.

Parameters
otherTask to move the handle from

◆ ~task()

template<typename R >
dpp::task< R >::~task ( )

Destructor.

Destroys the handle.

Warning
The coroutine must be finished before this is called, otherwise it runs the risk of being resumed after it is destroyed, resuming in use-after-free undefined behavior.

Member Function Documentation

◆ await_ready()

template<typename R >
bool dpp::task< R >::await_ready ( ) const

Check whether or not a call to co_await will suspend the caller.

This function is called by the standard library as a first step when using co_await. If it returns true then the caller is not suspended.

Exceptions
logic_exceptionif the task is empty.
Returns
bool Whether not to suspend the caller or not

◆ cancel()

template<typename R >
dpp::task< R > & dpp::task< R >::cancel ( ) &
noexcept

Cancel the task, it will stop the next time it uses co_await. On co_await-ing this task, throws dpp::task_cancelled_exception.

◆ done()

template<typename R >
bool dpp::task< R >::done ( ) const
noexcept

Function to check if the task has finished its execution entirely.

Returns
bool Whether the task is finished.

◆ operator co_await() [1/3]

template<typename R >
auto && dpp::task< R >::operator co_await ( ) &&
inlinenoexcept

Suspend the current coroutine until the task completes.

Exceptions
Onresumption, any exception thrown by the coroutine is propagated to the caller.
Returns
On resumption, this expression evaluates to the result object of type R, as an rvalue reference.

◆ operator co_await() [2/3]

template<typename R >
auto & dpp::task< R >::operator co_await ( ) &
inlinenoexcept

Suspend the current coroutine until the task completes.

Exceptions
Onresumption, any exception thrown by the coroutine is propagated to the caller.
Returns
On resumption, this expression evaluates to the result object of type R, as a reference.

◆ operator co_await() [3/3]

template<typename R >
const auto & dpp::task< R >::operator co_await ( ) const &
inlinenoexcept

Suspend the current coroutine until the task completes.

Exceptions
Onresumption, any exception thrown by the coroutine is propagated to the caller.
Returns
On resumption, this expression evaluates to the result object of type R, as a const reference.

◆ operator=() [1/2]

template<typename R >
task & dpp::task< R >::operator= ( const task< R > &  )
delete

Copy assignment is disabled.

◆ operator=() [2/2]

template<typename R >
task & dpp::task< R >::operator= ( task< R > &&  other)
noexcept

Move assignment, grabs another task's coroutine handle.

Parameters
otherTask to move the handle from

Friends And Related Function Documentation

◆ detail::task::task_base< R >

template<typename R >
friend class detail::task::task_base< R >
friend

Internal use only base class containing common logic between task<R> and task<void>. It also serves to prevent await_suspend and await_resume from being used directly.

Warning
For internal use only, do not use.
See also
operator co_await()
D++ 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