55class AsyncExecutor :
public std::enable_shared_from_this<AsyncExecutor<StatementType, HandleType, RetryPolicyType>> {
56 using FutureWithCallbackType =
typename HandleType::FutureWithCallbackType;
57 using CallbackType = std::function<void(
typename HandleType::ResultOrErrorType)>;
58 using RetryCallbackType = std::function<void()>;
63 RetryPolicyType retryPolicy_;
64 CallbackType onComplete_;
65 RetryCallbackType onRetry_;
68 using OptionalFuture = std::optional<FutureWithCallbackType>;
76 run(boost::asio::io_context& ioc,
77 HandleType
const& handle,
79 CallbackType&& onComplete,
80 RetryCallbackType&& onRetry)
83 struct EnableMakeShared :
public AsyncExecutor<StatementType, HandleType, RetryPolicyType> {
85 boost::asio::io_context& ioc,
87 CallbackType&& onComplete,
88 RetryCallbackType&& onRetry
95 auto ptr = std::make_shared<EnableMakeShared>(ioc, std::move(
data), std::move(onComplete), std::move(onRetry));
101 boost::asio::io_context& ioc,
102 StatementType&&
data,
103 CallbackType&& onComplete,
104 RetryCallbackType&& onRetry
106 : data_{std::move(
data)}, retryPolicy_{ioc}, onComplete_{std::move(onComplete)}, onRetry_{std::move(onRetry)}
111 execute(HandleType
const& handle)
113 auto self = this->shared_from_this();
116 auto handler = [
this, &handle, self](
auto&& res)
mutable {
118 onComplete_(std::forward<
decltype(res)>(res));
120 if (retryPolicy_.shouldRetry(res.error())) {
122 retryPolicy_.retry([self, &handle]() { self->execute(handle); });
124 onComplete_(std::forward<
decltype(res)>(res));
131 auto future = future_.template lock<std::scoped_lock>();
132 future->emplace(handle.asyncExecute(data_, std::move(handler)));
static void run(boost::asio::io_context &ioc, HandleType const &handle, StatementType &&data, CallbackType &&onComplete, RetryCallbackType &&onRetry)
Create a new instance of the AsyncExecutor and execute it.
Definition AsyncExecutor.hpp:76