From c4a7dba55687882d71e9024c3865519852e48cb0 Mon Sep 17 00:00:00 2001 From: Brent Lewis Date: Wed, 12 Sep 2012 14:44:39 -0700 Subject: [PATCH] weak_use_count() and weak_from_this() --- 1.49/include/boost/smart_ptr/detail/shared_count.hpp | 10 ++++++++++ .../boost/smart_ptr/detail/sp_counted_base_acc_ia64.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_aix.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_ppc.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_x86.hpp | 5 +++++ .../boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp | 5 +++++ .../boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp | 5 +++++ .../include/boost/smart_ptr/detail/sp_counted_base_gcc_ppc.hpp | 5 +++++ .../boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp | 5 +++++ .../include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_nt.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_pt.hpp | 9 +++++++++ .../include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_spin.hpp | 6 ++++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_sync.hpp | 5 +++++ 1.49/include/boost/smart_ptr/detail/sp_counted_base_w32.hpp | 5 +++++ 1.49/include/boost/smart_ptr/enable_shared_from_this.hpp | 10 ++++++++++ 1.49/include/boost/smart_ptr/shared_ptr.hpp | 5 +++++ 1.49/include/boost/smart_ptr/weak_ptr.hpp | 5 +++++ 19 files changed, 110 insertions(+) diff --git a/1.49/include/boost/smart_ptr/detail/shared_count.hpp b/1.49/include/boost/smart_ptr/detail/shared_count.hpp index f96a220..4e65009 100644 --- a/1.49/include/boost/smart_ptr/detail/shared_count.hpp +++ b/1.49/include/boost/smart_ptr/detail/shared_count.hpp @@ -356,6 +356,11 @@ public: { return pi_ != 0? pi_->use_count(): 0; } + + long weak_use_count() const // nothrow + { + return pi_ != 0? pi_->weak_use_count(): 0; + } bool unique() const // nothrow { @@ -482,6 +487,11 @@ public: { return pi_ != 0? pi_->use_count(): 0; } + + long weak_use_count() const // nothrow + { + return pi_ != 0? pi_->weak_use_count(): 0; + } bool empty() const // nothrow { diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_acc_ia64.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_acc_ia64.hpp index dffd995..887dc85 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_acc_ia64.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_acc_ia64.hpp @@ -141,6 +141,11 @@ public: { return static_cast( use_count_ ); // TODO use ld.acq here } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); // TODO use ld.acq here + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_aix.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_aix.hpp index 0208678..225954d 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_aix.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_aix.hpp @@ -133,6 +133,11 @@ public: { return fetch_and_add( const_cast(&use_count_), 0 ); } + + long weak_use_count() const // nothrow + { + return fetch_and_add( const_cast(&weak_count_), 0 ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_ppc.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_ppc.hpp index 51ac56a..b91c3e4 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_ppc.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_ppc.hpp @@ -161,6 +161,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_x86.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_x86.hpp index 1234e78..fb7feea 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_x86.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_cw_x86.hpp @@ -149,6 +149,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp index d122a49..c7443f0 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp @@ -148,6 +148,11 @@ public: { return static_cast( use_count_ ); // TODO use ld.acq here } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); // TODO use ld.acq here + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp index 3f1f449..489907b 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp @@ -172,6 +172,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ppc.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ppc.hpp index 7f5c414..f5b16fa 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ppc.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_ppc.hpp @@ -172,6 +172,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp index 21fa59d..55d9894 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp @@ -157,6 +157,11 @@ public: { return const_cast< int32_t const volatile & >( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast< int32_t const volatile &>( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp index 4d7fa8d..29a285a 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp @@ -164,6 +164,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_nt.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_nt.hpp index dfd70e7..af45ef4 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_nt.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_nt.hpp @@ -98,6 +98,11 @@ public: { return use_count_; } + + long weak_use_count() const // nothrow + { + return weak_count_; + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_pt.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_pt.hpp index 3c56fec..128da33 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_pt.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_pt.hpp @@ -126,6 +126,15 @@ public: return r; } + + long weak_use_count() const // nothrow + { + pthread_mutex_lock( &m_ ); + long r = weak_count_; + pthread_mutex_unlock( &m_ ); + + return r; + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp index d1b6bec..887d235 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp @@ -104,6 +104,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_spin.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_spin.hpp index bbd11e6..d3be6f1 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_spin.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_spin.hpp @@ -122,6 +122,12 @@ public: spinlock_pool<1>::scoped_lock lock( &use_count_ ); return use_count_; } + + long weak_use_count() const // nothrow + { + spinlock_pool<1>::scoped_lock lock( &weak_count_ ); + return weak_count_; + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_sync.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_sync.hpp index 41f654e..e6657e4 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_sync.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_sync.hpp @@ -146,6 +146,11 @@ public: { return const_cast< sp_int32_t const volatile & >( use_count_ ); } + + long weak_use_count() const // nothrow + { + return const_cast< sp_int32_t const volatile & >( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/detail/sp_counted_base_w32.hpp b/1.49/include/boost/smart_ptr/detail/sp_counted_base_w32.hpp index 06aa456..1a0a89b 100644 --- a/1.49/include/boost/smart_ptr/detail/sp_counted_base_w32.hpp +++ b/1.49/include/boost/smart_ptr/detail/sp_counted_base_w32.hpp @@ -121,6 +121,11 @@ public: { return static_cast( use_count_ ); } + + long weak_use_count() const // nothrow + { + return static_cast( weak_count_ ); + } }; } // namespace detail diff --git a/1.49/include/boost/smart_ptr/enable_shared_from_this.hpp b/1.49/include/boost/smart_ptr/enable_shared_from_this.hpp index f7b1445..870fda1 100644 --- a/1.49/include/boost/smart_ptr/enable_shared_from_this.hpp +++ b/1.49/include/boost/smart_ptr/enable_shared_from_this.hpp @@ -57,6 +57,16 @@ public: BOOST_ASSERT( p.get() == this ); return p; } + + weak_ptr weak_from_this() + { + return weak_this_; + } + + weak_ptr weak_from_this() const + { + return weak_this_; + } public: // actually private, but avoids compiler template friendship issues diff --git a/1.49/include/boost/smart_ptr/shared_ptr.hpp b/1.49/include/boost/smart_ptr/shared_ptr.hpp index 1bfb75c..626de30 100644 --- a/1.49/include/boost/smart_ptr/shared_ptr.hpp +++ b/1.49/include/boost/smart_ptr/shared_ptr.hpp @@ -442,6 +442,11 @@ public: { return pn.use_count(); } + + long weak_use_count() const //never throws + { + return pn.weak_use_count(); + } void swap(shared_ptr & other) // never throws { diff --git a/1.49/include/boost/smart_ptr/weak_ptr.hpp b/1.49/include/boost/smart_ptr/weak_ptr.hpp index 2e35583..6d5df5a 100644 --- a/1.49/include/boost/smart_ptr/weak_ptr.hpp +++ b/1.49/include/boost/smart_ptr/weak_ptr.hpp @@ -172,6 +172,11 @@ public: { return pn.use_count(); } + + long weak_use_count() const // never throws + { + return pn.weak_use_count(); + } bool expired() const // never throws { -- 1.7.11.1