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