Add .owner_hash_value to shared/weak_ptr, hash_value, std::hash/equal_to specializations for weak_ptr

This commit is contained in:
Peter Dimov committed 2020-06-04 20:40:57 +03:00
1 parent bc677e9098
commit 5dd84ea389
8 files changed
+308 -31

No files matched your search

+5
View File
@@ -398,3 +398,8 @@ run weak_ptr_mt_test.cpp
: : : <threading>multi ;
compile sp_report_implementation.cpp ;
run sp_owner_hash_value_test.cpp ;
run wp_hash_test.cpp ;
run wp_hash_test2.cpp ;
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
int main()
{
{
boost::shared_ptr<int> p1( new int );
boost::shared_ptr<int> p2( p1 );
BOOST_TEST_EQ( p1.owner_hash_value(), p2.owner_hash_value() );
boost::shared_ptr<int> p3( new int );
BOOST_TEST_NE( p1.owner_hash_value(), p3.owner_hash_value() );
boost::shared_ptr<int> p4;
boost::shared_ptr<int> p5;
BOOST_TEST_EQ( p4.owner_hash_value(), p5.owner_hash_value() );
BOOST_TEST_NE( p4.owner_hash_value(), p3.owner_hash_value() );
boost::shared_ptr<int> p6( static_cast<int*>(0) );
BOOST_TEST_NE( p4.owner_hash_value(), p6.owner_hash_value() );
boost::shared_ptr<void> p7( p1 );
BOOST_TEST_EQ( p1.owner_hash_value(), p7.owner_hash_value() );
boost::shared_ptr<void> p8;
BOOST_TEST_NE( p1.owner_hash_value(), p8.owner_hash_value() );
BOOST_TEST_EQ( p4.owner_hash_value(), p8.owner_hash_value() );
boost::weak_ptr<int> q1( p1 );
BOOST_TEST_EQ( p1.owner_hash_value(), q1.owner_hash_value() );
boost::weak_ptr<int> q2( p1 );
BOOST_TEST_EQ( q1.owner_hash_value(), q2.owner_hash_value() );
boost::weak_ptr<int> q3( p3 );
BOOST_TEST_NE( p1.owner_hash_value(), q3.owner_hash_value() );
BOOST_TEST_NE( q1.owner_hash_value(), q3.owner_hash_value() );
boost::weak_ptr<int> q4;
BOOST_TEST_EQ( p4.owner_hash_value(), q4.owner_hash_value() );
BOOST_TEST_NE( q1.owner_hash_value(), q4.owner_hash_value() );
boost::weak_ptr<void> q5;
BOOST_TEST_EQ( q4.owner_hash_value(), q5.owner_hash_value() );
boost::weak_ptr<void> q7( p7 );
BOOST_TEST_EQ( p1.owner_hash_value(), q7.owner_hash_value() );
BOOST_TEST_EQ( q1.owner_hash_value(), q7.owner_hash_value() );
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( q1.owner_hash_value(), q2.owner_hash_value() );
BOOST_TEST_EQ( q1.owner_hash_value(), q7.owner_hash_value() );
BOOST_TEST_NE( q1.owner_hash_value(), q3.owner_hash_value() );
BOOST_TEST_NE( q1.owner_hash_value(), q4.owner_hash_value() );
}
return boost::report_errors();
}
+76
View File
@@ -0,0 +1,76 @@
// Copyright 2011, 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/functional/hash.hpp>
#include <boost/core/lightweight_test.hpp>
int main()
{
{
boost::hash< boost::weak_ptr<int> > hasher;
boost::shared_ptr<int> p1, p2( p1 ), p3( new int ), p4( p3 ), p5( new int );
boost::weak_ptr<int> q1( p1 ), q2( p2 ), q3( p3 ), q4( p4 ), q5( p5 );
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
p3.reset();
p4.reset();
p5.reset();
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
}
{
boost::hash< boost::weak_ptr<int[]> > hasher;
boost::shared_ptr<int[]> p1, p2( p1 ), p3( new int[1] ), p4( p3 ), p5( new int[1] );
boost::weak_ptr<int[]> q1( p1 ), q2( p2 ), q3( p3 ), q4( p4 ), q5( p5 );
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
p3.reset();
p4.reset();
p5.reset();
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
}
{
boost::hash< boost::weak_ptr<int[1]> > hasher;
boost::shared_ptr<int[1]> p1, p2( p1 ), p3( new int[1] ), p4( p3 ), p5( new int[1] );
boost::weak_ptr<int[1]> q1( p1 ), q2( p2 ), q3( p3 ), q4( p4 ), q5( p5 );
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
p3.reset();
p4.reset();
p5.reset();
BOOST_TEST_EQ( hasher( q1 ), hasher( q2 ) );
BOOST_TEST_NE( hasher( q1 ), hasher( q3 ) );
BOOST_TEST_EQ( hasher( q3 ), hasher( q4 ) );
BOOST_TEST_NE( hasher( q3 ), hasher( q5 ) );
}
return boost::report_errors();
}
+61
View File
@@ -0,0 +1,61 @@
// Copyright 2011, 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <functional>
#if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
int main() {}
#else
int main()
{
{
boost::shared_ptr<int> p1, p2( new int );
boost::weak_ptr<int> q1( p1 ), q2( p2 ), q3;
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int> >()( q1 ), q1.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int> >()( q2 ), q2.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int> >()( q3 ), q3.owner_hash_value() );
p2.reset();
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int> >()( q2 ), q2.owner_hash_value() );
}
{
boost::shared_ptr<int[]> p1, p2( new int[1] );
boost::weak_ptr<int[]> q1( p1 ), q2( p2 ), q3;
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[]> >()( q1 ), q1.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[]> >()( q2 ), q2.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[]> >()( q3 ), q3.owner_hash_value() );
p2.reset();
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[]> >()( q2 ), q2.owner_hash_value() );
}
{
boost::shared_ptr<int[1]> p1, p2( new int[1] );
boost::weak_ptr<int[1]> q1( p1 ), q2( p2 ), q3;
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[1]> >()( q1 ), q1.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[1]> >()( q2 ), q2.owner_hash_value() );
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[1]> >()( q3 ), q3.owner_hash_value() );
p2.reset();
BOOST_TEST_EQ( std::hash< boost::weak_ptr<int[1]> >()( q2 ), q2.owner_hash_value() );
}
return boost::report_errors();
}
#endif // #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)