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 
11 #ifndef BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP
12 #define BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/memory.hpp>
20 #include <boost/asio/detail/type_traits.hpp>
21 #include <boost/asio/execution/detail/as_invocable.hpp>
22 #include <boost/asio/execution/execute.hpp>
23 #include <boost/asio/execution/set_error.hpp>
24 #include <boost/asio/traits/start_member.hpp>
25 
26 #include <boost/asio/detail/push_options.hpp>
27 
28 namespace boost {
29 namespace asio {
30 namespace execution {
31 namespace detail {
32 
33 template <typename Executor, typename Receiver>
34 struct as_operation
35 {
36   typename remove_cvref<Executor>::type ex_;
37   typename remove_cvref<Receiver>::type receiver_;
38 #if !defined(BOOST_ASIO_HAS_MOVE)
39   boost::asio::detail::shared_ptr<boost::asio::detail::atomic_count> ref_count_;
40 #endif // !defined(BOOST_ASIO_HAS_MOVE)
41 
42   template <typename E, typename R>
as_operationboost::asio::execution::detail::as_operation43   explicit as_operation(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(R) r)
44     : ex_(BOOST_ASIO_MOVE_CAST(E)(e)),
45       receiver_(BOOST_ASIO_MOVE_CAST(R)(r))
46 #if !defined(BOOST_ASIO_HAS_MOVE)
47       , ref_count_(new boost::asio::detail::atomic_count(1))
48 #endif // !defined(BOOST_ASIO_HAS_MOVE)
49   {
50   }
51 
startboost::asio::execution::detail::as_operation52   void start() BOOST_ASIO_NOEXCEPT
53   {
54 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
55     try
56     {
57 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
58       execution::execute(
59           BOOST_ASIO_MOVE_CAST(typename remove_cvref<Executor>::type)(ex_),
60           as_invocable<typename remove_cvref<Receiver>::type,
61               Executor>(receiver_
62 #if !defined(BOOST_ASIO_HAS_MOVE)
63                 , ref_count_
64 #endif // !defined(BOOST_ASIO_HAS_MOVE)
65               ));
66 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
67     }
68     catch (...)
69     {
70 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
71       execution::set_error(
72           BOOST_ASIO_MOVE_OR_LVALUE(
73             typename remove_cvref<Receiver>::type)(
74               receiver_),
75           std::current_exception());
76 #else // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
77       std::terminate();
78 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
79     }
80 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
81   }
82 };
83 
84 } // namespace detail
85 } // namespace execution
86 namespace traits {
87 
88 #if !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
89 
90 template <typename Executor, typename Receiver>
91 struct start_member<
92     boost::asio::execution::detail::as_operation<Executor, Receiver> >
93 {
94   BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
95   BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
96   typedef void result_type;
97 };
98 
99 #endif // !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
100 
101 } // namespace traits
102 } // namespace asio
103 } // namespace boost
104 
105 #include <boost/asio/detail/pop_options.hpp>
106 
107 #endif // BOOST_ASIO_EXECUTION_DETAIL_AS_OPERATION_HPP
108