2023-10-05 13:59:32 +02:00
|
|
|
#ifndef ASYNC_MQTT5_CANCELLABLE_HANDLER_HPP
|
|
|
|
#define ASYNC_MQTT5_CANCELLABLE_HANDLER_HPP
|
|
|
|
|
|
|
|
#include <boost/asio/associated_allocator.hpp>
|
2024-01-09 15:18:58 +01:00
|
|
|
#include <boost/asio/associated_executor.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/asio/associated_cancellation_slot.hpp>
|
2024-01-09 15:18:58 +01:00
|
|
|
#include <boost/asio/cancellation_state.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/asio/post.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
#include <boost/asio/prepend.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
#include <async_mqtt5/detail/async_traits.hpp>
|
|
|
|
|
|
|
|
namespace async_mqtt5::detail {
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
template <typename Handler, typename Executor>
|
2023-10-05 13:59:32 +02:00
|
|
|
class cancellable_handler {
|
|
|
|
Executor _executor;
|
2024-01-09 15:18:58 +01:00
|
|
|
Handler _handler;
|
|
|
|
tracking_type<Handler, Executor> _handler_ex;
|
|
|
|
asio::cancellation_state _cancellation_state;
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
public:
|
2024-01-09 15:18:58 +01:00
|
|
|
cancellable_handler(Handler&& handler, const Executor& ex) :
|
|
|
|
_executor(ex),
|
|
|
|
_handler(std::move(handler)),
|
|
|
|
_handler_ex(tracking_executor(_handler, ex)),
|
|
|
|
_cancellation_state(
|
|
|
|
asio::get_associated_cancellation_slot(_handler),
|
|
|
|
asio::enable_total_cancellation {},
|
|
|
|
asio::enable_terminal_cancellation {}
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
|
|
|
|
cancellable_handler(cancellable_handler&& other) noexcept = default;
|
2023-10-05 13:59:32 +02:00
|
|
|
cancellable_handler(const cancellable_handler&) = delete;
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
using executor_type = tracking_type<Handler, Executor>;
|
|
|
|
executor_type get_executor() const noexcept {
|
|
|
|
return _handler_ex;
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
using allocator_type = asio::associated_allocator_t<Handler>;
|
|
|
|
allocator_type get_allocator() const noexcept {
|
2024-01-09 15:18:58 +01:00
|
|
|
return asio::get_associated_allocator(_handler);
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
using cancellation_slot_type = asio::associated_cancellation_slot_t<Handler>;
|
|
|
|
cancellation_slot_type get_cancellation_slot() const noexcept {
|
|
|
|
return _cancellation_state.slot();
|
|
|
|
}
|
2023-10-05 13:59:32 +02:00
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::cancellation_type_t cancelled() const {
|
|
|
|
return _cancellation_state.cancelled();
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void complete(Args&&... args) {
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::get_associated_cancellation_slot(_handler).clear();
|
|
|
|
std::move(_handler)(std::forward<Args>(args)...);
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void complete_post(Args&&... args) {
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::get_associated_cancellation_slot(_handler).clear();
|
2023-10-05 13:59:32 +02:00
|
|
|
asio::post(
|
2023-11-28 11:04:00 +01:00
|
|
|
_executor,
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::prepend(std::move(_handler), std::forward<Args>(args)...)
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end async_mqtt5::detail
|
|
|
|
|
|
|
|
#endif // !ASYNC_MQTT5_CANCELLABLE_HANDLER_HPP
|