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:127

Constructors

Task<T>() noexcept

An empty task, holding no coroutine.

include/nprpc/task.hpp:194

explicit Task<T>(handle_t h) noexcept

Adopts a coroutine; used by promise_type.

include/nprpc/task.hpp:196

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

Takes o's coroutine, leaving o empty.

include/nprpc/task.hpp:198

Methods

bool await_ready() noexcept

co_await support: whether the task has already finished.

include/nprpc/task.hpp:148

decltype(auto) await_resume()

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

include/nprpc/task.hpp:158

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

co_await support: resumes h when the task finishes.

include/nprpc/task.hpp:151

bool done() const noexcept

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

include/nprpc/task.hpp:171

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:165

void rethrow_if_exception() const

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

include/nprpc/task.hpp:176

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:185

bool valid() const noexcept

Whether the task holds a coroutine.

include/nprpc/task.hpp:213

Operators

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

Destroys the current coroutine and takes o's.

include/nprpc/task.hpp:200

Type aliases

using handle_t = std::coroutine_handle<promise_type>

The coroutine handle type.

include/nprpc/task.hpp:143

Types

struct promise_type : detail::TaskPromiseBase<T>

Coroutine machinery; not called directly.

include/nprpc/task.hpp:130