Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57423 - in trunk: boost/smart_ptr libs/smart_ptr/test
From: fmhess_at_[hidden]
Date: 2009-11-05 16:41:39


Author: fmhess
Date: 2009-11-05 16:41:38 EST (Thu, 05 Nov 2009)
New Revision: 57423
URL: http://svn.boost.org/trac/boost/changeset/57423

Log:
Added weak_from_raw(), for use in conjunction with
enable_shared_from_raw base class.

Added:
   trunk/libs/smart_ptr/test/enable_shared_from_raw_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/smart_ptr/enable_shared_from_raw.hpp | 12 ++++++++++++
   trunk/boost/smart_ptr/weak_ptr.hpp | 7 ++++---
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1 +
   3 files changed, 17 insertions(+), 3 deletions(-)

Modified: trunk/boost/smart_ptr/enable_shared_from_raw.hpp
==============================================================================
--- trunk/boost/smart_ptr/enable_shared_from_raw.hpp (original)
+++ trunk/boost/smart_ptr/enable_shared_from_raw.hpp 2009-11-05 16:41:38 EST (Thu, 05 Nov 2009)
@@ -21,6 +21,7 @@
 namespace boost
 {
 template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
+template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
 
 namespace detail
 {
@@ -72,6 +73,7 @@
 private:
     template<class Y> friend class shared_ptr;
     template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
+ template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
     template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
 #endif
 
@@ -114,9 +116,19 @@
 template<typename T>
 boost::shared_ptr<T> shared_from_raw(T *p)
 {
+ BOOST_ASSERT(p != 0);
     return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
 }
 
+template<typename T>
+boost::weak_ptr<T> weak_from_raw(T *p)
+{
+ BOOST_ASSERT(p != 0);
+ boost::weak_ptr<T> result;
+ result._internal_aliasing_assign(p->enable_shared_from_raw::weak_this_, p);
+ return result;
+}
+
 namespace detail
 {
     template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )

Modified: trunk/boost/smart_ptr/weak_ptr.hpp
==============================================================================
--- trunk/boost/smart_ptr/weak_ptr.hpp (original)
+++ trunk/boost/smart_ptr/weak_ptr.hpp 2009-11-05 16:41:38 EST (Thu, 05 Nov 2009)
@@ -183,10 +183,11 @@
         pn.swap(other.pn);
     }
 
- void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
+ template<typename Y>
+ void _internal_aliasing_assign(weak_ptr<Y> const & r, T * px2)
     {
         px = px2;
- pn = pn2;
+ pn = r.pn;
     }
 
     template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
@@ -225,6 +226,6 @@
 
 #ifdef BOOST_MSVC
 # pragma warning(pop)
-#endif
+#endif
 
 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2009-11-05 16:41:38 EST (Thu, 05 Nov 2009)
@@ -59,6 +59,7 @@
           [ run sp_recursive_assign_rv_test.cpp ]
           [ run sp_recursive_assign2_rv_test.cpp ]
           [ run esft_constructor_test.cpp ]
+ [ run enable_shared_from_raw_test.cpp ]
           [ compile-fail auto_ptr_lv_fail.cpp ]
           [ run atomic_count_test2.cpp ]
         ;

Added: trunk/libs/smart_ptr/test/enable_shared_from_raw_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/smart_ptr/test/enable_shared_from_raw_test.cpp 2009-11-05 16:41:38 EST (Thu, 05 Nov 2009)
@@ -0,0 +1,39 @@
+#include <boost/config.hpp>
+
+//
+// weak_from_raw_test.cpp
+//
+// Copyright (c) 2009 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/smart_ptr/enable_shared_from_raw.hpp>
+
+#include <boost/detail/lightweight_test.hpp>
+
+
+struct X: public boost::enable_shared_from_raw
+{};
+
+void basic_weak_from_raw_test()
+{
+ X *p(new X);
+ boost::weak_ptr<X> weak = boost::weak_from_raw(p);
+ BOOST_TEST(weak.expired());
+ boost::shared_ptr<X> shared(p);
+ weak = boost::weak_from_raw(p);
+ BOOST_TEST(weak.expired() == false);
+ boost::shared_ptr<X> shared2(weak);
+ BOOST_TEST((shared < shared2 || shared2 < shared) == false);
+ BOOST_TEST(shared.get() == p);
+}
+
+int main()
+{
+ basic_weak_from_raw_test();
+ 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