mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-30 12:47:28 +02:00
Add owner_hash
This commit is contained in:
27
include/boost/smart_ptr/owner_hash.hpp
Normal file
27
include/boost/smart_ptr/owner_hash.hpp
Normal file
@ -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 <boost/config.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
template<class T> 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
|
@ -405,3 +405,5 @@ run wp_hash_test.cpp ;
|
||||
run wp_hash_test2.cpp ;
|
||||
|
||||
run wp_unordered_test.cpp ;
|
||||
|
||||
run owner_hash_test.cpp ;
|
||||
|
88
test/owner_hash_test.cpp
Normal file
88
test/owner_hash_test.cpp
Normal file
@ -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 <boost/smart_ptr/owner_hash.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
template<class T> std::size_t hash( T const& t )
|
||||
{
|
||||
return boost::owner_hash<T>()( t );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::shared_ptr<int> p1( new int );
|
||||
boost::shared_ptr<int> p2( p1 );
|
||||
|
||||
BOOST_TEST_EQ( hash( p1 ), hash( p2 ) );
|
||||
|
||||
boost::shared_ptr<int> p3( new int );
|
||||
|
||||
BOOST_TEST_NE( hash( p1 ), hash( p3 ) );
|
||||
|
||||
boost::shared_ptr<int> p4;
|
||||
boost::shared_ptr<int> p5;
|
||||
|
||||
BOOST_TEST_EQ( hash( p4 ), hash( p5 ) );
|
||||
BOOST_TEST_NE( hash( p4 ), hash( p3 ) );
|
||||
|
||||
boost::shared_ptr<int> p6( static_cast<int*>(0) );
|
||||
|
||||
BOOST_TEST_NE( hash( p4 ), hash( p6 ) );
|
||||
|
||||
boost::shared_ptr<void> p7( p1 );
|
||||
|
||||
BOOST_TEST_EQ( hash( p1 ), hash( p7 ) );
|
||||
|
||||
boost::shared_ptr<void> p8;
|
||||
|
||||
BOOST_TEST_NE( hash( p1 ), hash( p8 ) );
|
||||
BOOST_TEST_EQ( hash( p4 ), hash( p8 ) );
|
||||
|
||||
boost::weak_ptr<int> q1( p1 );
|
||||
|
||||
BOOST_TEST_EQ( hash( p1 ), hash( q1 ) );
|
||||
|
||||
boost::weak_ptr<int> q2( p1 );
|
||||
|
||||
BOOST_TEST_EQ( hash( q1 ), hash( q2 ) );
|
||||
|
||||
boost::weak_ptr<int> q3( p3 );
|
||||
|
||||
BOOST_TEST_NE( hash( p1 ), hash( q3 ) );
|
||||
BOOST_TEST_NE( hash( q1 ), hash( q3 ) );
|
||||
|
||||
boost::weak_ptr<int> q4;
|
||||
|
||||
BOOST_TEST_EQ( hash( p4 ), hash( q4 ) );
|
||||
BOOST_TEST_NE( hash( q1 ), hash( q4 ) );
|
||||
|
||||
boost::weak_ptr<void> q5;
|
||||
|
||||
BOOST_TEST_EQ( hash( q4 ), hash( q5 ) );
|
||||
|
||||
boost::weak_ptr<void> 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();
|
||||
}
|
Reference in New Issue
Block a user