Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76111 - in trunk: boost/smart_ptr libs/smart_ptr/test
From: pdimov_at_[hidden]
Date: 2011-12-22 22:00:07


Author: pdimov
Date: 2011-12-22 22:00:05 EST (Thu, 22 Dec 2011)
New Revision: 76111
URL: http://svn.boost.org/trac/boost/changeset/76111

Log:
Creatively apply patch from #2603. Refs #2603.
Added:
   trunk/boost/smart_ptr/owner_less.hpp (contents, props changed)
   trunk/libs/smart_ptr/test/owner_less_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/smart_ptr/shared_ptr.hpp | 9 +++++++--
   trunk/boost/smart_ptr/weak_ptr.hpp | 9 +++++++--
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1 +
   3 files changed, 15 insertions(+), 4 deletions(-)

Added: trunk/boost/smart_ptr/owner_less.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/smart_ptr/owner_less.hpp 2011-12-22 22:00:05 EST (Thu, 22 Dec 2011)
@@ -0,0 +1,57 @@
+#ifndef BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED
+#define BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED
+
+//
+// owner_less.hpp
+//
+// Copyright (c) 2008 Frank Mori Hess
+//
+// 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)
+//
+// See http://www.boost.org/libs/smart_ptr/smart_ptr.htm for documentation.
+//
+
+#include <functional>
+
+namespace boost
+{
+ template<typename T> class shared_ptr;
+ template<typename T> class weak_ptr;
+
+ namespace detail
+ {
+ template<typename T, typename U>
+ struct generic_owner_less : public std::binary_function<T, T, bool>
+ {
+ bool operator()(const T &lhs, const T &rhs) const
+ {
+ return lhs.owner_before(rhs);
+ }
+ bool operator()(const T &lhs, const U &rhs) const
+ {
+ return lhs.owner_before(rhs);
+ }
+ bool operator()(const U &lhs, const T &rhs) const
+ {
+ return lhs.owner_before(rhs);
+ }
+ };
+ } // namespace detail
+
+ template<typename T> struct owner_less;
+
+ template<typename T>
+ struct owner_less<shared_ptr<T> >:
+ public detail::generic_owner_less<shared_ptr<T>, weak_ptr<T> >
+ {};
+
+ template<typename T>
+ struct owner_less<weak_ptr<T> >:
+ public detail::generic_owner_less<weak_ptr<T>, shared_ptr<T> >
+ {};
+
+} // namespace boost
+
+#endif // #ifndef BOOST_SMART_PTR_OWNER_LESS_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-12-22 22:00:05 EST (Thu, 22 Dec 2011)
@@ -443,7 +443,12 @@
         pn.swap(other.pn);
     }
 
- template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
+ template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const
+ {
+ return pn < rhs.pn;
+ }
+
+ template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
     {
         return pn < rhs.pn;
     }
@@ -499,7 +504,7 @@
 
 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
 {
- return a._internal_less(b);
+ return a.owner_before( b );
 }
 
 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)

Modified: trunk/boost/smart_ptr/weak_ptr.hpp
==============================================================================
--- trunk/boost/smart_ptr/weak_ptr.hpp (original)
+++ trunk/boost/smart_ptr/weak_ptr.hpp 2011-12-22 22:00:05 EST (Thu, 22 Dec 2011)
@@ -206,7 +206,12 @@
         pn = r.pn;
     }
 
- template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
+ template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
+ {
+ return pn < rhs.pn;
+ }
+
+ template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const
     {
         return pn < rhs.pn;
     }
@@ -230,7 +235,7 @@
 
 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
 {
- return a._internal_less(b);
+ return a.owner_before( b );
 }
 
 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2011-12-22 22:00:05 EST (Thu, 22 Dec 2011)
@@ -68,5 +68,6 @@
           [ run sp_hash_test.cpp ]
           [ run get_deleter_array_test.cpp ]
           [ run ip_hash_test.cpp ]
+ [ run owner_less_test.cpp ]
         ;
 }

Added: trunk/libs/smart_ptr/test/owner_less_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/smart_ptr/test/owner_less_test.cpp 2011-12-22 22:00:05 EST (Thu, 22 Dec 2011)
@@ -0,0 +1,67 @@
+//
+// owner_less_test.cpp
+//
+// A regression test for owner_less
+//
+// Copyright (c) 2008 Frank Mori Hess
+//
+// 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/detail/lightweight_test.hpp>
+#include <boost/smart_ptr/owner_less.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+
+int main()
+{
+ boost::owner_less<boost::shared_ptr<int> > comp;
+ {
+ boost::shared_ptr<int> x;
+ boost::shared_ptr<int> y;
+ boost::weak_ptr<int> w;
+ BOOST_TEST(!(comp(x, w) || comp(w, x)));
+ }
+ {
+ boost::shared_ptr<int> z((int*)0);
+ boost::weak_ptr<int> w;
+ BOOST_TEST(comp(z, w) || comp(w, z));
+ {
+ boost::shared_ptr<int> zz(z);
+ w = boost::weak_ptr<int>(zz);
+ BOOST_TEST(!(comp(z, zz) || comp(z, zz)));
+ BOOST_TEST(!(comp(z, w) || comp(z, w)));
+ }
+ BOOST_TEST(!(comp(z, w) || comp(w, z)));
+ }
+ {
+ boost::shared_ptr<int> x;
+ boost::shared_ptr<int> z((int*)0);
+ BOOST_TEST(comp(x, z) || comp(z, x));
+ }
+ {
+ boost::shared_ptr<int> a((int*)0);
+ boost::shared_ptr<int> b((int*)0);
+ BOOST_TEST(comp(a, b) || comp(b, a));
+ boost::weak_ptr<int> w(a);
+ BOOST_TEST(!(comp(a, w) || comp(w, a)));
+ BOOST_TEST(comp(b, w) || comp(w, b));
+ }
+
+ boost::owner_less<boost::weak_ptr<int> > weak_comp;
+ {
+ boost::shared_ptr<int> a((int*)0);
+ boost::weak_ptr<int> wa(a);
+ boost::shared_ptr<int> b((int*)0);
+ boost::weak_ptr<int> wb(b);
+ BOOST_TEST(!(weak_comp(a, wa) || weak_comp(wa, a)));
+ BOOST_TEST(!(weak_comp(b, wb) || weak_comp(wb, b)));
+ BOOST_TEST(weak_comp(wa, wb) || weak_comp(wb, wa));
+ BOOST_TEST(weak_comp(wa, b) || weak_comp(b, wa));
+ }
+
+ 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