weak_ptr made thread safe, shared->weak conversions, lightweight_mutex added.

[SVN r12786]
This commit is contained in:
Peter Dimov
2002-02-12 16:55:25 +00:00
parent cd8dea78e6
commit 1b69c14f45
8 changed files with 429 additions and 43 deletions

View File

@@ -96,18 +96,28 @@ void release_object(int * p)
std::cout << "release_object()\n";
}
template<class T> void test_is_X(T const & p)
template<class T> void test_is_X(boost::shared_ptr<T> const & p)
{
BOOST_TEST(p->id() == 1);
BOOST_TEST((*p).id() == 1);
}
template<class T> void test_is_Y(T const & p)
template<class T> void test_is_X(boost::weak_ptr<T> const & p)
{
BOOST_TEST(p->id() == 1);
}
template<class T> void test_is_Y(boost::shared_ptr<T> const & p)
{
BOOST_TEST(p->id() == 2);
BOOST_TEST((*p).id() == 2);
}
template<class T> void test_is_Y(boost::weak_ptr<T> const & p)
{
BOOST_TEST(p->id() == 2);
}
template<class T> void test_eq(T const & a, T const & b)
{
BOOST_TEST(a == b);
@@ -136,6 +146,30 @@ template<class T, class U> void test_ne2(T const & a, U const & b)
BOOST_TEST(a != b);
}
template<class T> void test_is_zero(boost::shared_ptr<T> const & p)
{
BOOST_TEST(!p);
BOOST_TEST(p.get() == 0);
}
template<class T> void test_is_zero(boost::weak_ptr<T> const & p)
{
BOOST_TEST(!p);
test_is_zero(p.get());
}
template<class T> void test_is_nonzero(boost::shared_ptr<T> const & p)
{
BOOST_TEST(p);
BOOST_TEST(p.get() != 0);
}
template<class T> void test_is_nonzero(boost::weak_ptr<T> const & p)
{
BOOST_TEST(p);
test_is_nonzero(p.get());
}
int test_main(int, char * [])
{
using namespace boost;
@@ -144,6 +178,8 @@ int test_main(int, char * [])
shared_ptr<X> p(new Y);
shared_ptr<X> p2(new X);
test_is_nonzero(p);
test_is_nonzero(p2);
test_is_Y(p);
test_is_X(p2);
test_ne(p, p2);
@@ -156,6 +192,9 @@ int test_main(int, char * [])
shared_ptr<Y> p3 = shared_dynamic_cast<Y>(p);
shared_ptr<Y> p4 = shared_dynamic_cast<Y>(p2);
test_is_nonzero(p3);
test_is_zero(p4);
BOOST_TEST(p.use_count() == 2);
BOOST_TEST(p2.use_count() == 1);
BOOST_TEST(p3.use_count() == 2);
@@ -167,6 +206,7 @@ int test_main(int, char * [])
shared_ptr<void> p5(p);
test_is_nonzero(p5);
test_eq2(p, p5);
std::cout << "--\n";
@@ -176,6 +216,11 @@ int test_main(int, char * [])
p3.reset();
p4.reset();
test_is_zero(p);
test_is_zero(p2);
test_is_zero(p3);
test_is_zero(p4);
std::cout << "--\n";
BOOST_TEST(p5.use_count() == 1);
@@ -183,25 +228,24 @@ int test_main(int, char * [])
weak_ptr<X> wp1;
BOOST_TEST(wp1.use_count() == 0);
BOOST_TEST(wp1.get() == 0);
test_is_zero(wp1);
weak_ptr<X> wp2 = shared_static_cast<X>(p5);
BOOST_TEST(wp2.use_count() == 1);
BOOST_TEST(wp2.get() != 0);
test_is_nonzero(wp2);
test_is_Y(wp2);
test_ne(wp1, wp2);
weak_ptr<Y> wp3 = shared_dynamic_cast<Y>(wp2);
BOOST_TEST(wp3.use_count() == 1);
BOOST_TEST(wp3.get() != 0);
test_is_nonzero(wp3);
test_eq2(wp2, wp3);
weak_ptr<X> wp4(wp3);
test_is_nonzero(wp4);
test_eq(wp2, wp4);
wp1 = p2;
@@ -210,8 +254,7 @@ int test_main(int, char * [])
wp1 = wp2;
BOOST_TEST(wp1.use_count() == 1);
BOOST_TEST(wp1.get() != 0);
test_is_nonzero(wp1);
test_eq(wp1, wp2);
weak_ptr<X> wp5;
@@ -220,15 +263,16 @@ int test_main(int, char * [])
bool b2 = wp5 < wp1;
p5.reset();
test_is_zero(wp5);
BOOST_TEST(wp1.use_count() == 0);
BOOST_TEST(wp1.get() == 0);
test_is_zero(wp1);
BOOST_TEST(wp2.use_count() == 0);
BOOST_TEST(wp2.get() == 0);
test_is_zero(wp2);
BOOST_TEST(wp3.use_count() == 0);
BOOST_TEST(wp3.get() == 0);
test_is_zero(wp3);
// Test operator< stability for std::set< weak_ptr<> >
// Thanks to Joe Gottman for pointing this out