mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-10-16 17:35:25 +02:00
Compare commits
4 Commits
feature/we
...
feature/ow
Author | SHA1 | Date | |
---|---|---|---|
|
d38f64ded9 | ||
|
b66fe51566 | ||
|
1b5568d585 | ||
|
fad0c20263 |
@@ -37,6 +37,7 @@ include::smart_ptr/pointer_to_other.adoc[]
|
||||
include::smart_ptr/atomic_shared_ptr.adoc[]
|
||||
include::smart_ptr/owner_less.adoc[]
|
||||
include::smart_ptr/owner_equal_to.adoc[]
|
||||
include::smart_ptr/owner_hash.adoc[]
|
||||
|
||||
// appendix
|
||||
include::smart_ptr/techniques.adoc[]
|
||||
|
@@ -16,11 +16,11 @@ http://www.boost.org/LICENSE_1_0.txt
|
||||
## Changes in 1.74.0
|
||||
|
||||
* Added `owner_equals` to `shared_ptr`, `weak_ptr`, `local_shared_ptr`
|
||||
* Added `owner_equal_to`
|
||||
* Added `std::hash` specializations for `shared_ptr`, `local_shared_ptr`
|
||||
* Added `owner_hash_value` to `shared_ptr`, `weak_ptr`
|
||||
* Added `boost::hash` support and `std::hash`, `std::equal_to`
|
||||
specializations for `weak_ptr`
|
||||
* Added `owner_equal_to`, `owner_hash`
|
||||
* Added `std::hash` specializations for `shared_ptr`, `local_shared_ptr`
|
||||
* Added `boost::hash` support to, and `std::hash`, `std::equal_to`
|
||||
specializations for, `weak_ptr`
|
||||
|
||||
## Changes in 1.72.0
|
||||
|
||||
|
56
doc/smart_ptr/owner_hash.adoc
Normal file
56
doc/smart_ptr/owner_hash.adoc
Normal file
@@ -0,0 +1,56 @@
|
||||
////
|
||||
Copyright 2020 Peter Dimov
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
https://www.boost.org/LICENSE_1_0.txt
|
||||
////
|
||||
|
||||
[#owner_hash]
|
||||
# owner_hash
|
||||
:toc:
|
||||
:toc-title:
|
||||
:idprefix: owner_hash_to_
|
||||
|
||||
## Description
|
||||
|
||||
`owner_hash<T>` is a helper function object that takes a smart pointer `p`
|
||||
and returns `p.owner_hash_value()`. It's useful for creating unordered
|
||||
containers of `shared_ptr` that use ownership-based equality, instead of
|
||||
the default pointer value equality. (It can be used with `weak_ptr` too,
|
||||
but there's no need, because `boost::hash` and `std::hash` for `weak_ptr`
|
||||
already use ownership-based equality.)
|
||||
|
||||
## Example
|
||||
|
||||
```
|
||||
std::unordered_set< boost::shared_ptr<void>,
|
||||
boost::owner_hash< boost::shared_ptr<void> >,
|
||||
boost::owner_equal_to< boost::shared_ptr<void> > > set;
|
||||
```
|
||||
|
||||
## Synopsis
|
||||
|
||||
`owner_hash` is defined in `<boost/smart_ptr/owner_hash.hpp>`.
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
template<class T> struct owner_hash
|
||||
{
|
||||
typedef std::size_t result_type;
|
||||
typedef T argument_type;
|
||||
|
||||
std::size_t operator()( T const & p ) const noexcept;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
## Members
|
||||
|
||||
```
|
||||
std::size_t operator()( T const & p ) const noexcept;
|
||||
```
|
||||
[none]
|
||||
* {blank}
|
||||
+
|
||||
Returns::
|
||||
`p.owner_hash_value()`.
|
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,6 @@ run wp_hash_test.cpp ;
|
||||
run wp_hash_test2.cpp ;
|
||||
|
||||
run wp_unordered_test.cpp ;
|
||||
|
||||
run owner_hash_test.cpp ;
|
||||
run sp_unordered_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();
|
||||
}
|
51
test/sp_unordered_test.cpp
Normal file
51
test/sp_unordered_test.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2011, 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/smart_ptr/owner_hash.hpp>
|
||||
#include <boost/smart_ptr/owner_equal_to.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_UNORDERED_SET)
|
||||
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unordered_set< boost::shared_ptr<void>, boost::owner_hash< boost::shared_ptr<void> >, boost::owner_equal_to< boost::shared_ptr<void> > > set;
|
||||
|
||||
boost::shared_ptr<int> p1( (int*)0 );
|
||||
boost::shared_ptr<int> p2( p1 );
|
||||
boost::shared_ptr<void> p3( p1 );
|
||||
|
||||
set.insert( p1 );
|
||||
set.insert( p2 );
|
||||
set.insert( p3 );
|
||||
|
||||
BOOST_TEST_EQ( set.size(), 1 );
|
||||
|
||||
boost::shared_ptr<int> p4( (int*)0 );
|
||||
|
||||
set.insert( p4 );
|
||||
|
||||
BOOST_TEST_EQ( set.size(), 2 );
|
||||
|
||||
BOOST_TEST_EQ( set.count( p1 ), 1 );
|
||||
BOOST_TEST_EQ( set.count( p2 ), 1 );
|
||||
BOOST_TEST_EQ( set.count( p3 ), 1 );
|
||||
BOOST_TEST_EQ( set.count( p4 ), 1 );
|
||||
|
||||
boost::shared_ptr<int> p5( (int*)0 );
|
||||
|
||||
BOOST_TEST_EQ( set.count( p5 ), 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif // #if defined(BOOST_NO_CXX11_HDR_UNORDERED_SET)
|
Reference in New Issue
Block a user