Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69260 - in trunk: boost/detail boost/smart_ptr libs/smart_ptr/test
From: pdimov_at_[hidden]
Date: 2011-02-24 18:24:56


Author: pdimov
Date: 2011-02-24 18:24:54 EST (Thu, 24 Feb 2011)
New Revision: 69260
URL: http://svn.boost.org/trac/boost/changeset/69260

Log:
Add hash_value for shared_ptr; prevents hash_value( bool ) from being used. Refs #5216.
Added:
   trunk/libs/smart_ptr/test/sp_hash_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/detail/lightweight_test.hpp | 17 +++++++++++++++++
   trunk/boost/smart_ptr/shared_ptr.hpp | 12 +++++++++++-
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1 +
   3 files changed, 29 insertions(+), 1 deletions(-)

Modified: trunk/boost/detail/lightweight_test.hpp
==============================================================================
--- trunk/boost/detail/lightweight_test.hpp (original)
+++ trunk/boost/detail/lightweight_test.hpp 2011-02-24 18:24:54 EST (Thu, 24 Feb 2011)
@@ -95,6 +95,22 @@
     }
 }
 
+template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
+ char const * file, int line, char const * function, T const & t, U const & u )
+{
+ if( t != u )
+ {
+ }
+ else
+ {
+ BOOST_LIGHTWEIGHT_TEST_OSTREAM
+ << file << "(" << line << "): test '" << expr1 << " != " << expr2
+ << "' failed in function '" << function << "': "
+ << "'" << t << "' == '" << u << "'" << std::endl;
+ ++test_errors();
+ }
+}
+
 } // namespace detail
 
 inline int report_errors()
@@ -122,5 +138,6 @@
 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
 #define BOOST_ERROR(msg) ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
+#define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
 
 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED

Modified: trunk/boost/smart_ptr/shared_ptr.hpp
==============================================================================
--- trunk/boost/smart_ptr/shared_ptr.hpp (original)
+++ trunk/boost/smart_ptr/shared_ptr.hpp 2011-02-24 18:24:54 EST (Thu, 24 Feb 2011)
@@ -41,6 +41,7 @@
 #include <algorithm> // for std::swap
 #include <functional> // for std::less
 #include <typeinfo> // for std::bad_cast
+#include <cstddef> // for std::size_t
 
 #if !defined(BOOST_NO_IOSTREAM)
 #if !defined(BOOST_NO_IOSFWD)
@@ -722,7 +723,16 @@
     return atomic_compare_exchange( p, v, w ); // std::move( w )
 }
 
-#endif
+#endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
+
+// hash_value
+
+template< class T > struct hash;
+
+template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p )
+{
+ return boost::hash< T* >()( p.get() );
+}
 
 } // namespace boost
 

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2011-02-24 18:24:54 EST (Thu, 24 Feb 2011)
@@ -65,5 +65,6 @@
           [ run atomic_count_test2.cpp ]
           [ run sp_typeinfo_test.cpp ]
           [ compile make_shared_fp_test.cpp ]
+ [ run sp_hash_test.cpp ]
         ;
 }

Added: trunk/libs/smart_ptr/test/sp_hash_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/smart_ptr/test/sp_hash_test.cpp 2011-02-24 18:24:54 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,34 @@
+//
+// sp_hash_test.cpp
+//
+// Copyright 2011 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0.
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+
+#include <boost/shared_ptr.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+int main()
+{
+ boost::hash< boost::shared_ptr<int> > hasher;
+
+ boost::shared_ptr< int > 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 ) );
+
+ return boost::report_errors();
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk