forward_cancellation.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #ifndef BOOST_COBALT_DETAIL_FORWARD_CANCELLATION_HPP
  8. #define BOOST_COBALT_DETAIL_FORWARD_CANCELLATION_HPP
  9. #include <boost/cobalt/config.hpp>
  10. #include <boost/asio/cancellation_signal.hpp>
  11. #include <boost/asio/dispatch.hpp>
  12. namespace boost::cobalt
  13. {
  14. // Requests cancellation where a successful cancellation results
  15. // in no apparent side effects and where the op can re-awaited.
  16. template<typename Awaitable>
  17. concept interruptible =
  18. ( std::is_rvalue_reference_v<Awaitable> && requires (Awaitable && t) {std::move(t).interrupt_await();})
  19. || (!std::is_rvalue_reference_v<Awaitable> && requires (Awaitable t) {t.interrupt_await();});
  20. }
  21. namespace boost::cobalt::detail
  22. {
  23. struct forward_cancellation
  24. {
  25. asio::cancellation_signal &cancel_signal;
  26. forward_cancellation(asio::cancellation_signal &cancel_signal) : cancel_signal(cancel_signal) {}
  27. void operator()(asio::cancellation_type ct) const
  28. {
  29. cancel_signal.emit(ct);
  30. }
  31. };
  32. }
  33. #endif //BOOST_COBALT_DETAIL_FORWARD_CANCELLATION_HPP