diff --git a/include/boost/smart_ptr/owner_hash.hpp b/include/boost/smart_ptr/owner_hash.hpp new file mode 100644 index 0000000..4378d91 --- /dev/null +++ b/include/boost/smart_ptr/owner_hash.hpp @@ -0,0 +1,27 @@ +#ifndef BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED +#define BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED + +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +namespace boost +{ + +template struct owner_hash +{ + typedef std::size_t result_type; + typedef T argument_type; + + std::size_t operator()( T const & t ) const BOOST_NOEXCEPT + { + return t.owner_hash_value(); + } +}; + +} // namespace boost + +#endif // #ifndef BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED diff --git a/test/Jamfile b/test/Jamfile index f8edba8..f265e40 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -405,3 +405,5 @@ run wp_hash_test.cpp ; run wp_hash_test2.cpp ; run wp_unordered_test.cpp ; + +run owner_hash_test.cpp ; diff --git a/test/owner_hash_test.cpp b/test/owner_hash_test.cpp new file mode 100644 index 0000000..15da1b6 --- /dev/null +++ b/test/owner_hash_test.cpp @@ -0,0 +1,88 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +template std::size_t hash( T const& t ) +{ + return boost::owner_hash()( t ); +} + +int main() +{ + boost::shared_ptr p1( new int ); + boost::shared_ptr p2( p1 ); + + BOOST_TEST_EQ( hash( p1 ), hash( p2 ) ); + + boost::shared_ptr p3( new int ); + + BOOST_TEST_NE( hash( p1 ), hash( p3 ) ); + + boost::shared_ptr p4; + boost::shared_ptr p5; + + BOOST_TEST_EQ( hash( p4 ), hash( p5 ) ); + BOOST_TEST_NE( hash( p4 ), hash( p3 ) ); + + boost::shared_ptr p6( static_cast(0) ); + + BOOST_TEST_NE( hash( p4 ), hash( p6 ) ); + + boost::shared_ptr p7( p1 ); + + BOOST_TEST_EQ( hash( p1 ), hash( p7 ) ); + + boost::shared_ptr p8; + + BOOST_TEST_NE( hash( p1 ), hash( p8 ) ); + BOOST_TEST_EQ( hash( p4 ), hash( p8 ) ); + + boost::weak_ptr q1( p1 ); + + BOOST_TEST_EQ( hash( p1 ), hash( q1 ) ); + + boost::weak_ptr q2( p1 ); + + BOOST_TEST_EQ( hash( q1 ), hash( q2 ) ); + + boost::weak_ptr q3( p3 ); + + BOOST_TEST_NE( hash( p1 ), hash( q3 ) ); + BOOST_TEST_NE( hash( q1 ), hash( q3 ) ); + + boost::weak_ptr q4; + + BOOST_TEST_EQ( hash( p4 ), hash( q4 ) ); + BOOST_TEST_NE( hash( q1 ), hash( q4 ) ); + + boost::weak_ptr q5; + + BOOST_TEST_EQ( hash( q4 ), hash( q5 ) ); + + boost::weak_ptr q7( p7 ); + + BOOST_TEST_EQ( hash( p1 ), hash( q7 ) ); + BOOST_TEST_EQ( hash( q1 ), hash( q7 ) ); + + p1.reset(); + p2.reset(); + p3.reset(); + p7.reset(); + + BOOST_TEST( q1.expired() ); + BOOST_TEST( q2.expired() ); + BOOST_TEST( q3.expired() ); + BOOST_TEST( q7.expired() ); + + BOOST_TEST_EQ( hash( q1 ), hash( q2 ) ); + BOOST_TEST_EQ( hash( q1 ), hash( q7 ) ); + BOOST_TEST_NE( hash( q1 ), hash( q3 ) ); + BOOST_TEST_NE( hash( q1 ), hash( q4 ) ); + + return boost::report_errors(); +}