channel_handler.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // experimental/detail/channel_handler.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_HANDLER_HPP
  11. #define ASIO_EXPERIMENTAL_DETAIL_CHANNEL_HANDLER_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/associator.hpp"
  17. #include "asio/experimental/detail/channel_payload.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace experimental {
  21. namespace detail {
  22. template <typename Payload, typename Handler>
  23. class channel_handler
  24. {
  25. public:
  26. channel_handler(Payload&& p, Handler& h)
  27. : payload_(static_cast<Payload&&>(p)),
  28. handler_(static_cast<Handler&&>(h))
  29. {
  30. }
  31. void operator()()
  32. {
  33. payload_.receive(handler_);
  34. }
  35. //private:
  36. Payload payload_;
  37. Handler handler_;
  38. };
  39. } // namespace detail
  40. } // namespace experimental
  41. template <template <typename, typename> class Associator,
  42. typename Payload, typename Handler, typename DefaultCandidate>
  43. struct associator<Associator,
  44. experimental::detail::channel_handler<Payload, Handler>,
  45. DefaultCandidate>
  46. : Associator<Handler, DefaultCandidate>
  47. {
  48. static typename Associator<Handler, DefaultCandidate>::type get(
  49. const experimental::detail::channel_handler<Payload, Handler>& h) noexcept
  50. {
  51. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  52. }
  53. static auto get(
  54. const experimental::detail::channel_handler<Payload, Handler>& h,
  55. const DefaultCandidate& c) noexcept
  56. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  57. {
  58. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  59. }
  60. };
  61. } // namespace asio
  62. #include "asio/detail/pop_options.hpp"
  63. #endif // ASIO_EXPERIMENTAL_DETAIL_CHANNEL_HANDLER_HPP