immediate_executor.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2023 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  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. #ifndef BOOST_BEAST_TEST_IMMEDIATE_EXECUTOR_HPP
  8. #define BOOST_BEAST_TEST_IMMEDIATE_EXECUTOR_HPP
  9. #include <boost/asio/any_io_executor.hpp>
  10. #include <boost/asio/execution_context.hpp>
  11. namespace boost
  12. {
  13. namespace beast
  14. {
  15. namespace test
  16. {
  17. /** A immediate executor that directly invokes and counts how often that happened. */
  18. class immediate_executor
  19. {
  20. asio::execution_context* context_ = nullptr;
  21. std::size_t &count_;
  22. public:
  23. immediate_executor(std::size_t & count) noexcept : count_(count) {}
  24. asio::execution_context &query(asio::execution::context_t) const noexcept
  25. {
  26. BOOST_ASSERT(false);
  27. return *context_;
  28. }
  29. constexpr static asio::execution::blocking_t
  30. query(asio::execution::blocking_t) noexcept
  31. {
  32. return asio::execution::blocking_t::never_t{};
  33. }
  34. constexpr static asio::execution::relationship_t
  35. query(asio::execution::relationship_t) noexcept
  36. {
  37. return asio::execution::relationship_t::fork_t{};
  38. }
  39. // this function takes the function F and runs it on the event loop.
  40. template<class F>
  41. void
  42. execute(F f) const
  43. {
  44. count_++;
  45. std::forward<F>(f)();
  46. }
  47. bool
  48. operator==(immediate_executor const &) const noexcept
  49. {
  50. return true;
  51. }
  52. bool
  53. operator!=(immediate_executor const &) const noexcept
  54. {
  55. return false;
  56. }
  57. };
  58. } // test
  59. } // beast
  60. #if ! BOOST_BEAST_DOXYGEN
  61. namespace asio
  62. {
  63. namespace traits
  64. {
  65. template<typename F>
  66. struct execute_member<beast::test::immediate_executor, F>
  67. {
  68. static constexpr bool is_valid = true;
  69. static constexpr bool is_noexcept = false;
  70. typedef void result_type;
  71. };
  72. template<>
  73. struct equality_comparable<beast::test::immediate_executor>
  74. {
  75. static constexpr bool is_valid = true;
  76. static constexpr bool is_noexcept = true;
  77. };
  78. template<>
  79. struct query_member<beast::test::immediate_executor, execution::context_t>
  80. {
  81. static constexpr bool is_valid = true;
  82. static constexpr bool is_noexcept = true;
  83. typedef execution_context& result_type;
  84. };
  85. template<typename Property>
  86. struct query_static_constexpr_member<
  87. beast::test::immediate_executor,
  88. Property,
  89. typename enable_if<std::is_convertible<Property, execution::blocking_t>::value>::type>
  90. {
  91. static constexpr bool is_valid = true;
  92. static constexpr bool is_noexcept = true;
  93. typedef execution::blocking_t::never_t result_type;
  94. static constexpr result_type value() noexcept
  95. {
  96. return result_type();
  97. }
  98. };
  99. } // traits
  100. } // asio
  101. #endif
  102. } // boost
  103. #endif //BOOST_BEAST_TEST_IMMEDIATE_EXECUTOR_HPP