channel_message.hpp 2.6 KB

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