Ditto 4.11.0
 
Loading...
Searching...
No Matches
ditto::AsyncContext< ReturnType > Class Template Reference

Represents the result of an asynchronous FFI operation. More...

Inheritance diagram for ditto::AsyncContext< ReturnType >:
ditto::AsyncContextBase

Public Types

typedef ReturnType return_type
 

Public Member Functions

 AsyncContext ()=default
 
 AsyncContext (const AsyncContext &)=delete
 
AsyncContextoperator= (const AsyncContext &)=delete
 
 AsyncContext (AsyncContext &&)=delete
 
AsyncContextoperator= (AsyncContext &&)=delete
 
void set_value (ReturnType value)
 Set the value to be returned.
 
void set_exception (std::exception_ptr e)
 Set an exception to be thrown.
 
std::future< ReturnType > get_future ()
 Get a future that can be used to wait for the return value.
 

Public Attributes

void(*)(void *) get_deleter ()
 Get the deleter function for this context.
 

Detailed Description

template<typename ReturnType>
class ditto::AsyncContext< ReturnType >

Represents the result of an asynchronous FFI operation.

This class is used to wrap the result of an asynchronous FFI operation. The async operation's continuation/callback can set the value of a std::promise, and the original caller can wait for that result using a std::future.

Example, calling a (fictional) FFI function that returns a uint64_t:

#include "async_helpers.hpp"
// Callback to be called when the FFI function completes
extern "C" void completion_callback(
void *env_ptr, dittoffi_result_uint64_t ffi_result) {
auto ctx = static_cast<AsyncContext<uint64_t> *>(env_ptr);
if (ffi_result.error != nullptr) {
ctx->set_exception(
std::make_exception_ptr(DittoError(ffi_result.error)));
return;
}
ctx->set_value(result.success);
}
std::string get_string_example() {
// Create an AsyncContext to hold the result of the async operation
auto ctx = new AsyncContext<uint64_t>();
// The context will be deleted wheen the callback completes,
// so we need to get a future before calling the FFI operation.
auto async_result = ctx->get_future();
// Create a continuation to pass to the FFI operation
continuation_dittoffi_result_uint64_t ffi_continuation = {
.env_ptr = ctx,
.call = completion_callback,
.free = ctx->deleter()};
dittoffi_example_async_throws(ffi_continuation);
// Wait for the result of the async operation
return async_future.get();
}
AsyncContext()=default
All errors that are thrown by the Ditto SDK are wrapped as a DittoError.
Definition Errors.hpp:42
See also
DITTO_DEFINE_ASYNC_CONTEXT_CALLBACK() for a macro that will generate a callback function that can be used with this class.
Template Parameters
ReturnTypeThe type of the result of the asynchronous operation.

Constructor & Destructor Documentation

◆ AsyncContext()

template<typename ReturnType>
ditto::AsyncContext< ReturnType >::AsyncContext ( )
default

Constructor

Member Function Documentation

◆ get_future()

template<typename ReturnType>
std::future< ReturnType > ditto::AsyncContext< ReturnType >::get_future ( )
inline

Get a future that can be used to wait for the return value.

An exception will be thrown if get_future() is called more than once. Use std::future::share() if you need to share the future with multiple consumers.