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"
extern "C" void completion_callback(
void *env_ptr, dittoffi_result_uint64_t ffi_result) {
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() {
auto async_result = ctx->get_future();
continuation_dittoffi_result_uint64_t ffi_continuation = {
.env_ptr = ctx,
.call = completion_callback,
.free = ctx->deleter()};
dittoffi_example_async_throws(ffi_continuation);
return async_future.get();
}
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
-
| ReturnType | The type of the result of the asynchronous operation. |