immediate_executor.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. namespace boost
  11. {
  12. namespace beast
  13. {
  14. namespace test
  15. {
  16. /** A immediate executor that directly invokes and counts how often that happened. */
  17. class immediate_executor
  18. {
  19. std::size_t &count_;
  20. public:
  21. immediate_executor(std::size_t & count) noexcept : count_(count) {}
  22. asio::execution_context &query(asio::execution::context_t) const
  23. {
  24. BOOST_ASSERT(false);
  25. return *static_cast<asio::execution_context*>(nullptr);
  26. }
  27. constexpr static asio::execution::blocking_t
  28. query(asio::execution::blocking_t) noexcept
  29. {
  30. return asio::execution::blocking_t::never_t{};
  31. }
  32. constexpr static asio::execution::relationship_t
  33. query(asio::execution::relationship_t) noexcept
  34. {
  35. return asio::execution::relationship_t::fork_t{};
  36. }
  37. // this function takes the function F and runs it on the event loop.
  38. template<class F>
  39. void
  40. execute(F f) const
  41. {
  42. count_++;
  43. std::forward<F>(f)();
  44. }
  45. bool
  46. operator==(immediate_executor const &other) const noexcept
  47. {
  48. return true;
  49. }
  50. bool
  51. operator!=(immediate_executor const &other) const noexcept
  52. {
  53. return false;
  54. }
  55. };
  56. }
  57. }
  58. }
  59. #endif //BOOST_BEAST_TEST_IMMEDIATE_EXECUTOR_HPP