// // execute.cpp // ~~~~~~~~~~~ // // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // Disable autolinking for unit tests. #if !defined(BOOST_ALL_NO_LIB) #define BOOST_ALL_NO_LIB 1 #endif // !defined(BOOST_ALL_NO_LIB) // Test that header file is self-contained. #include #include #include #include #include #include "../unit_test.hpp" #if !defined(BOOST_ASIO_NO_DEPRECATED) namespace exec = boost::asio::execution; struct no_execute { }; struct const_member_execute { template void execute(F&& f) const { typename boost::asio::decay::type tmp(static_cast(f)); tmp(); } }; #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) namespace boost { namespace asio { namespace traits { template struct execute_member { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef void result_type; }; } // namespace traits } // namespace asio } // namespace boost #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) struct free_execute_const_executor { template friend void execute(const free_execute_const_executor&, F&& f) { typename boost::asio::decay::type tmp(static_cast(f)); tmp(); } }; #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_FREE_TRAIT) namespace boost { namespace asio { namespace traits { template struct execute_free { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef void result_type; }; } // namespace traits } // namespace asio } // namespace boost #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_FREE_TRAIT) // Support for rvalue references is required in order to use the execute // customisation point with non-const member functions and free functions // taking non-const arguments. struct non_const_member_execute { template void execute(F&& f) { typename boost::asio::decay::type tmp(static_cast(f)); tmp(); } }; #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) namespace boost { namespace asio { namespace traits { template struct execute_member { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef void result_type; }; template struct execute_member { static constexpr bool is_valid = false; static constexpr bool is_noexcept = false; typedef void result_type; }; template struct execute_member { static constexpr bool is_valid = false; static constexpr bool is_noexcept = false; typedef void result_type; }; } // namespace traits } // namespace asio } // namespace boost #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) struct free_execute_non_const_executor { template friend void execute(free_execute_non_const_executor&, F&& f) { typename boost::asio::decay::type tmp(static_cast(f)); tmp(); } }; #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_FREE_TRAIT) namespace boost { namespace asio { namespace traits { template struct execute_free { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef void result_type; }; template struct execute_free { static constexpr bool is_valid = false; static constexpr bool is_noexcept = false; typedef void result_type; }; template struct execute_free { static constexpr bool is_valid = false; static constexpr bool is_noexcept = false; typedef void result_type; }; } // namespace traits } // namespace asio } // namespace boost #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_FREE_TRAIT) struct operation_state { void start() noexcept { } }; namespace boost { namespace asio { namespace traits { #if !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT) template <> struct start_member { static constexpr bool is_valid = true; static constexpr bool is_noexcept = true; typedef void result_type; }; #endif // !defined(BOOST_ASIO_HAS_DEDUCED_START_MEMBER_TRAIT) } // namespace traits } // namespace asio } // namespace boost struct sender : exec::sender_base { sender() { } template operation_state connect(R&& r) const { (void)r; return operation_state(); } template void submit(R&& r) const { exec::set_value(static_cast(r)); } }; namespace boost { namespace asio { namespace traits { #if !defined(BOOST_ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT) template struct connect_member { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef operation_state result_type; }; #endif // !defined(BOOST_ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT) #if !defined(BOOST_ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT) template struct submit_member { static constexpr bool is_valid = true; static constexpr bool is_noexcept = false; typedef void result_type; }; #endif // !defined(BOOST_ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT) } // namespace traits } // namespace asio } // namespace boost void test_can_execute() { constexpr bool b1 = exec::can_execute< no_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b1 == false); constexpr bool b2 = exec::can_execute< const no_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b2 == false); constexpr bool b3 = exec::can_execute< const_member_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b3 == true); constexpr bool b4 = exec::can_execute< const const_member_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b4 == true); constexpr bool b5 = exec::can_execute< free_execute_const_executor&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b5 == true); constexpr bool b6 = exec::can_execute< const free_execute_const_executor&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b6 == true); constexpr bool b7 = exec::can_execute< non_const_member_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b7 == true); constexpr bool b8 = exec::can_execute< const non_const_member_execute&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b8 == false); constexpr bool b9 = exec::can_execute< free_execute_non_const_executor&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b9 == true); constexpr bool b10 = exec::can_execute< const free_execute_non_const_executor&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b10 == false); constexpr bool b11 = exec::can_execute< sender&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b11 == true); constexpr bool b12 = exec::can_execute< const sender&, exec::invocable_archetype>::value; BOOST_ASIO_CHECK(b12 == true); } void increment(int* count) { ++(*count); } void test_execute() { namespace bindns = std; int count = 0; const_member_execute ex1 = {}; exec::execute(ex1, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; const const_member_execute ex2 = {}; exec::execute(ex2, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; exec::execute(const_member_execute(), bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; free_execute_const_executor ex3 = {}; exec::execute(ex3, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; const free_execute_const_executor ex4 = {}; exec::execute(ex4, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; exec::execute(free_execute_const_executor(), bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; non_const_member_execute ex5 = {}; exec::execute(ex5, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; free_execute_non_const_executor ex6 = {}; exec::execute(ex6, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; sender ex7; exec::execute(ex3, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); count = 0; const sender ex8; exec::execute(ex4, bindns::bind(&increment, &count)); BOOST_ASIO_CHECK(count == 1); } BOOST_ASIO_TEST_SUITE ( "execute", BOOST_ASIO_TEST_CASE(test_can_execute) BOOST_ASIO_TEST_CASE(test_execute) ) #else // !defined(BOOST_ASIO_NO_DEPRECATED) BOOST_ASIO_TEST_SUITE ( "execute", BOOST_ASIO_TEST_CASE(null_test) ) #endif // !defined(BOOST_ASIO_NO_DEPRECATED)