StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
stripekit::Task< TResult > Class Template Reference

The result of an asynchronous StripeKit operation. More...

#include <Async.h>

Classes

struct  promise_type
 Coroutine promise type. More...

Public Member Functions

 Task ()=default
 Creates a task with no coroutine attached.
 ~Task ()
 Destroys the task and its coroutine state.
 Task (std::coroutine_handle< promise_type > coroutine)
 Adopts an existing coroutine handle.
 Task (const Task &)=delete
Task & operator= (const Task &)=delete
 Task (Task &&other) noexcept
 Takes over another task's coroutine, leaving it empty.
Task & operator= (Task &&other) noexcept
 Takes over another task's coroutine, leaving it empty.
bool is_ready () const
 Reports whether the value is already available.
TResult result ()
 Waits for the operation to finish and returns its value.
auto operator co_await ()
 Makes the task awaitable, so co_await yields its value.

Detailed Description

template<typename TResult>
class stripekit::Task< TResult >

The result of an asynchronous StripeKit operation.

Every *_async method returns one of these. co_await it to get the value:

const stripekit::Customer customer = co_await stripe->customers().create_async(request);
A Stripe customer: the account everything else is billed against.
Definition Models.h:28

Outside a coroutine, result() blocks until the value is ready. That is convenient in command-line tools and tests, but must never be done on a thread that is running an event loop, since the work being waited on may need that loop to make progress.

The task starts on the first co_await or result(), not on construction, so a task that is created and dropped never runs. Tasks are move-only, and each one produces its value once.

Template Parameters
TResultType of value the operation produces.

Definition at line 45 of file Async.h.

Constructor & Destructor Documentation

◆ Task() [1/3]

template<typename TResult>
stripekit::Task< TResult >::Task ( )
default

Creates a task with no coroutine attached.

Awaiting or querying an empty task throws.

◆ ~Task()

template<typename TResult>
stripekit::Task< TResult >::~Task ( )
inline

Destroys the task and its coroutine state.

Definition at line 142 of file Async.h.

◆ Task() [2/3]

template<typename TResult>
stripekit::Task< TResult >::Task ( std::coroutine_handle< promise_type > coroutine)
inlineexplicit

Adopts an existing coroutine handle.

Parameters
coroutineThe coroutine this task takes ownership of.

Definition at line 153 of file Async.h.

◆ Task() [3/3]

template<typename TResult>
stripekit::Task< TResult >::Task ( Task< TResult > && other)
inlinenoexcept

Takes over another task's coroutine, leaving it empty.

Parameters
otherThe task to move from.

Definition at line 163 of file Async.h.

Member Function Documentation

◆ operator=()

template<typename TResult>
Task & stripekit::Task< TResult >::operator= ( Task< TResult > && other)
inlinenoexcept

Takes over another task's coroutine, leaving it empty.

Parameters
otherThe task to move from.
Returns
This task.

Definition at line 171 of file Async.h.

◆ is_ready()

template<typename TResult>
bool stripekit::Task< TResult >::is_ready ( ) const
inline

Reports whether the value is already available.

Returns
True when the operation has finished, or the task is empty.

Definition at line 186 of file Async.h.

◆ result()

template<typename TResult>
TResult stripekit::Task< TResult >::result ( )
inline

Waits for the operation to finish and returns its value.

Starts the operation if it has not started yet, then blocks the calling thread. Do not call this from an event loop thread: the work being waited on may need that loop to run, which would deadlock. Prefer co_await.

Returns
The produced value, moved out of the task.
Exceptions
std::runtime_errorThe task is empty.
StripeApiExceptionOr any other exception the operation threw.

Definition at line 205 of file Async.h.

◆ operator co_await()

template<typename TResult>
auto stripekit::Task< TResult >::operator co_await ( )
inline

Makes the task awaitable, so co_await yields its value.

Starts the operation if it has not started yet, suspends the awaiting coroutine, and resumes it with the value. An exception thrown by the operation is rethrown at the co_await.

Returns
The awaiter used by the compiler.

Definition at line 233 of file Async.h.


The documentation for this class was generated from the following file: