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

Makes a callback safe to hand to code that might fire it more than once. More...

#include <Callback.h>

Public Types

using Callback = std::function<void(Args...)>
 Signature of the wrapped callback.

Public Member Functions

 OnceCallback (Callback callback)
 Wraps a callback.
bool invoke (Args... args)
 Calls the wrapped callback, unless it has already been called.
bool was_invoked () const
 Reports whether the callback has run.

Detailed Description

template<typename... Args>
class stripekit::OnceCallback< Args >

Makes a callback safe to hand to code that might fire it more than once.

StripeKit's callback-form methods must invoke their completion exactly once: calling it twice corrupts whatever is waiting on it, and never calling it hangs the request. Adapters wrap the completion in one of these so the rule holds even on unusual error paths.

auto once = std::make_shared<stripekit::OnceCallback<Result>>(std::move(completion));
transport.send(request, [once](Result result) { once->invoke(std::move(result)); });

Safe to call from several threads at once.

Template Parameters
ArgsArgument types the wrapped callback takes.
See also
Writing an Adapter

Definition at line 40 of file Callback.h.

Member Typedef Documentation

◆ Callback

template<typename... Args>
using stripekit::OnceCallback< Args >::Callback = std::function<void(Args...)>

Signature of the wrapped callback.

Definition at line 43 of file Callback.h.

Constructor & Destructor Documentation

◆ OnceCallback()

template<typename... Args>
stripekit::OnceCallback< Args >::OnceCallback ( Callback callback)
inlineexplicit

Wraps a callback.

Parameters
callbackThe callback to guard. May be empty, in which case invoke() still reports the first call but does nothing.

Definition at line 51 of file Callback.h.

Member Function Documentation

◆ invoke()

template<typename... Args>
bool stripekit::OnceCallback< Args >::invoke ( Args... args)
inline

Calls the wrapped callback, unless it has already been called.

Parameters
argsArguments forwarded to the callback.
Returns
True if this call ran the callback; false if it had already run, in which case the arguments are discarded.

Definition at line 60 of file Callback.h.

◆ was_invoked()

template<typename... Args>
bool stripekit::OnceCallback< Args >::was_invoked ( ) const
inline

Reports whether the callback has run.

Returns
True once invoke() has been called.

Definition at line 83 of file Callback.h.


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