Modified weak_ptr to reflect list comments.

[SVN r12793]
This commit is contained in:
Peter Dimov committed 2002-02-13 13:16:15 +00:00
1 parent b1a1ab99aa
commit 6c5d296722
3 files changed
+70 -70

No files matched your search

+6 -6
View File
@@ -31,19 +31,19 @@
namespace boost
{
namespace detail
{
class bad_weak_to_shared_cast: public std::exception
class use_count_is_zero: public std::exception
{
public:
virtual char const * what() const throw()
{
return "bad_weak_to_shared_cast";
return "use_count_is_zero";
}
};
namespace detail
{
class counted_base
{
public:
@@ -75,7 +75,7 @@ public:
void add_ref()
{
lightweight_mutex::scoped_lock lock(mtx_);
if(use_count_ == 0) throw bad_weak_to_shared_cast();
if(use_count_ == 0) throw use_count_is_zero();
++use_count_;
++weak_count_;
}
+20 -40
View File
@@ -30,7 +30,6 @@ private:
// Borland 5.5.1 specific workarounds
typedef weak_ptr<T> this_type;
typedef shared_ptr<T> shared_type;
public:
@@ -96,53 +95,16 @@ public:
this_type().swap(*this);
}
shared_type get() const // never throws
T * get() const // never throws; unsafe in multithreaded programs!
{
// optimization: avoid throw overhead
if(use_count() == 0)
{
return shared_type();
}
try
{
return shared_type(*this);
}
catch(boost::detail::bad_weak_to_shared_cast const &)
{
return shared_type();
}
return use_count() == 0? 0: px;
}
// operator* has been removed; it's unsafe.
// operator-> retained for convenience, since it's safe
// in its current form.
shared_type operator-> () const // may throw
{
return shared_type(*this);
}
long use_count() const // never throws
{
return pn.use_count();
}
// implicit conversion to "bool"
typedef long (this_type::*bool_type)() const;
operator bool_type() const // never throws
{
return px == 0 || use_count() == 0? 0: &this_type::use_count;
}
bool operator! () const // never throws
{
return px == 0 || use_count() == 0;
}
void swap(this_type & other) // never throws
{
std::swap(px, other.px);
@@ -202,6 +164,24 @@ template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
a.swap(b);
}
template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r) // never throws
{
// optimization: avoid throw overhead
if(r.use_count() == 0)
{
return shared_ptr<T>();
}
try
{
return shared_ptr<T>(r);
}
catch(use_count_is_zero const &)
{
return shared_ptr<T>();
}
}
template<class T, class U> weak_ptr<T> shared_static_cast(weak_ptr<U> const & r)
{
return weak_ptr<T>(r, detail::static_cast_tag());