From cfd18095199e546d6fd647bc53ade1b0dcdb6da6 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Wed, 7 Jun 2023 11:20:15 -0700 Subject: [PATCH] Add support for std::coroutine_handle --- include/boost/container_hash/hash.hpp | 16 +++++++ test/Jamfile.v2 | 2 + test/hash_coroutine_handle.cpp | 65 +++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 test/hash_coroutine_handle.cpp diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index 605644b3..4013010b 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -69,6 +69,10 @@ # include #endif +#if !defined(BOOST_NO_CXX20_HDR_COROUTINE) +# include +#endif + namespace boost { @@ -551,6 +555,18 @@ namespace boost return seed; } +#endif + + // std::coroutine_handle + +#if !defined(BOOST_NO_CXX20_HDR_COROUTINE) + + template + std::size_t hash_value( std::coroutine_handle h ) + { + return hash_value( h.address() ); + } + #endif // diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 63d9ed35..11be32f0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -125,3 +125,5 @@ run is_tuple_like_test.cpp ; run hash_tuple_like_test.cpp ; run hash_tuple_like_test2.cpp : : : extra ; + +run hash_coroutine_handle.cpp ; diff --git a/test/hash_coroutine_handle.cpp b/test/hash_coroutine_handle.cpp new file mode 100644 index 00000000..23c696ce --- /dev/null +++ b/test/hash_coroutine_handle.cpp @@ -0,0 +1,65 @@ +// Copyright 2023 Christian Mazakas. +// 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) + +#include + +#if defined(BOOST_NO_CXX20_HDR_COROUTINE) + +#include + +BOOST_PRAGMA_MESSAGE( + "hash_coroutine_handle test requires C++20 and support") +int main() {} +#else + +#include +#include + +#include + +struct promise; + +struct task : std::coroutine_handle { + using promise_type = ::promise; +}; + +struct promise { + task get_return_object() { return {task::from_promise(*this)}; } + + void return_void() {} + void unhandled_exception() {} + std::suspend_always initial_suspend() { return {}; } + std::suspend_always final_suspend() noexcept { return {}; } +}; + +task make_task() { co_return; } + +namespace { + +void coroutine_handle_tests() { + task t = make_task(); + void *p = t.address(); + + { + auto h = boost::hash>()(t); + BOOST_TEST_EQ(h, boost::hash()(p)); + } + + std::coroutine_handle<> t2 = t; + { + auto h = boost::hash>()(t2); + BOOST_TEST_EQ(h, boost::hash()(p)); + } + + t.destroy(); +} + +} // namespace + +int main() { + coroutine_handle_tests(); + return boost::report_errors(); +} + +#endif