36class AsyncExecutor :
public std::enable_shared_from_this<
37 AsyncExecutor<StatementType, HandleType, RetryPolicyType>> {
38 using FutureWithCallbackType =
typename HandleType::FutureWithCallbackType;
39 using CallbackType = std::function<void(
typename HandleType::ResultOrErrorType)>;
40 using RetryCallbackType = std::function<void()>;
45 RetryPolicyType retryPolicy_;
46 CallbackType onComplete_;
47 RetryCallbackType onRetry_;
50 using OptionalFuture = std::optional<FutureWithCallbackType>;
58 run(boost::asio::io_context& ioc,
59 HandleType
const& handle,
61 CallbackType&& onComplete,
62 RetryCallbackType&& onRetry)
65 struct EnableMakeShared :
public AsyncExecutor<StatementType, HandleType, RetryPolicyType> {
67 boost::asio::io_context& ioc,
69 CallbackType&& onComplete,
70 RetryCallbackType&& onRetry
72 : AsyncExecutor(ioc, std::move(
data), std::move(onComplete), std::move(onRetry))
77 auto ptr = std::make_shared<EnableMakeShared>(
78 ioc, std::move(
data), std::move(onComplete), std::move(onRetry)
85 boost::asio::io_context& ioc,
87 CallbackType&& onComplete,
88 RetryCallbackType&& onRetry
90 : data_{std::move(
data)}
92 , onComplete_{std::move(onComplete)}
93 , onRetry_{std::move(onRetry)}
98 execute(HandleType
const& handle)
100 auto self = this->shared_from_this();
103 auto handler = [
this, &handle, self](
auto&& res)
mutable {
105 onComplete_(std::forward<
decltype(res)>(res));
107 if (retryPolicy_.shouldRetry(res.error())) {
109 retryPolicy_.retry([self, &handle]() { self->execute(handle); });
111 onComplete_(std::forward<
decltype(res)>(res));
118 auto future = future_.template lock<std::scoped_lock>();
119 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:58