2 // execution/detail/as_invocable.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_EXECUTION_DETAIL_AS_INVOCABLE_HPP
12 #define BOOST_ASIO_EXECUTION_DETAIL_AS_INVOCABLE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/atomic_count.hpp>
20 #include <boost/asio/detail/memory.hpp>
21 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/execution/receiver_invocation_error.hpp>
23 #include <boost/asio/execution/set_done.hpp>
24 #include <boost/asio/execution/set_error.hpp>
25 #include <boost/asio/execution/set_value.hpp>
27 #include <boost/asio/detail/push_options.hpp>
34 #if defined(BOOST_ASIO_HAS_MOVE)
36 template <typename Receiver, typename>
41 explicit as_invocable(Receiver& r) BOOST_ASIO_NOEXCEPT
42 : receiver_(boost::asio::detail::addressof(r))
46 as_invocable(as_invocable&& other) BOOST_ASIO_NOEXCEPT
47 : receiver_(other.receiver_)
55 execution::set_done(BOOST_ASIO_MOVE_OR_LVALUE(Receiver)(*receiver_));
58 void operator()() BOOST_ASIO_LVALUE_REF_QUAL BOOST_ASIO_NOEXCEPT
60 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
63 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
64 execution::set_value(BOOST_ASIO_MOVE_CAST(Receiver)(*receiver_));
66 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
70 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
71 execution::set_error(BOOST_ASIO_MOVE_CAST(Receiver)(*receiver_),
72 std::make_exception_ptr(receiver_invocation_error()));
74 #else // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
76 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
78 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
82 #else // defined(BOOST_ASIO_HAS_MOVE)
84 template <typename Receiver, typename>
88 boost::asio::detail::shared_ptr<boost::asio::detail::atomic_count> ref_count_;
90 explicit as_invocable(Receiver& r,
91 const boost::asio::detail::shared_ptr<
92 boost::asio::detail::atomic_count>& c) BOOST_ASIO_NOEXCEPT
93 : receiver_(boost::asio::detail::addressof(r)),
98 as_invocable(const as_invocable& other) BOOST_ASIO_NOEXCEPT
99 : receiver_(other.receiver_),
100 ref_count_(other.ref_count_)
107 if (--(*ref_count_) == 0)
108 execution::set_done(*receiver_);
111 void operator()() BOOST_ASIO_LVALUE_REF_QUAL BOOST_ASIO_NOEXCEPT
113 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
116 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
117 execution::set_value(*receiver_);
120 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
123 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
124 execution::set_error(*receiver_,
125 std::make_exception_ptr(receiver_invocation_error()));
127 #else // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
129 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
131 #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
135 #endif // defined(BOOST_ASIO_HAS_MOVE)
137 template <typename T>
138 struct is_as_invocable : false_type
142 template <typename Function, typename T>
143 struct is_as_invocable<as_invocable<Function, T> > : true_type
147 } // namespace detail
148 } // namespace execution
152 #include <boost/asio/detail/pop_options.hpp>
154 #endif // BOOST_ASIO_EXECUTION_DETAIL_AS_INVOCABLE_HPP