55class AsyncExecutor :
public std::enable_shared_from_this<
56 AsyncExecutor<StatementType, HandleType, RetryPolicyType>> {
57 using FutureWithCallbackType =
typename HandleType::FutureWithCallbackType;
58 using CallbackType = std::function<void(
typename HandleType::ResultOrErrorType)>;
59 using RetryCallbackType = std::function<void()>;
64 RetryPolicyType retryPolicy_;
65 CallbackType onComplete_;
66 RetryCallbackType onRetry_;
69 using OptionalFuture = std::optional<FutureWithCallbackType>;
77 run(boost::asio::io_context& ioc,
78 HandleType
const& handle,
80 CallbackType&& onComplete,
81 RetryCallbackType&& onRetry)
84 struct EnableMakeShared :
public AsyncExecutor<StatementType, HandleType, RetryPolicyType> {
86 boost::asio::io_context& ioc,
88 CallbackType&& onComplete,
89 RetryCallbackType&& onRetry
91 : AsyncExecutor(ioc, std::move(
data), std::move(onComplete), std::move(onRetry))
96 auto ptr = std::make_shared<EnableMakeShared>(
97 ioc, std::move(
data), std::move(onComplete), std::move(onRetry)
104 boost::asio::io_context& ioc,
105 StatementType&&
data,
106 CallbackType&& onComplete,
107 RetryCallbackType&& onRetry
109 : data_{std::move(
data)}
111 , onComplete_{std::move(onComplete)}
112 , onRetry_{std::move(onRetry)}
117 execute(HandleType
const& handle)
119 auto self = this->shared_from_this();
122 auto handler = [
this, &handle, self](
auto&& res)
mutable {
124 onComplete_(std::forward<
decltype(res)>(res));
126 if (retryPolicy_.shouldRetry(res.error())) {
128 retryPolicy_.retry([self, &handle]() { self->execute(handle); });
130 onComplete_(std::forward<
decltype(res)>(res));
137 auto future = future_.template lock<std::scoped_lock>();
138 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:77