Skip to content

Commit c6415e1

Browse files
committed
common/async: use asio::associator for Forwarding/CompletionHandler
asio keeps adding new types of associations. since this was written, get_associated_cancellation_slot() and get_associated_immediate_executor() were added. we can now specialize the asio::associator<> template to forward all of them Signed-off-by: Casey Bodley <[email protected]>
1 parent abaca81 commit c6415e1

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/common/async/bind_handler.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#define CEPH_ASYNC_BIND_HANDLER_H
1717

1818
#include <tuple>
19-
#include <boost/asio/associated_allocator.hpp>
20-
#include <boost/asio/associated_executor.hpp>
19+
#include <boost/asio/associator.hpp>
2120

2221
namespace ceph::async {
2322

@@ -52,25 +51,25 @@ struct CompletionHandler {
5251
void operator()() && {
5352
std::apply(std::move(handler), std::move(args));
5453
}
55-
56-
using allocator_type = boost::asio::associated_allocator_t<Handler>;
57-
allocator_type get_allocator() const noexcept {
58-
return boost::asio::get_associated_allocator(handler);
59-
}
6054
};
6155

6256
} // namespace ceph::async
6357

6458
namespace boost::asio {
6559

66-
// specialize boost::asio::associated_executor<> for CompletionHandler
67-
template <typename Handler, typename Tuple, typename Executor>
68-
struct associated_executor<ceph::async::CompletionHandler<Handler, Tuple>, Executor> {
69-
using type = boost::asio::associated_executor_t<Handler, Executor>;
70-
71-
static type get(const ceph::async::CompletionHandler<Handler, Tuple>& handler,
72-
const Executor& ex = Executor()) noexcept {
73-
return boost::asio::get_associated_executor(handler.handler, ex);
60+
// forward the handler's associated executor, allocator, cancellation slot, etc
61+
template <template <typename, typename> class Associator,
62+
typename Handler, typename Tuple, typename DefaultCandidate>
63+
struct associator<Associator,
64+
ceph::async::CompletionHandler<Handler, Tuple>, DefaultCandidate>
65+
: Associator<Handler, DefaultCandidate>
66+
{
67+
static auto get(const ceph::async::CompletionHandler<Handler, Tuple>& h) noexcept {
68+
return Associator<Handler, DefaultCandidate>::get(h.handler);
69+
}
70+
static auto get(const ceph::async::CompletionHandler<Handler, Tuple>& h,
71+
const DefaultCandidate& c) noexcept {
72+
return Associator<Handler, DefaultCandidate>::get(h.handler, c);
7473
}
7574
};
7675

src/common/async/forward_handler.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#ifndef CEPH_ASYNC_FORWARD_HANDLER_H
1616
#define CEPH_ASYNC_FORWARD_HANDLER_H
1717

18-
#include <boost/asio/associated_allocator.hpp>
19-
#include <boost/asio/associated_executor.hpp>
18+
#include <boost/asio/associator.hpp>
2019

2120
namespace ceph::async {
2221

@@ -47,25 +46,25 @@ struct ForwardingHandler {
4746
void operator()(Args&& ...args) {
4847
std::move(handler)(std::forward<Args>(args)...);
4948
}
50-
51-
using allocator_type = boost::asio::associated_allocator_t<Handler>;
52-
allocator_type get_allocator() const noexcept {
53-
return boost::asio::get_associated_allocator(handler);
54-
}
5549
};
5650

5751
} // namespace ceph::async
5852

5953
namespace boost::asio {
6054

61-
// specialize boost::asio::associated_executor<> for ForwardingHandler
62-
template <typename Handler, typename Executor>
63-
struct associated_executor<ceph::async::ForwardingHandler<Handler>, Executor> {
64-
using type = boost::asio::associated_executor_t<Handler, Executor>;
65-
66-
static type get(const ceph::async::ForwardingHandler<Handler>& handler,
67-
const Executor& ex = Executor()) noexcept {
68-
return boost::asio::get_associated_executor(handler.handler, ex);
55+
// forward the handler's associated executor, allocator, cancellation slot, etc
56+
template <template <typename, typename> class Associator,
57+
typename Handler, typename DefaultCandidate>
58+
struct associator<Associator,
59+
ceph::async::ForwardingHandler<Handler>, DefaultCandidate>
60+
: Associator<Handler, DefaultCandidate>
61+
{
62+
static auto get(const ceph::async::ForwardingHandler<Handler>& h) noexcept {
63+
return Associator<Handler, DefaultCandidate>::get(h.handler);
64+
}
65+
static auto get(const ceph::async::ForwardingHandler<Handler>& h,
66+
const DefaultCandidate& c) noexcept {
67+
return Associator<Handler, DefaultCandidate>::get(h.handler, c);
6968
}
7069
};
7170

0 commit comments

Comments
 (0)