NPRPC docs

class Task

C++

template <typename T = void> class Task

A coroutine result: Task<T> f() { co_return value; }.

Starts running as soon as it is called (there is no initial suspension) and needs no executor. Wait for it with co_await task inside another coroutine, or task.get() from ordinary code. Generated proxies return one from each <method>Async, and servants return one from streaming methods.

Move-only; destroying a Task destroys its coroutine frame, so keep it alive until it is done.

include/nprpc/task.hpp:241

Constructors

Task<T>() noexcept

An empty task, holding no coroutine.

include/nprpc/task.hpp:314

explicit Task<T>(handle_t h) noexcept

Adopts a coroutine; used by promise_type.

include/nprpc/task.hpp:316

Task<T>(Task<T> &&o) noexcept

Takes o's coroutine, leaving o empty.

include/nprpc/task.hpp:318

Methods

bool await_ready() noexcept

co_await support: whether the task has already finished.

include/nprpc/task.hpp:262

decltype(auto) await_resume()

co_await support: the result, or the exception the task threw.

include/nprpc/task.hpp:273

bool await_suspend(std::coroutine_handle<> h) noexcept

co_await support: resumes h when the task finishes, or carries straight on if it already has.

include/nprpc/task.hpp:266

bool done() const noexcept

Whether the task has finished (or holds no coroutine).

include/nprpc/task.hpp:291

decltype(auto) get()

Blocks the calling thread until the task finishes, then returns its result or rethrows its exception. Do not call it on the thread the task needs in order to finish.

include/nprpc/task.hpp:280

void rethrow_if_exception() const

Rethrows the task's exception if it finished with one.

include/nprpc/task.hpp:296

void set_completion_handler(std::move_only_function<void (std::exception_ptr)> handler)

Calls handler when the task finishes, with its exception or null. For fire-and-forget tasks that still need to report failure.

include/nprpc/task.hpp:305

bool valid() const noexcept

Whether the task holds a coroutine.

include/nprpc/task.hpp:333

Operators

Task<T> &operator=(Task<T> &&o) noexcept

Destroys the current coroutine and takes o's.

include/nprpc/task.hpp:320

Type aliases

using handle_t = std::coroutine_handle<promise_type>

The coroutine handle type.

include/nprpc/task.hpp:257

Types

struct promise_type : detail::TaskPromiseBase<T>

Coroutine machinery; not called directly.

include/nprpc/task.hpp:244