channel_send_functions.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // experimental/detail/channel_send_functions.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_EXPERIMENTAL_DETAIL_CHANNEL_SEND_FUNCTIONS_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_FUNCTIONS_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/async_result.hpp>
  17. #include <boost/asio/detail/completion_message.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/system/error_code.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace experimental {
  24. namespace detail {
  25. template <typename Derived, typename Executor, typename... Signatures>
  26. class channel_send_functions;
  27. template <typename Derived, typename Executor, typename R, typename... Args>
  28. class channel_send_functions<Derived, Executor, R(Args...)>
  29. {
  30. public:
  31. template <typename... Args2>
  32. enable_if_t<
  33. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  34. int, Args2...>::value,
  35. bool
  36. > try_send(Args2&&... args)
  37. {
  38. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  39. Derived* self = static_cast<Derived*>(this);
  40. return self->service_->template try_send<message_type>(
  41. self->impl_, false, static_cast<Args2&&>(args)...);
  42. }
  43. template <typename... Args2>
  44. enable_if_t<
  45. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  46. int, Args2...>::value,
  47. bool
  48. > try_send_via_dispatch(Args2&&... args)
  49. {
  50. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  51. Derived* self = static_cast<Derived*>(this);
  52. return self->service_->template try_send<message_type>(
  53. self->impl_, true, static_cast<Args2&&>(args)...);
  54. }
  55. template <typename... Args2>
  56. enable_if_t<
  57. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  58. int, Args2...>::value,
  59. std::size_t
  60. > try_send_n(std::size_t count, Args2&&... args)
  61. {
  62. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  63. Derived* self = static_cast<Derived*>(this);
  64. return self->service_->template try_send_n<message_type>(
  65. self->impl_, count, false, static_cast<Args2&&>(args)...);
  66. }
  67. template <typename... Args2>
  68. enable_if_t<
  69. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  70. int, Args2...>::value,
  71. std::size_t
  72. > try_send_n_via_dispatch(std::size_t count, Args2&&... args)
  73. {
  74. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  75. Derived* self = static_cast<Derived*>(this);
  76. return self->service_->template try_send_n<message_type>(
  77. self->impl_, count, true, static_cast<Args2&&>(args)...);
  78. }
  79. template <
  80. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  81. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  82. auto async_send(Args... args,
  83. CompletionToken&& token
  84. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  85. -> decltype(
  86. async_initiate<CompletionToken, void (boost::system::error_code)>(
  87. declval<typename conditional_t<false, CompletionToken,
  88. Derived>::initiate_async_send>(), token,
  89. declval<typename conditional_t<false, CompletionToken,
  90. Derived>::payload_type>()))
  91. {
  92. typedef typename Derived::payload_type payload_type;
  93. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  94. Derived* self = static_cast<Derived*>(this);
  95. return async_initiate<CompletionToken, void (boost::system::error_code)>(
  96. typename Derived::initiate_async_send(self), token,
  97. payload_type(message_type(0, static_cast<Args&&>(args)...)));
  98. }
  99. };
  100. template <typename Derived, typename Executor,
  101. typename R, typename... Args, typename... Signatures>
  102. class channel_send_functions<Derived, Executor, R(Args...), Signatures...> :
  103. public channel_send_functions<Derived, Executor, Signatures...>
  104. {
  105. public:
  106. using channel_send_functions<Derived, Executor, Signatures...>::try_send;
  107. using channel_send_functions<Derived, Executor, Signatures...>::async_send;
  108. template <typename... Args2>
  109. enable_if_t<
  110. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  111. int, Args2...>::value,
  112. bool
  113. > try_send(Args2&&... args)
  114. {
  115. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  116. Derived* self = static_cast<Derived*>(this);
  117. return self->service_->template try_send<message_type>(
  118. self->impl_, false, static_cast<Args2&&>(args)...);
  119. }
  120. template <typename... Args2>
  121. enable_if_t<
  122. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  123. int, Args2...>::value,
  124. bool
  125. > try_send_via_dispatch(Args2&&... args)
  126. {
  127. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  128. Derived* self = static_cast<Derived*>(this);
  129. return self->service_->template try_send<message_type>(
  130. self->impl_, true, static_cast<Args2&&>(args)...);
  131. }
  132. template <typename... Args2>
  133. enable_if_t<
  134. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  135. int, Args2...>::value,
  136. std::size_t
  137. > try_send_n(std::size_t count, Args2&&... args)
  138. {
  139. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  140. Derived* self = static_cast<Derived*>(this);
  141. return self->service_->template try_send_n<message_type>(
  142. self->impl_, count, false, static_cast<Args2&&>(args)...);
  143. }
  144. template <typename... Args2>
  145. enable_if_t<
  146. is_constructible<boost::asio::detail::completion_message<R(Args...)>,
  147. int, Args2...>::value,
  148. std::size_t
  149. > try_send_n_via_dispatch(std::size_t count, Args2&&... args)
  150. {
  151. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  152. Derived* self = static_cast<Derived*>(this);
  153. return self->service_->template try_send_n<message_type>(
  154. self->impl_, count, true, static_cast<Args2&&>(args)...);
  155. }
  156. template <
  157. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  158. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  159. auto async_send(Args... args,
  160. CompletionToken&& token
  161. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  162. -> decltype(
  163. async_initiate<CompletionToken, void (boost::system::error_code)>(
  164. declval<typename conditional_t<false, CompletionToken,
  165. Derived>::initiate_async_send>(), token,
  166. declval<typename conditional_t<false, CompletionToken,
  167. Derived>::payload_type>()))
  168. {
  169. typedef typename Derived::payload_type payload_type;
  170. typedef boost::asio::detail::completion_message<R(Args...)> message_type;
  171. Derived* self = static_cast<Derived*>(this);
  172. return async_initiate<CompletionToken, void (boost::system::error_code)>(
  173. typename Derived::initiate_async_send(self), token,
  174. payload_type(message_type(0, static_cast<Args&&>(args)...)));
  175. }
  176. };
  177. } // namespace detail
  178. } // namespace experimental
  179. } // namespace asio
  180. } // namespace boost
  181. #include <boost/asio/detail/pop_options.hpp>
  182. #endif // BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_FUNCTIONS_HPP