Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for std::coroutine_handle #32

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/boost/container_hash/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
# include <string_view>
#endif

#if !defined(BOOST_NO_CXX20_HDR_COROUTINE)
# include <coroutine>
#endif

namespace boost
{

Expand Down Expand Up @@ -551,6 +555,18 @@ namespace boost
return seed;
}

#endif

// std::coroutine_handle

#if !defined(BOOST_NO_CXX20_HDR_COROUTINE)

template <typename T>
std::size_t hash_value( std::coroutine_handle<T> h )
{
return hash_value( h.address() );
}

#endif

//
Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,5 @@ run is_tuple_like_test.cpp ;
run hash_tuple_like_test.cpp ;
run hash_tuple_like_test2.cpp
: : : <warnings>extra ;

run hash_coroutine_handle.cpp ;
65 changes: 65 additions & 0 deletions test/hash_coroutine_handle.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/config.hpp>

#if defined(BOOST_NO_CXX20_HDR_COROUTINE)

#include <boost/config/pragma_message.hpp>

BOOST_PRAGMA_MESSAGE(
"hash_coroutine_handle test requires C++20 and <coroutine> support")
int main() {}
#else

#include <boost/container_hash/hash.hpp>
#include <boost/core/lightweight_test.hpp>

#include <coroutine>

struct promise;

struct task : std::coroutine_handle<promise> {
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<std::coroutine_handle<promise>>()(t);
BOOST_TEST_EQ(h, boost::hash<void *>()(p));
}

std::coroutine_handle<> t2 = t;
{
auto h = boost::hash<std::coroutine_handle<>>()(t2);
BOOST_TEST_EQ(h, boost::hash<void *>()(p));
}

t.destroy();
}

} // namespace

int main() {
coroutine_handle_tests();
return boost::report_errors();
}

#endif