detached.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_DETACHED_HPP
  8. #define BOOST_COBALT_DETAIL_DETACHED_HPP
  9. #include <boost/cobalt/detail/exception.hpp>
  10. #include <boost/cobalt/detail/forward_cancellation.hpp>
  11. #include <boost/cobalt/detail/wrapper.hpp>
  12. #include <boost/cobalt/detail/this_thread.hpp>
  13. #include <boost/cobalt/op.hpp>
  14. #include <boost/asio/cancellation_signal.hpp>
  15. #include <boost/core/exchange.hpp>
  16. #include <coroutine>
  17. #include <optional>
  18. #include <utility>
  19. #include <boost/asio/bind_allocator.hpp>
  20. namespace boost::cobalt
  21. {
  22. struct detached;
  23. namespace detail
  24. {
  25. struct detached_promise
  26. : promise_memory_resource_base,
  27. promise_cancellation_base<asio::cancellation_slot, asio::enable_total_cancellation>,
  28. promise_throw_if_cancelled_base,
  29. enable_awaitables<detached_promise>,
  30. enable_await_allocator<detached_promise>,
  31. enable_await_executor<detached_promise>,
  32. enable_await_deferred
  33. {
  34. using promise_cancellation_base<asio::cancellation_slot, asio::enable_total_cancellation>::await_transform;
  35. using promise_throw_if_cancelled_base::await_transform;
  36. using enable_awaitables<detached_promise>::await_transform;
  37. using enable_await_allocator<detached_promise>::await_transform;
  38. using enable_await_executor<detached_promise>::await_transform;
  39. using enable_await_deferred::await_transform;
  40. [[nodiscard]] detached get_return_object();
  41. std::suspend_never await_transform(
  42. cobalt::this_coro::reset_cancellation_source_t<asio::cancellation_slot> reset) noexcept
  43. {
  44. this->reset_cancellation_source(reset.source);
  45. return {};
  46. }
  47. using executor_type = executor;
  48. executor_type exec;
  49. const executor_type & get_executor() const {return exec;}
  50. template<typename ... Args>
  51. detached_promise(Args & ...args)
  52. :
  53. #if !defined(BOOST_COBALT_NO_PMR)
  54. promise_memory_resource_base(detail::get_memory_resource_from_args(args...)),
  55. #endif
  56. exec{detail::get_executor_from_args(args...)}
  57. {
  58. }
  59. std::suspend_never initial_suspend() noexcept {return {};}
  60. std::suspend_never final_suspend() noexcept {return {};}
  61. void return_void() {}
  62. #if !defined(BOOST_NO_EXCEPTIONS)
  63. void unhandled_exception() { throw ; }
  64. #endif
  65. };
  66. }
  67. }
  68. #endif //BOOST_COBALT_DETAIL_DETACHED_HPP