remap_post_to_defer.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_REMAP_POST_TO_DEFER_HPP
  10. #define BOOST_BEAST_DETAIL_REMAP_POST_TO_DEFER_HPP
  11. #include <boost/asio/is_executor.hpp>
  12. #include <boost/core/empty_value.hpp>
  13. #include <type_traits>
  14. #include <utility>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. template<class Executor>
  19. class remap_post_to_defer
  20. : private boost::empty_value<Executor>
  21. {
  22. BOOST_STATIC_ASSERT(
  23. net::is_executor<Executor>::value);
  24. Executor const&
  25. ex() const noexcept
  26. {
  27. return this->get();
  28. }
  29. public:
  30. remap_post_to_defer(
  31. remap_post_to_defer&&) = default;
  32. remap_post_to_defer(
  33. remap_post_to_defer const&) = default;
  34. explicit
  35. remap_post_to_defer(
  36. Executor const& ex)
  37. : boost::empty_value<Executor>(
  38. boost::empty_init_t{}, ex)
  39. {
  40. }
  41. bool
  42. operator==(
  43. remap_post_to_defer const& other) const noexcept
  44. {
  45. return ex() == other.ex();
  46. }
  47. bool
  48. operator!=(
  49. remap_post_to_defer const& other) const noexcept
  50. {
  51. return ex() != other.ex();
  52. }
  53. decltype(std::declval<Executor const&>().context())
  54. context() const noexcept
  55. {
  56. return ex().context();
  57. }
  58. void
  59. on_work_started() const noexcept
  60. {
  61. ex().on_work_started();
  62. }
  63. void
  64. on_work_finished() const noexcept
  65. {
  66. ex().on_work_finished();
  67. }
  68. template<class F, class A>
  69. void
  70. dispatch(F&& f, A const& a) const
  71. {
  72. ex().dispatch(std::forward<F>(f), a);
  73. }
  74. template<class F, class A>
  75. void
  76. post(F&& f, A const& a) const
  77. {
  78. ex().defer(std::forward<F>(f), a);
  79. }
  80. template<class F, class A>
  81. void
  82. defer(F&& f, A const& a) const
  83. {
  84. ex().defer(std::forward<F>(f), a);
  85. }
  86. };
  87. } // detail
  88. } // beast
  89. } // boost
  90. #endif