|
Boost-Commit : |
From: pdimov_at_[hidden]
Date: 2008-04-12 10:27:23
Author: pdimov
Date: 2008-04-12 10:27:22 EDT (Sat, 12 Apr 2008)
New Revision: 44344
URL: http://svn.boost.org/trac/boost/changeset/44344
Log:
shared_ptr::lock no longer requires exceptions.
Text files modified:
trunk/boost/detail/shared_count.hpp | 19 +++++++++++++++++++
trunk/boost/shared_ptr.hpp | 9 +++++++++
trunk/boost/weak_ptr.hpp | 26 +-------------------------
3 files changed, 29 insertions(+), 25 deletions(-)
Modified: trunk/boost/detail/shared_count.hpp
==============================================================================
--- trunk/boost/detail/shared_count.hpp (original)
+++ trunk/boost/detail/shared_count.hpp 2008-04-12 10:27:22 EDT (Sat, 12 Apr 2008)
@@ -46,6 +46,8 @@
#endif
+struct sp_nothrow_tag {};
+
class weak_count;
class shared_count
@@ -216,6 +218,7 @@
}
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
+ shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
shared_count & operator= (shared_count const & r) // nothrow
{
@@ -248,6 +251,11 @@
return use_count() == 1;
}
+ bool empty() const // nothrow
+ {
+ return pi_ == 0;
+ }
+
friend inline bool operator==(shared_count const & a, shared_count const & b)
{
return a.pi_ == b.pi_;
@@ -364,6 +372,17 @@
}
}
+inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
+#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
+ , id_(shared_count_id)
+#endif
+{
+ if( pi_ != 0 && !pi_->add_ref_lock() )
+ {
+ pi_ = 0;
+ }
+}
+
} // namespace detail
} // namespace boost
Modified: trunk/boost/shared_ptr.hpp
==============================================================================
--- trunk/boost/shared_ptr.hpp (original)
+++ trunk/boost/shared_ptr.hpp 2008-04-12 10:27:22 EDT (Sat, 12 Apr 2008)
@@ -221,6 +221,15 @@
}
template<class Y>
+ shared_ptr( weak_ptr<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>
shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
{
}
Modified: trunk/boost/weak_ptr.hpp
==============================================================================
--- trunk/boost/weak_ptr.hpp (original)
+++ trunk/boost/weak_ptr.hpp 2008-04-12 10:27:22 EDT (Sat, 12 Apr 2008)
@@ -93,31 +93,7 @@
shared_ptr<T> lock() const // never throws
{
-#if defined(BOOST_HAS_THREADS)
-
- // optimization: avoid throw overhead
- if(expired())
- {
- return shared_ptr<element_type>();
- }
-
- try
- {
- return shared_ptr<element_type>(*this);
- }
- catch(bad_weak_ptr const &)
- {
- // Q: how can we get here?
- // A: another thread may have invalidated r after the use_count test above.
- return shared_ptr<element_type>();
- }
-
-#else
-
- // optimization: avoid try/catch overhead when single threaded
- return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
-
-#endif
+ return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
}
long use_count() const // never throws
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