as_operation.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // execution/detail/as_operation.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP
  11. #define BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/memory.hpp>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/execution/detail/as_invocable.hpp>
  19. #include <boost/asio/execution/execute.hpp>
  20. #include <boost/asio/execution/set_error.hpp>
  21. #include <boost/asio/traits/start_member.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace execution {
  26. namespace detail {
  27. template <typename Executor, typename Receiver>
  28. struct as_operation
  29. {
  30. typename remove_cvref<Executor>::type ex_;
  31. typename remove_cvref<Receiver>::type receiver_;
  32. #if !defined(BOOST_ASIO_HAS_MOVE)
  33. boost::asio::detail::shared_ptr<boost::asio::detail::atomic_count> ref_count_;
  34. #endif // !defined(BOOST_ASIO_HAS_MOVE)
  35. template <typename E, typename R>
  36. explicit as_operation(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(R) r)
  37. : ex_(BOOST_ASIO_MOVE_CAST(E)(e)),
  38. receiver_(BOOST_ASIO_MOVE_CAST(R)(r))
  39. #if !defined(BOOST_ASIO_HAS_MOVE)
  40. , ref_count_(new boost::asio::detail::atomic_count(1))
  41. #endif // !defined(BOOST_ASIO_HAS_MOVE)
  42. {
  43. }
  44. void start() BOOST_ASIO_NOEXCEPT
  45. {
  46. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  47. try
  48. {
  49. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  50. execution::execute(
  51. BOOST_ASIO_MOVE_CAST(typename remove_cvref<Executor>::type)(ex_),
  52. as_invocable<typename remove_cvref<Receiver>::type,
  53. Executor>(receiver_
  54. #if !defined(BOOST_ASIO_HAS_MOVE)
  55. , ref_count_
  56. #endif // !defined(BOOST_ASIO_HAS_MOVE)
  57. ));
  58. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  59. }
  60. catch (...)
  61. {
  62. #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
  63. execution::set_error(
  64. BOOST_ASIO_MOVE_OR_LVALUE(
  65. typename remove_cvref<Receiver>::type)(
  66. receiver_),
  67. std::current_exception());
  68. #else // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
  69. std::terminate();
  70. #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
  71. }
  72. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  73. }
  74. };
  75. } // namespace detail
  76. } // namespace execution
  77. namespace traits {
  78. #if !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
  79. template <typename Executor, typename Receiver>
  80. struct start_member<
  81. boost::asio::execution::detail::as_operation<Executor, Receiver> >
  82. {
  83. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  84. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  85. typedef void result_type;
  86. };
  87. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
  88. } // namespace traits
  89. } // namespace asio
  90. } // namespace boost
  91. #include <boost/asio/detail/pop_options.hpp>
  92. #endif // BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP