channel_message.hpp 2.7 KB

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