diff --git a/include/boost/smart_ptr/intrusive_ptr.hpp b/include/boost/smart_ptr/intrusive_ptr.hpp index ba23e5a..22f7e34 100644 --- a/include/boost/smart_ptr/intrusive_ptr.hpp +++ b/include/boost/smart_ptr/intrusive_ptr.hpp @@ -385,4 +385,23 @@ template< class T > std::size_t hash_value( boost::intrusive_ptr const & p ) } // namespace boost +// std::hash + +#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +namespace std +{ + +template struct hash< ::boost::intrusive_ptr > +{ + std::size_t operator()( ::boost::intrusive_ptr const & v ) const BOOST_SP_NOEXCEPT + { + return hash_value( v ); + } +}; + +} // namespace std + +#endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED diff --git a/include/boost/smart_ptr/local_shared_ptr.hpp b/include/boost/smart_ptr/local_shared_ptr.hpp index 30f3f32..c1fc459 100644 --- a/include/boost/smart_ptr/local_shared_ptr.hpp +++ b/include/boost/smart_ptr/local_shared_ptr.hpp @@ -688,4 +688,23 @@ template< class T > std::size_t hash_value( local_shared_ptr const & p ) BOOS } // namespace boost +// std::hash + +#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +namespace std +{ + +template struct hash< ::boost::local_shared_ptr > +{ + std::size_t operator()( ::boost::local_shared_ptr const & v ) const BOOST_SP_NOEXCEPT + { + return hash_value( v ); + } +}; + +} // namespace std + +#endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + #endif // #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED diff --git a/include/boost/smart_ptr/shared_ptr.hpp b/include/boost/smart_ptr/shared_ptr.hpp index 3a60199..ff6c290 100644 --- a/include/boost/smart_ptr/shared_ptr.hpp +++ b/include/boost/smart_ptr/shared_ptr.hpp @@ -1165,6 +1165,25 @@ template< class T > std::size_t hash_value( boost::shared_ptr const & p ) BOO } // namespace boost +// std::hash + +#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +namespace std +{ + +template struct hash< ::boost::shared_ptr > +{ + std::size_t operator()( ::boost::shared_ptr const & v ) const BOOST_SP_NOEXCEPT + { + return hash_value( v ); + } +}; + +} // namespace std + +#endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + #include namespace boost diff --git a/test/Jamfile b/test/Jamfile index 29981d6..4daede5 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -376,3 +376,8 @@ run owner_equal_to_test.cpp ; run owner_equal_to_test2.cpp ; run owner_less_test2.cpp ; + +run ip_hash_test2.cpp ; +run sp_hash_test4.cpp ; +run lsp_hash_test.cpp ; +run lsp_hash_test2.cpp ; diff --git a/test/ip_hash_test2.cpp b/test/ip_hash_test2.cpp new file mode 100644 index 0000000..e4a1cad --- /dev/null +++ b/test/ip_hash_test2.cpp @@ -0,0 +1,71 @@ +// Copyright 2011, 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +int main() {} + +#else + +class base +{ +private: + + int use_count_; + + base(base const &); + base & operator=(base const &); + +protected: + + base(): use_count_(0) + { + } + + virtual ~base() + { + } + +public: + + long use_count() const + { + return use_count_; + } + + inline friend void intrusive_ptr_add_ref(base * p) + { + ++p->use_count_; + } + + inline friend void intrusive_ptr_release(base * p) + { + if(--p->use_count_ == 0) delete p; + } +}; + +struct X: public base +{ +}; + +int main() +{ + boost::hash< boost::intrusive_ptr > hasher; + std::hash< boost::intrusive_ptr > hasher2; + + boost::intrusive_ptr p1, p2( new X ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + + return boost::report_errors(); +} + +#endif // #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) diff --git a/test/lsp_hash_test.cpp b/test/lsp_hash_test.cpp new file mode 100644 index 0000000..39e8547 --- /dev/null +++ b/test/lsp_hash_test.cpp @@ -0,0 +1,66 @@ +// Copyright 2011, 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int main() +{ + { + boost::hash< boost::local_shared_ptr > hasher; + + boost::local_shared_ptr p1, p2( p1 ), p3( new int ), p4( p3 ), p5( new int ); + + BOOST_TEST_EQ( p1, p2 ); + BOOST_TEST_EQ( hasher( p1 ), hasher( p2 ) ); + + BOOST_TEST_NE( p1, p3 ); + BOOST_TEST_NE( hasher( p1 ), hasher( p3 ) ); + + BOOST_TEST_EQ( p3, p4 ); + BOOST_TEST_EQ( hasher( p3 ), hasher( p4 ) ); + + BOOST_TEST_NE( p3, p5 ); + BOOST_TEST_NE( hasher( p3 ), hasher( p5 ) ); + } + + { + boost::hash< boost::local_shared_ptr > hasher; + + boost::local_shared_ptr p1, p2( p1 ), p3( new int[1] ), p4( p3 ), p5( new int[1] ); + + BOOST_TEST_EQ( p1, p2 ); + BOOST_TEST_EQ( hasher( p1 ), hasher( p2 ) ); + + BOOST_TEST_NE( p1, p3 ); + BOOST_TEST_NE( hasher( p1 ), hasher( p3 ) ); + + BOOST_TEST_EQ( p3, p4 ); + BOOST_TEST_EQ( hasher( p3 ), hasher( p4 ) ); + + BOOST_TEST_NE( p3, p5 ); + BOOST_TEST_NE( hasher( p3 ), hasher( p5 ) ); + } + + { + boost::hash< boost::local_shared_ptr > hasher; + + boost::local_shared_ptr p1, p2( p1 ), p3( new int[1] ), p4( p3 ), p5( new int[1] ); + + BOOST_TEST_EQ( p1, p2 ); + BOOST_TEST_EQ( hasher( p1 ), hasher( p2 ) ); + + BOOST_TEST_NE( p1, p3 ); + BOOST_TEST_NE( hasher( p1 ), hasher( p3 ) ); + + BOOST_TEST_EQ( p3, p4 ); + BOOST_TEST_EQ( hasher( p3 ), hasher( p4 ) ); + + BOOST_TEST_NE( p3, p5 ); + BOOST_TEST_NE( hasher( p3 ), hasher( p5 ) ); + } + + return boost::report_errors(); +} diff --git a/test/lsp_hash_test2.cpp b/test/lsp_hash_test2.cpp new file mode 100644 index 0000000..ce4b9f4 --- /dev/null +++ b/test/lsp_hash_test2.cpp @@ -0,0 +1,52 @@ +// Copyright 2011, 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +int main() {} + +#else + +int main() +{ + { + boost::hash< boost::local_shared_ptr > hasher; + std::hash< boost::local_shared_ptr > hasher2; + + boost::local_shared_ptr p1, p2( new int ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + { + boost::hash< boost::local_shared_ptr > hasher; + std::hash< boost::local_shared_ptr > hasher2; + + boost::local_shared_ptr p1, p2( new int[1] ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + { + boost::hash< boost::local_shared_ptr > hasher; + std::hash< boost::local_shared_ptr > hasher2; + + boost::local_shared_ptr p1, p2( new int[1] ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + return boost::report_errors(); +} + +#endif // #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) diff --git a/test/sp_hash_test4.cpp b/test/sp_hash_test4.cpp new file mode 100644 index 0000000..68f0a06 --- /dev/null +++ b/test/sp_hash_test4.cpp @@ -0,0 +1,52 @@ +// Copyright 2011, 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) + +int main() {} + +#else + +int main() +{ + { + boost::hash< boost::shared_ptr > hasher; + std::hash< boost::shared_ptr > hasher2; + + boost::shared_ptr p1, p2( new int ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + { + boost::hash< boost::shared_ptr > hasher; + std::hash< boost::shared_ptr > hasher2; + + boost::shared_ptr p1, p2( new int[1] ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + { + boost::hash< boost::shared_ptr > hasher; + std::hash< boost::shared_ptr > hasher2; + + boost::shared_ptr p1, p2( new int[1] ); + + BOOST_TEST_EQ( hasher( p1 ), hasher2( p1 ) ); + BOOST_TEST_EQ( hasher( p2 ), hasher2( p2 ) ); + } + + return boost::report_errors(); +} + +#endif // #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)