| 1 | // |
| 2 | // Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | |
| 8 | #ifndef BOOST_BEAST_CORE_DETAIL_FILTERING_CANCELLATION_SLOT_HPP |
| 9 | #define BOOST_BEAST_CORE_DETAIL_FILTERING_CANCELLATION_SLOT_HPP |
| 10 | |
| 11 | #include <boost/beast/core/detail/config.hpp> |
| 12 | #include <boost/asio/cancellation_signal.hpp> |
| 13 | |
| 14 | namespace boost { |
| 15 | namespace beast { |
| 16 | namespace detail { |
| 17 | |
| 18 | template<typename CancellationSlot = net::cancellation_slot> |
| 19 | struct filtering_cancellation_slot : CancellationSlot |
| 20 | { |
| 21 | template<typename ... Args> |
| 22 | filtering_cancellation_slot(net::cancellation_type type, Args && ... args) |
| 23 | : CancellationSlot(std::forward<Args>(args)...), type(type) {} |
| 24 | |
| 25 | net::cancellation_type type = net::cancellation_type::terminal; |
| 26 | |
| 27 | using CancellationSlot::operator=; |
| 28 | |
| 29 | template<typename Handler> |
| 30 | struct handler_wrapper |
| 31 | { |
| 32 | Handler handler; |
| 33 | const net::cancellation_type type; |
| 34 | |
| 35 | template<typename ... Args> |
| 36 | handler_wrapper(net::cancellation_type type, Args && ... args) |
| 37 | : handler(std::forward<Args>(args)...), |
| 38 | type(type) {} |
| 39 | |
| 40 | void operator()(net::cancellation_type tp) |
| 41 | { |
| 42 | if ((tp & type) != net::cancellation_type::none) |
| 43 | handler(tp); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | template <typename CancellationHandler, typename ... Args> |
| 48 | CancellationHandler& emplace(Args && ... args) |
| 49 | { |
| 50 | return CancellationSlot::template emplace<handler_wrapper<CancellationHandler>>( |
| 51 | type, std::forward<Args>(args)...).handler; |
| 52 | } |
| 53 | |
| 54 | template <typename CancellationHandler> |
| 55 | CancellationHandler& assign(CancellationHandler && ch) |
| 56 | { |
| 57 | return CancellationSlot::template emplace<handler_wrapper<CancellationHandler>>( |
| 58 | type, std::forward<CancellationHandler>(ch)).handler; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | #endif //BOOST_BEAST_CORE_DETAIL_FILTERING_CANCELLATION_SLOT_HPP |
| 66 | |