Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r55000 - sandbox/fmhess/boost/smart_ptr
From: fmhess_at_[hidden]
Date: 2009-07-17 14:42:51


Author: fmhess
Date: 2009-07-17 14:42:50 EDT (Fri, 17 Jul 2009)
New Revision: 55000
URL: http://svn.boost.org/trac/boost/changeset/55000

Log:
Added generic_weak complement to generic_shared, and
wrapper implementation of weak_ptr for the sake of running
shared_ptr/weak_ptr test suite.

Added:
   sandbox/fmhess/boost/smart_ptr/generic_weak.hpp
      - copied, changed from r54999, /trunk/boost/smart_ptr/weak_ptr.hpp
   sandbox/fmhess/boost/smart_ptr/weak_ptr.hpp (contents, props changed)
Text files modified:
   sandbox/fmhess/boost/smart_ptr/generic_shared.hpp | 20 ++++++++++
   sandbox/fmhess/boost/smart_ptr/generic_weak.hpp | 73 ++++++++++++++++++++-------------------
   sandbox/fmhess/boost/smart_ptr/shared_ptr.hpp | 13 ++++++-
   3 files changed, 68 insertions(+), 38 deletions(-)

Modified: sandbox/fmhess/boost/smart_ptr/generic_shared.hpp
==============================================================================
--- sandbox/fmhess/boost/smart_ptr/generic_shared.hpp (original)
+++ sandbox/fmhess/boost/smart_ptr/generic_shared.hpp 2009-07-17 14:42:50 EDT (Fri, 17 Jul 2009)
@@ -71,6 +71,7 @@
 {
 
 template<class T> class generic_shared;
+template<class T> class generic_weak;
 
 template<class T> struct smart_pointer_traits
 {
@@ -207,6 +208,7 @@
     typedef typename boost::smart_pointer_traits<T>::value_type value_type;
     typedef T pointer;
     typedef typename boost::smart_pointer_traits<T>::reference reference;
+ typedef generic_weak<T> weak_type;
 
     generic_shared(): px(), pn()
     {
@@ -239,6 +241,22 @@
 // generated copy constructor, destructor are fine
 
     template<class Y>
+ explicit generic_shared(generic_weak<Y> const & r): pn(r.pn) // may throw
+ {
+ // it is now safe to copy r.px, as pn(r.pn) did not throw
+ px = r.px;
+ }
+
+ template<class Y>
+ generic_shared( generic_weak<Y> const & r, boost::detail::sp_nothrow_tag ): px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() ) // never throws
+ {
+ if( !pn.empty() )
+ {
+ px = r.px;
+ }
+ }
+
+ template<class Y>
 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
 
     generic_shared( generic_shared<Y> const & r, typename gs_detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
@@ -542,7 +560,7 @@
 private:
 
     template<class Y> friend class generic_shared;
- template<class Y> friend class weak_ptr;
+ template<class Y> friend class generic_weak;
 
 
 #endif

Copied: sandbox/fmhess/boost/smart_ptr/generic_weak.hpp (from r54999, /trunk/boost/smart_ptr/weak_ptr.hpp)
==============================================================================
--- /trunk/boost/smart_ptr/weak_ptr.hpp (original)
+++ sandbox/fmhess/boost/smart_ptr/generic_weak.hpp 2009-07-17 14:42:50 EDT (Fri, 17 Jul 2009)
@@ -1,21 +1,22 @@
-#ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
-#define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
+#ifndef BOOST_SMART_PTR_GENERIC_WEAK_HPP_INCLUDED
+#define BOOST_SMART_PTR_GENERIC_WEAK_HPP_INCLUDED
 
 //
-// weak_ptr.hpp
+// generic_weak.hpp
 //
 // Copyright (c) 2001, 2002, 2003 Peter Dimov
+// 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)
 //
-// See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
+// See http://www.boost.org/libs/smart_ptr/generic_weak.htm for documentation.
 //
 
 #include <memory> // boost.TR1 include order fix
 #include <boost/smart_ptr/detail/shared_count.hpp>
-#include <boost/smart_ptr/shared_ptr.hpp>
+#include <boost/smart_ptr/generic_shared.hpp>
 
 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
 # pragma warning(push)
@@ -25,18 +26,20 @@
 namespace boost
 {
 
-template<class T> class weak_ptr
+template<class T> class generic_weak
 {
 private:
 
     // Borland 5.5.1 specific workarounds
- typedef weak_ptr<T> this_type;
+ typedef generic_weak<T> this_type;
 
 public:
 
- typedef T element_type;
+ typedef typename smart_pointer_traits<T>::value_type element_type;
+ typedef typename smart_pointer_traits<T>::value_type value_type;
+ typedef generic_shared<T> shared_type;
 
- weak_ptr(): px(0), pn() // never throws in 1.30+
+ generic_weak(): px(), pn() // never throws in 1.30+
     {
     }
 
@@ -47,7 +50,7 @@
 // The "obvious" converting constructor implementation:
 //
 // template<class Y>
-// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
+// generic_weak(generic_weak<Y> const & r): px(r.px), pn(r.pn) // never throws
 // {
 // }
 //
@@ -63,11 +66,11 @@
     template<class Y>
 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
 
- weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+ generic_weak( generic_weak<Y> const & r, typename gs_detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
 
 #else
 
- weak_ptr( weak_ptr<Y> const & r )
+ generic_weak( generic_weak<Y> const & r )
 
 #endif
     : px(r.lock().get()), pn(r.pn) // never throws
@@ -79,26 +82,26 @@
     template<class Y>
 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
 
- weak_ptr( weak_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+ generic_weak( generic_weak<Y> && r, typename gs_detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
 
 #else
 
- weak_ptr( weak_ptr<Y> && r )
+ generic_weak( generic_weak<Y> && r )
 
 #endif
     : px(r.lock().get()), pn(std::move(r.pn)) // never throws
     {
- r.px = 0;
+ gs_detail::set_plain_pointer_to_null(r.px);
     }
 
     // for better efficiency in the T == Y case
- weak_ptr( weak_ptr && r ): px( r.px ), pn(std::move(r.pn)) // never throws
+ generic_weak( generic_weak && r ): px( r.px ), pn(std::move(r.pn)) // never throws
     {
- r.px = 0;
+ gs_detail::set_plain_pointer_to_null(r.px);
     }
 
     // for better efficiency in the T == Y case
- weak_ptr & operator=( weak_ptr && r ) // never throws
+ generic_weak & operator=( generic_weak && r ) // never throws
     {
         this_type( std::move( r ) ).swap( *this );
         return *this;
@@ -110,11 +113,11 @@
     template<class Y>
 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
 
- weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+ generic_weak( generic_shared<Y> const & r, typename gs_detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
 
 #else
 
- weak_ptr( shared_ptr<Y> const & r )
+ generic_weak( generic_shared<Y> const & r )
 
 #endif
     : px( r.px ), pn( r.pn ) // never throws
@@ -124,7 +127,7 @@
 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
 
     template<class Y>
- weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
+ generic_weak & operator=(generic_weak<Y> const & r) // never throws
     {
         px = r.lock().get();
         pn = r.pn;
@@ -134,7 +137,7 @@
 #if defined( BOOST_HAS_RVALUE_REFS )
 
     template<class Y>
- weak_ptr & operator=(weak_ptr<Y> && r)
+ generic_weak & operator=(generic_weak<Y> && r)
     {
         this_type( std::move( r ) ).swap( *this );
         return *this;
@@ -143,7 +146,7 @@
 #endif
 
     template<class Y>
- weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
+ generic_weak & operator=(generic_shared<Y> const & r) // never throws
     {
         px = r.px;
         pn = r.pn;
@@ -152,9 +155,9 @@
 
 #endif
 
- shared_ptr<T> lock() const // never throws
+ generic_shared<T> lock() const // never throws
     {
- return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
+ return generic_shared<T>( *this, boost::detail::sp_nothrow_tag() );
     }
 
     long use_count() const // never throws
@@ -183,13 +186,13 @@
         pn.swap(other.pn);
     }
 
- void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
+ void _internal_assign(T px2, boost::detail::shared_count const & pn2)
     {
         px = px2;
         pn = pn2;
     }
 
- template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
+ template<class Y> bool _internal_less(generic_weak<Y> const & rhs) const
     {
         return pn < rhs.pn;
     }
@@ -201,22 +204,22 @@
 
 private:
 
- template<class Y> friend class weak_ptr;
- template<class Y> friend class shared_ptr;
+ template<class Y> friend class generic_weak;
+ template<class Y> friend class generic_shared;
 
 #endif
 
- T * px; // contained pointer
+ T px; // contained pointer
     boost::detail::weak_count pn; // reference counter
 
-}; // weak_ptr
+}; // generic_weak
 
-template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
+template<class T, class U> inline bool operator<(generic_weak<T> const & a, generic_weak<U> const & b)
 {
     return a._internal_less(b);
 }
 
-template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
+template<class T> void swap(generic_weak<T> & a, generic_weak<T> & b)
 {
     a.swap(b);
 }
@@ -225,6 +228,6 @@
 
 #ifdef BOOST_MSVC
 # pragma warning(pop)
-#endif
+#endif
 
-#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
+#endif // #ifndef BOOST_SMART_PTR_GENERIC_WEAK_HPP_INCLUDED

Modified: sandbox/fmhess/boost/smart_ptr/shared_ptr.hpp
==============================================================================
--- sandbox/fmhess/boost/smart_ptr/shared_ptr.hpp (original)
+++ sandbox/fmhess/boost/smart_ptr/shared_ptr.hpp 2009-07-17 14:42:50 EDT (Fri, 17 Jul 2009)
@@ -23,6 +23,8 @@
                 }
         }
 
+ template<typename T> class weak_ptr;
+
         template<typename T>
         class shared_ptr: public generic_shared<T*>
         {
@@ -54,9 +56,16 @@
                 template<typename Y>
                 explicit shared_ptr(std::auto_ptr<Y> & r): base_type(r)
                 {}
- template<typename Ap>
- explicit shared_ptr( Ap r): base_type(r)
+#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
+ template<class Ap>
+ explicit shared_ptr( Ap r, typename boost::gs_detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ):
+ base_type(r)
                 {}
+#endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
+ template<class Y>
+ explicit shared_ptr(weak_ptr<Y> const & r): base_type(static_cast<generic_weak<Y*> const &>(r))
+ {}
+
                 template<typename Y>
                 shared_ptr & operator=( std::auto_ptr<Y> & r )
                 {

Added: sandbox/fmhess/boost/smart_ptr/weak_ptr.hpp
==============================================================================
--- (empty file)
+++ sandbox/fmhess/boost/smart_ptr/weak_ptr.hpp 2009-07-17 14:42:50 EDT (Fri, 17 Jul 2009)
@@ -0,0 +1,49 @@
+#ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
+#define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
+
+// Implementation of weak_ptr as wrapper around generic_weak.
+// Written primarily to help test generic_shared/generic_weak using unmodified
+// shared_ptr tests.
+
+// 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/generic_weak.hpp>
+#include <boost/smart_ptr/shared_ptr.hpp>
+
+namespace boost
+{
+ template<typename T>
+ class weak_ptr: public generic_weak<T*>
+ {
+ typedef generic_weak<T*> base_type;
+ public:
+ weak_ptr() {}
+ weak_ptr(base_type const & b): base_type(b) {}
+ template<class Y>
+#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
+ weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+#else
+ weak_ptr( weak_ptr<Y> const & r )
+#endif
+ :base_type(static_cast<generic_weak<Y*> const &>(r))
+ {}
+ template<class Y>
+#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
+ weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+#else
+ weak_ptr( shared_ptr<Y> const & r )
+#endif
+ :base_type(static_cast<generic_shared<Y*> const &>(r))
+ {}
+ shared_ptr<T> lock() const
+ {
+ return base_type::lock();
+ }
+ };
+}
+
+#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED


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