forked from boostorg/smart_ptr
weak_ptr::operator< is now stable, shared_ptr_test has much better coverage.
[SVN r12769]
This commit is contained in:
@@ -25,6 +25,8 @@
|
|||||||
#include <boost/checked_delete.hpp>
|
#include <boost/checked_delete.hpp>
|
||||||
#include <boost/detail/atomic_count.hpp>
|
#include <boost/detail/atomic_count.hpp>
|
||||||
|
|
||||||
|
#include <functional> // for std::less
|
||||||
|
|
||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -215,6 +217,16 @@ public:
|
|||||||
{
|
{
|
||||||
return pi_->use_count() == 1;
|
return pi_->use_count() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend inline bool operator==(shared_count const & a, shared_count const & b)
|
||||||
|
{
|
||||||
|
return a.pi_ == b.pi_;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend inline bool operator<(shared_count const & a, shared_count const & b)
|
||||||
|
{
|
||||||
|
return std::less<counted_base *>()(a.pi_, b.pi_);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class weak_count
|
class weak_count
|
||||||
@@ -275,6 +287,16 @@ public:
|
|||||||
{
|
{
|
||||||
return pi_->use_count();
|
return pi_->use_count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend inline bool operator==(weak_count const & a, weak_count const & b)
|
||||||
|
{
|
||||||
|
return a.pi_ == b.pi_;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend inline bool operator<(weak_count const & a, weak_count const & b)
|
||||||
|
{
|
||||||
|
return std::less<counted_base *>()(a.pi_, b.pi_);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
@@ -29,6 +29,7 @@
|
|||||||
#include <memory> // for std::auto_ptr
|
#include <memory> // for std::auto_ptr
|
||||||
#include <algorithm> // for std::swap
|
#include <algorithm> // for std::swap
|
||||||
#include <functional> // for std::less
|
#include <functional> // for std::less
|
||||||
|
#include <typeinfo> // for std::bad_cast
|
||||||
|
|
||||||
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
||||||
# pragma warning(push)
|
# pragma warning(push)
|
||||||
|
@@ -22,7 +22,7 @@
|
|||||||
#include <boost/detail/shared_count.hpp>
|
#include <boost/detail/shared_count.hpp>
|
||||||
|
|
||||||
#include <algorithm> // for std::swap
|
#include <algorithm> // for std::swap
|
||||||
#include <functional> // for std::less
|
#include <typeinfo> // for std::bad_cast
|
||||||
|
|
||||||
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
||||||
# pragma warning(push)
|
# pragma warning(push)
|
||||||
@@ -133,6 +133,11 @@ public:
|
|||||||
pn.swap(other.pn);
|
pn.swap(other.pn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool less(this_type const & rhs) const // implementation detail, never throws
|
||||||
|
{
|
||||||
|
return pn < rhs.pn;
|
||||||
|
}
|
||||||
|
|
||||||
// Tasteless as this may seem, making all members public allows member templates
|
// Tasteless as this may seem, making all members public allows member templates
|
||||||
// to work in the absence of member template friends. (Matthew Langston)
|
// to work in the absence of member template friends. (Matthew Langston)
|
||||||
|
|
||||||
@@ -161,7 +166,7 @@ template<class T, class U> inline bool operator!=(weak_ptr<T> const & a, weak_pt
|
|||||||
|
|
||||||
template<class T> inline bool operator<(weak_ptr<T> const & a, weak_ptr<T> const & b)
|
template<class T> inline bool operator<(weak_ptr<T> const & a, weak_ptr<T> const & b)
|
||||||
{
|
{
|
||||||
return std::less<T*>()(a.get(), b.get());
|
return a.less(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
|
template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
|
||||||
|
@@ -46,6 +46,11 @@ struct X
|
|||||||
std::cout << "X(" << this << ")::~X()\n";
|
std::cout << "X(" << this << ")::~X()\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual int id() const
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
X(X const &);
|
X(X const &);
|
||||||
@@ -66,6 +71,11 @@ struct Y: public X
|
|||||||
std::cout << "Y(" << this << ")::~Y()\n";
|
std::cout << "Y(" << this << ")::~Y()\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual int id() const
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
Y(Y const &);
|
Y(Y const &);
|
||||||
@@ -86,6 +96,76 @@ void release_object(int * p)
|
|||||||
std::cout << "release_object()\n";
|
std::cout << "release_object()\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T> void test_is_X(T const & p)
|
||||||
|
{
|
||||||
|
BOOST_TEST(p->id() == 1);
|
||||||
|
BOOST_TEST((*p).id() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> void test_is_Y(T const & p)
|
||||||
|
{
|
||||||
|
BOOST_TEST(p->id() == 2);
|
||||||
|
BOOST_TEST((*p).id() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// std::rel_ops::operator!= breaks x != y when defined in the global namespace
|
||||||
|
|
||||||
|
#if defined(__STL_BEGIN_RELOPS_NAMESPACE) && !defined(__STL_USE_NAMESPACE_FOR_RELOPS)
|
||||||
|
# define BOOST_BROKEN_INEQUALITY
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class T> void test_eq(T const & a, T const & b)
|
||||||
|
{
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
|
||||||
|
#ifndef BOOST_BROKEN_INEQUALITY
|
||||||
|
|
||||||
|
BOOST_TEST(!(a != b));
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
BOOST_TEST(!(a < b));
|
||||||
|
BOOST_TEST(!(b < a));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> void test_ne(T const & a, T const & b)
|
||||||
|
{
|
||||||
|
BOOST_TEST(!(a == b));
|
||||||
|
|
||||||
|
#ifndef BOOST_BROKEN_INEQUALITY
|
||||||
|
|
||||||
|
BOOST_TEST(a != b);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
BOOST_TEST(a < b || b < a);
|
||||||
|
BOOST_TEST(!(a < b && b < a));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> void test_eq2(T const & a, U const & b)
|
||||||
|
{
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
|
||||||
|
#ifndef BOOST_BROKEN_INEQUALITY
|
||||||
|
|
||||||
|
BOOST_TEST(!(a != b));
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> void test_ne2(T const & a, U const & b)
|
||||||
|
{
|
||||||
|
BOOST_TEST(!(a == b));
|
||||||
|
|
||||||
|
#ifndef BOOST_BROKEN_INEQUALITY
|
||||||
|
|
||||||
|
BOOST_TEST(a != b);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int test_main(int, char * [])
|
int test_main(int, char * [])
|
||||||
{
|
{
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
@@ -94,6 +174,15 @@ int test_main(int, char * [])
|
|||||||
shared_ptr<X> p(new Y);
|
shared_ptr<X> p(new Y);
|
||||||
shared_ptr<X> p2(new X);
|
shared_ptr<X> p2(new X);
|
||||||
|
|
||||||
|
test_is_Y(p);
|
||||||
|
test_is_X(p2);
|
||||||
|
test_ne(p, p2);
|
||||||
|
|
||||||
|
{
|
||||||
|
shared_ptr<X> q(p);
|
||||||
|
test_eq(p, q);
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<Y> p3 = shared_dynamic_cast<Y>(p);
|
shared_ptr<Y> p3 = shared_dynamic_cast<Y>(p);
|
||||||
shared_ptr<Y> p4 = shared_dynamic_cast<Y>(p2);
|
shared_ptr<Y> p4 = shared_dynamic_cast<Y>(p2);
|
||||||
|
|
||||||
@@ -102,8 +191,14 @@ int test_main(int, char * [])
|
|||||||
BOOST_TEST(p3.use_count() == 2);
|
BOOST_TEST(p3.use_count() == 2);
|
||||||
BOOST_TEST(p4.use_count() == 1);
|
BOOST_TEST(p4.use_count() == 1);
|
||||||
|
|
||||||
|
test_is_Y(p3);
|
||||||
|
test_eq2(p, p3);
|
||||||
|
test_ne2(p2, p4);
|
||||||
|
|
||||||
shared_ptr<void> p5(p);
|
shared_ptr<void> p5(p);
|
||||||
|
|
||||||
|
test_eq2(p, p5);
|
||||||
|
|
||||||
std::cout << "--\n";
|
std::cout << "--\n";
|
||||||
|
|
||||||
p.reset();
|
p.reset();
|
||||||
@@ -125,14 +220,20 @@ int test_main(int, char * [])
|
|||||||
BOOST_TEST(wp2.use_count() == 1);
|
BOOST_TEST(wp2.use_count() == 1);
|
||||||
BOOST_TEST(wp2.get() != 0);
|
BOOST_TEST(wp2.get() != 0);
|
||||||
|
|
||||||
|
test_is_Y(wp2);
|
||||||
|
test_ne(wp1, wp2);
|
||||||
|
|
||||||
weak_ptr<Y> wp3 = shared_dynamic_cast<Y>(wp2);
|
weak_ptr<Y> wp3 = shared_dynamic_cast<Y>(wp2);
|
||||||
|
|
||||||
BOOST_TEST(wp3.use_count() == 1);
|
BOOST_TEST(wp3.use_count() == 1);
|
||||||
BOOST_TEST(wp3.get() != 0);
|
BOOST_TEST(wp3.get() != 0);
|
||||||
BOOST_TEST(wp2 == wp3);
|
|
||||||
|
test_eq2(wp2, wp3);
|
||||||
|
|
||||||
weak_ptr<X> wp4(wp3);
|
weak_ptr<X> wp4(wp3);
|
||||||
|
|
||||||
|
test_eq(wp2, wp4);
|
||||||
|
|
||||||
wp1 = p2;
|
wp1 = p2;
|
||||||
wp1 = p4;
|
wp1 = p4;
|
||||||
wp1 = wp3;
|
wp1 = wp3;
|
||||||
@@ -140,7 +241,13 @@ int test_main(int, char * [])
|
|||||||
|
|
||||||
BOOST_TEST(wp1.use_count() == 1);
|
BOOST_TEST(wp1.use_count() == 1);
|
||||||
BOOST_TEST(wp1.get() != 0);
|
BOOST_TEST(wp1.get() != 0);
|
||||||
BOOST_TEST(wp1 == wp2);
|
|
||||||
|
test_eq(wp1, wp2);
|
||||||
|
|
||||||
|
weak_ptr<X> wp5;
|
||||||
|
|
||||||
|
bool b1 = wp1 < wp5;
|
||||||
|
bool b2 = wp5 < wp1;
|
||||||
|
|
||||||
p5.reset();
|
p5.reset();
|
||||||
|
|
||||||
@@ -153,6 +260,12 @@ int test_main(int, char * [])
|
|||||||
BOOST_TEST(wp3.use_count() == 0);
|
BOOST_TEST(wp3.use_count() == 0);
|
||||||
BOOST_TEST(wp3.get() == 0);
|
BOOST_TEST(wp3.get() == 0);
|
||||||
|
|
||||||
|
// Test operator< stability for std::set< weak_ptr<> >
|
||||||
|
// Thanks to Joe Gottman for pointing this out
|
||||||
|
|
||||||
|
BOOST_TEST(b1 == (wp1 < wp5));
|
||||||
|
BOOST_TEST(b2 == (wp5 < wp1));
|
||||||
|
|
||||||
shared_ptr<int> p6(get_object(), release_object);
|
shared_ptr<int> p6(get_object(), release_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user