mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-29 20:37:13 +02:00
Add .owner_hash_value to shared/weak_ptr, hash_value, std::hash/equal_to specializations for weak_ptr
This commit is contained in:
@ -22,28 +22,25 @@
|
||||
# pragma warn -8027 // Functions containing try are not expanded inline
|
||||
#endif
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/smart_ptr/bad_weak_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_counted_base.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_counted_impl.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/core/addressof.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/workaround.hpp>
|
||||
// In order to avoid circular dependencies with Boost.TR1
|
||||
// we make sure that our include of <memory> doesn't try to
|
||||
// pull in the TR1 headers: that's why we use this header
|
||||
// rather than including <memory> directly:
|
||||
#include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
|
||||
#include <functional> // std::less
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <memory> // std::auto_ptr
|
||||
#include <functional> // std::less
|
||||
#include <cstddef> // std::size_t
|
||||
|
||||
#ifdef BOOST_NO_EXCEPTIONS
|
||||
# include <new> // std::bad_alloc
|
||||
#endif
|
||||
|
||||
#include <boost/core/addressof.hpp>
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
@ -103,6 +100,14 @@ template< class D > struct sp_convert_reference< D& >
|
||||
typedef sp_reference_wrapper< D > type;
|
||||
};
|
||||
|
||||
template<class T> std::size_t sp_hash_pointer( T* p ) BOOST_NOEXCEPT
|
||||
{
|
||||
boost::uintptr_t v = reinterpret_cast<boost::uintptr_t>( p );
|
||||
|
||||
// match boost::hash<T*>
|
||||
return static_cast<std::size_t>( v + ( v >> 3 ) );
|
||||
}
|
||||
|
||||
class weak_count;
|
||||
|
||||
class shared_count
|
||||
@ -517,6 +522,11 @@ public:
|
||||
{
|
||||
return pi_? pi_->get_untyped_deleter(): 0;
|
||||
}
|
||||
|
||||
std::size_t hash_value() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return sp_hash_pointer( pi_ );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -643,6 +653,11 @@ public:
|
||||
{
|
||||
return std::less<sp_counted_base *>()( pi_, r.pi_ );
|
||||
}
|
||||
|
||||
std::size_t hash_value() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return sp_hash_pointer( pi_ );
|
||||
}
|
||||
};
|
||||
|
||||
inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
|
||||
|
@ -18,25 +18,21 @@
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||
# error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
|
||||
#endif
|
||||
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_counted_base.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/core/addressof.hpp>
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||
#include <boost/smart_ptr/detail/quick_allocator.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_SP_USE_STD_ALLOCATOR)
|
||||
#include <memory> // std::allocator
|
||||
#endif
|
||||
|
||||
#include <memory> // std::allocator, std::allocator_traits
|
||||
#include <cstddef> // std::size_t
|
||||
|
||||
namespace boost
|
||||
|
@ -14,23 +14,16 @@
|
||||
// See http://www.boost.org/libs/smart_ptr/ for documentation.
|
||||
//
|
||||
|
||||
#include <boost/config.hpp> // for broken compiler workarounds
|
||||
|
||||
// In order to avoid circular dependencies with Boost.TR1
|
||||
// we make sure that our include of <memory> doesn't try to
|
||||
// pull in the TR1 headers: that's why we use this header
|
||||
// rather than including <memory> directly:
|
||||
#include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/smart_ptr/detail/shared_count.hpp>
|
||||
#include <boost/config/workaround.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_convertible.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/workaround.hpp>
|
||||
|
||||
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
|
||||
#include <boost/smart_ptr/detail/spinlock_pool.hpp>
|
||||
@ -40,6 +33,7 @@
|
||||
#include <functional> // for std::less
|
||||
#include <typeinfo> // for std::bad_cast
|
||||
#include <cstddef> // for std::size_t
|
||||
#include <memory> // for std::auto_ptr
|
||||
|
||||
#if !defined(BOOST_NO_IOSTREAM)
|
||||
#if !defined(BOOST_NO_IOSFWD)
|
||||
@ -787,6 +781,11 @@ public:
|
||||
return pn == rhs.pn;
|
||||
}
|
||||
|
||||
std::size_t owner_hash_value() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return pn.hash_value();
|
||||
}
|
||||
|
||||
void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return pn.get_deleter( ti );
|
||||
|
@ -13,10 +13,11 @@
|
||||
// See http://www.boost.org/libs/smart_ptr/ for documentation.
|
||||
//
|
||||
|
||||
#include <memory> // boost.TR1 include order fix
|
||||
#include <boost/smart_ptr/detail/shared_count.hpp>
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
|
||||
#include <memory>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
@ -247,6 +248,11 @@ public:
|
||||
return pn == rhs.pn;
|
||||
}
|
||||
|
||||
std::size_t owner_hash_value() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return pn.hash_value();
|
||||
}
|
||||
|
||||
// Tasteless as this may seem, making all members public allows member templates
|
||||
// to work in the absence of member template friends. (Matthew Langston)
|
||||
|
||||
@ -280,6 +286,40 @@ template<class T> weak_ptr( shared_ptr<T> ) -> weak_ptr<T>;
|
||||
|
||||
#endif
|
||||
|
||||
// hash_value
|
||||
|
||||
template< class T > std::size_t hash_value( boost::weak_ptr<T> const & p ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return p.owner_hash_value();
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
// std::hash, std::equal_to
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
template<class T> struct hash< ::boost::weak_ptr<T> >
|
||||
{
|
||||
std::size_t operator()( ::boost::weak_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return p.owner_hash_value();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
template<class T> struct equal_to< ::boost::weak_ptr<T> >
|
||||
{
|
||||
bool operator()( ::boost::weak_ptr<T> const & a, ::boost::weak_ptr<T> const & b ) const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.owner_equals( b );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|
||||
|
@ -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
test/sp_owner_hash_value_test.cpp
Normal file
85
test/sp_owner_hash_value_test.cpp
Normal 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
test/wp_hash_test.cpp
Normal file
76
test/wp_hash_test.cpp
Normal 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
test/wp_hash_test2.cpp
Normal file
61
test/wp_hash_test2.cpp
Normal 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)
|
Reference in New Issue
Block a user