channel_send_functions.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // experimental/detail/channel_send_functions.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_FUNCTIONS_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/async_result.hpp"
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/error_code.hpp"
  19. #include "asio/experimental/detail/channel_message.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace experimental {
  23. namespace detail {
  24. template <typename Derived, typename Executor, typename... Signatures>
  25. class channel_send_functions;
  26. template <typename Derived, typename Executor, typename R, typename... Args>
  27. class channel_send_functions<Derived, Executor, R(Args...)>
  28. {
  29. public:
  30. template <typename... Args2>
  31. typename enable_if<
  32. is_constructible<detail::channel_message<R(Args...)>, int, Args2...>::value,
  33. bool
  34. >::type try_send(ASIO_MOVE_ARG(Args2)... args)
  35. {
  36. typedef typename detail::channel_message<R(Args...)> message_type;
  37. Derived* self = static_cast<Derived*>(this);
  38. return self->service_->template try_send<message_type>(
  39. self->impl_, ASIO_MOVE_CAST(Args2)(args)...);
  40. }
  41. template <typename... Args2>
  42. typename enable_if<
  43. is_constructible<detail::channel_message<R(Args...)>, int, Args2...>::value,
  44. std::size_t
  45. >::type try_send_n(std::size_t count, ASIO_MOVE_ARG(Args2)... args)
  46. {
  47. typedef typename detail::channel_message<R(Args...)> message_type;
  48. Derived* self = static_cast<Derived*>(this);
  49. return self->service_->template try_send_n<message_type>(
  50. self->impl_, count, ASIO_MOVE_CAST(Args2)(args)...);
  51. }
  52. template <
  53. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  54. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  55. auto async_send(Args... args,
  56. ASIO_MOVE_ARG(CompletionToken) token
  57. ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  58. {
  59. typedef typename Derived::payload_type payload_type;
  60. typedef typename detail::channel_message<R(Args...)> message_type;
  61. Derived* self = static_cast<Derived*>(this);
  62. return async_initiate<CompletionToken, void (asio::error_code)>(
  63. typename Derived::initiate_async_send(self), token,
  64. payload_type(message_type(0, ASIO_MOVE_CAST(Args)(args)...)));
  65. }
  66. };
  67. template <typename Derived, typename Executor,
  68. typename R, typename... Args, typename... Signatures>
  69. class channel_send_functions<Derived, Executor, R(Args...), Signatures...> :
  70. public channel_send_functions<Derived, Executor, Signatures...>
  71. {
  72. public:
  73. using channel_send_functions<Derived, Executor, Signatures...>::try_send;
  74. using channel_send_functions<Derived, Executor, Signatures...>::async_send;
  75. template <typename... Args2>
  76. typename enable_if<
  77. is_constructible<detail::channel_message<R(Args...)>, int, Args2...>::value,
  78. bool
  79. >::type try_send(ASIO_MOVE_ARG(Args2)... args)
  80. {
  81. typedef typename detail::channel_message<R(Args...)> message_type;
  82. Derived* self = static_cast<Derived*>(this);
  83. return self->service_->template try_send<message_type>(
  84. self->impl_, ASIO_MOVE_CAST(Args2)(args)...);
  85. }
  86. template <typename... Args2>
  87. typename enable_if<
  88. is_constructible<detail::channel_message<R(Args...)>, int, Args2...>::value,
  89. std::size_t
  90. >::type try_send_n(std::size_t count, ASIO_MOVE_ARG(Args2)... args)
  91. {
  92. typedef typename detail::channel_message<R(Args...)> message_type;
  93. Derived* self = static_cast<Derived*>(this);
  94. return self->service_->template try_send_n<message_type>(
  95. self->impl_, count, ASIO_MOVE_CAST(Args2)(args)...);
  96. }
  97. template <
  98. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  99. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  100. auto async_send(Args... args,
  101. ASIO_MOVE_ARG(CompletionToken) token
  102. ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  103. {
  104. typedef typename Derived::payload_type payload_type;
  105. typedef typename detail::channel_message<R(Args...)> message_type;
  106. Derived* self = static_cast<Derived*>(this);
  107. return async_initiate<CompletionToken, void (asio::error_code)>(
  108. typename Derived::initiate_async_send(self), token,
  109. payload_type(message_type(0, ASIO_MOVE_CAST(Args)(args)...)));
  110. }
  111. };
  112. } // namespace detail
  113. } // namespace experimental
  114. } // namespace asio
  115. #include "asio/detail/pop_options.hpp"
  116. #endif // ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_FUNCTIONS_HPP