channel_message.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // experimental/detail/channel_message.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_MESSAGE_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_MESSAGE_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 <tuple>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/detail/utility.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace experimental {
  23. namespace detail {
  24. template <typename Signature>
  25. class channel_message;
  26. template <typename R>
  27. class channel_message<R()>
  28. {
  29. public:
  30. channel_message(int)
  31. {
  32. }
  33. template <typename Handler>
  34. void receive(Handler& handler)
  35. {
  36. static_cast<Handler&&>(handler)();
  37. }
  38. };
  39. template <typename R, typename Arg0>
  40. class channel_message<R(Arg0)>
  41. {
  42. public:
  43. template <typename T0>
  44. channel_message(int, T0&& t0)
  45. : arg0_(static_cast<T0&&>(t0))
  46. {
  47. }
  48. template <typename Handler>
  49. void receive(Handler& handler)
  50. {
  51. static_cast<Handler&&>(handler)(
  52. static_cast<arg0_type&&>(arg0_));
  53. }
  54. private:
  55. typedef decay_t<Arg0> arg0_type;
  56. arg0_type arg0_;
  57. };
  58. template <typename R, typename Arg0, typename Arg1>
  59. class channel_message<R(Arg0, Arg1)>
  60. {
  61. public:
  62. template <typename T0, typename T1>
  63. channel_message(int, T0&& t0, T1&& t1)
  64. : arg0_(static_cast<T0&&>(t0)),
  65. arg1_(static_cast<T1&&>(t1))
  66. {
  67. }
  68. template <typename Handler>
  69. void receive(Handler& handler)
  70. {
  71. static_cast<Handler&&>(handler)(
  72. static_cast<arg0_type&&>(arg0_),
  73. static_cast<arg1_type&&>(arg1_));
  74. }
  75. private:
  76. typedef decay_t<Arg0> arg0_type;
  77. arg0_type arg0_;
  78. typedef decay_t<Arg1> arg1_type;
  79. arg1_type arg1_;
  80. };
  81. template <typename R, typename... Args>
  82. class channel_message<R(Args...)>
  83. {
  84. public:
  85. template <typename... T>
  86. channel_message(int, T&&... t)
  87. : args_(static_cast<T&&>(t)...)
  88. {
  89. }
  90. template <typename Handler>
  91. void receive(Handler& h)
  92. {
  93. this->do_receive(h, boost::asio::detail::index_sequence_for<Args...>());
  94. }
  95. private:
  96. template <typename Handler, std::size_t... I>
  97. void do_receive(Handler& h, boost::asio::detail::index_sequence<I...>)
  98. {
  99. static_cast<Handler&&>(h)(
  100. std::get<I>(static_cast<args_type&&>(args_))...);
  101. }
  102. typedef std::tuple<decay_t<Args>...> args_type;
  103. args_type args_;
  104. };
  105. } // namespace detail
  106. } // namespace experimental
  107. } // namespace asio
  108. } // namespace boost
  109. #include <boost/asio/detail/pop_options.hpp>
  110. #endif // BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_MESSAGE_HPP