Commit 8dd5e93a by Jonathan Wakely Committed by Paolo Carlini

shared_ptr.h: Update comparisons to match WP.

2008-11-01  Jonathan Wakely  <jwakely.gcc@gmail.com>

        * include/bits/shared_ptr.h: Update comparisons to match WP.
        (_Sp_counted_ptr): Make copy and assignment members deleted.
        (_Sp_counted_deleter): Remove private copy and assignment members.
        (__shared_count::_M_less,__weak_count::_M_less,operator<): Replace
        friend operator< with overloaded _M_less member functions to allow
        comparison with either shared_count or weak_count.
        (__shared_ptr::_M_less,__weak_ptr::_M_less): Replace with...
        (__shared_ptr::owner_before,__weak_ptr::owner_before): New overloads
        for ownership-based ordering.
        (operator<(__shared_ptr,__shared_ptr)): Compare stored pointers,
        make non-friend.
        (operator==(__shared_ptr,__shared_ptr)): Make non-friend.
        (operator!=(__shared_ptr,__shared_ptr)): Likewise.
        (less<__shared_ptr<>>,less<shared_ptr<>>,_Sp_less): Explicitly call
        pointer specialization.
        (__weak_ptr::operator<,weak_ptr::operator<=,weak_ptr::operator>,
        weak_ptr::operator>=): Remove operator< and delete all comparisons.
        (_Sp_owner_less,owner_less): Predicate for ownership-based ordering.
        (operator<(shared_ptr,shared_ptr): Overload for derived shared_ptr.
        (operator==(shared_ptr,shared_ptr): Likewise.
        (operator!=(shared_ptr,shared_ptr): Likewise.
        (swap(shared_ptr,shared_ptr)): Fix parameter types.
        (swap(weak_ptr,weak_ptr)): Add missing overload.
        * testsuite/20_util/owner_less/cmp.cc: New.
        * testsuite/20_util/shared_ptr/comparison/cmp.cc: Test other ops.
        * testsuite/20_util/shared_ptr/comparison/less.cc: New.
        * testsuite/20_util/shared_ptr/observers/owner_before.cc: New.
        * testsuite/20_util/weak_ptr/observers/owner_before.cc: New.
        * testsuite/20_util/weak_ptr/comparison/cmp_neg.cc: New.

From-SVN: r141512
parent 1b867ae7
2008-11-01 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/bits/shared_ptr.h: Update comparisons to match WP.
(_Sp_counted_ptr): Make copy and assignment members deleted.
(_Sp_counted_deleter): Remove private copy and assignment members.
(__shared_count::_M_less,__weak_count::_M_less,operator<): Replace
friend operator< with overloaded _M_less member functions to allow
comparison with either shared_count or weak_count.
(__shared_ptr::_M_less,__weak_ptr::_M_less): Replace with...
(__shared_ptr::owner_before,__weak_ptr::owner_before): New overloads
for ownership-based ordering.
(operator<(__shared_ptr,__shared_ptr)): Compare stored pointers,
make non-friend.
(operator==(__shared_ptr,__shared_ptr)): Make non-friend.
(operator!=(__shared_ptr,__shared_ptr)): Likewise.
(less<__shared_ptr<>>,less<shared_ptr<>>,_Sp_less): Explicitly call
pointer specialization.
(__weak_ptr::operator<,weak_ptr::operator<=,weak_ptr::operator>,
weak_ptr::operator>=): Remove operator< and delete all comparisons.
(_Sp_owner_less,owner_less): Predicate for ownership-based ordering.
(operator<(shared_ptr,shared_ptr): Overload for derived shared_ptr.
(operator==(shared_ptr,shared_ptr): Likewise.
(operator!=(shared_ptr,shared_ptr): Likewise.
(swap(shared_ptr,shared_ptr)): Fix parameter types.
(swap(weak_ptr,weak_ptr)): Add missing overload.
* testsuite/20_util/owner_less/cmp.cc: New.
* testsuite/20_util/shared_ptr/comparison/cmp.cc: Test other ops.
* testsuite/20_util/shared_ptr/comparison/less.cc: New.
* testsuite/20_util/shared_ptr/observers/owner_before.cc: New.
* testsuite/20_util/weak_ptr/observers/owner_before.cc: New.
* testsuite/20_util/weak_ptr/comparison/cmp_neg.cc: New.
2008-10-31 Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/37958
......
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.8.13.4 Template class owner_less [util.smartptr.ownerless]
#include <memory>
#include <algorithm>
#include <testsuite_hooks.h>
struct A { };
struct B { A a[2]; };
// 20.8.13.4 Template class owner_less [util.smartptr.ownerless]
int
test01()
{
// test empty shared_ptrs compare equivalent
std::owner_less<std::shared_ptr<A>> less;
std::owner_less<std::weak_ptr<A>> wless;
std::shared_ptr<A> p1;
std::shared_ptr<A> p2;
VERIFY( !less(p1, p2) && !less(p2, p1) );
std::weak_ptr<A> p3;
VERIFY( !less(p1, p3) && !less(p3, p1) );
VERIFY( !wless(p1, p3) && !wless(p3, p1) );
return 0;
}
// Construction from pointer
int
test02()
{
std::owner_less<std::shared_ptr<A>> less;
std::owner_less<std::weak_ptr<A>> wless;
std::shared_ptr<A> empty;
std::shared_ptr<A> a1(new A);
VERIFY( less(empty, a1) || less(a1, empty) );
std::shared_ptr<A> a2(new A);
VERIFY( less(a1, a2) || less(a2, a1) );
std::weak_ptr<A> w1(a1);
VERIFY( !less(a1, w1) && !less(w1, a1) );
std::weak_ptr<A> w2(a2);
VERIFY( wless(w1, w2) || wless(w2, w1) );
a1.reset();
VERIFY( !less(empty, a1) && !less(a1, empty) );
VERIFY( less(a1, w1) || less(w1, a1) );
a2.reset();
VERIFY( !less(a2, a1) && !less(a1, a2) );
return 0;
}
// aliasing
int
test03()
{
std::owner_less<std::shared_ptr<A>> less;
std::owner_less<std::weak_ptr<A>> wless;
std::shared_ptr<B> b(new B);
std::shared_ptr<A> a0(b, &b->a[0]);
std::shared_ptr<A> a1(b, &b->a[1]);
// values are different but owners are equivalent:
VERIFY( a0 < a1 && !less(a0, a1) && !less(a1, a0) );
std::weak_ptr<A> w0(a0);
std::weak_ptr<A> w1(a1);
VERIFY( !wless(w0, w1) && !wless(w1, w0) );
VERIFY( !less(a0, w1) && !less(w1, a0) );
VERIFY( !wless(w0, a1) && !wless(a1, w0) );
return 0;
}
// strict weak ordering
int
test04()
{
std::owner_less<std::shared_ptr<A>> less;
std::shared_ptr<A> a[3];
a[0].reset(new A);
a[1].reset(new A);
a[2].reset(new A);
std::sort(a, a+3, less);
VERIFY( !less(a[0], a[0]) );
VERIFY( less(a[0], a[1]) && !less(a[1], a[0]) );
VERIFY( less(a[0], a[1]) && less(a[1], a[2]) && less(a[0], a[2]) );
return 0;
}
int
main()
{
test01();
test02();
test03();
test04();
return 0;
}
......@@ -76,10 +76,24 @@ test02()
return 0;
}
int
test03()
{
std::shared_ptr<A> p1;
// check other operators are defined
VERIFY( p1 <= p1 );
VERIFY( p1 >= p1 );
VERIFY( !(p1 > p1) );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.8.13.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
namespace std
{
template<>
struct less<A*> : binary_function<A*,A*,bool>
{
static int count;
bool operator()(A* l, A* r) { ++count; return l < r; }
};
int less<A*>::count = 0;
}
// 20.8.13.2.7 shared_ptr comparison [util.smartptr.shared.cmp]
int
test01()
{
std::less<std::shared_ptr<A>> less;
// test empty shared_ptrs compare equivalent
std::shared_ptr<A> p1;
std::shared_ptr<A> p2;
VERIFY( !less(p1, p2) && !less(p2, p1) );
VERIFY( std::less<A*>::count == 2 );
return 0;
}
// Construction from pointer
int
test02()
{
std::less<std::shared_ptr<A>> less;
std::shared_ptr<A> empty;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(new A);
VERIFY( less(p1, p2) || less(p2, p1) );
VERIFY( !(less(p1, p2) && less(p2, p1)) );
p1.reset();
VERIFY( !less(p1, empty) && !less(empty, p1) );
p2.reset();
VERIFY( !less(p1, p2) && !less(p2, p1) );
return 0;
}
// Aliasing
int
test03()
{
std::less<std::shared_ptr<A>> less;
A a;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(p1, &a);
VERIFY( less(p1, p2) || less(p2, p1) );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -28,7 +28,7 @@ struct A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// conversion to bool
int
void
test01()
{
bool test __attribute__((unused)) = true;
......@@ -37,11 +37,9 @@ test01()
VERIFY( p1 == false );
const std::shared_ptr<A> p2(p1);
VERIFY( p2 == false );
return 0;
}
int
void
test02()
{
bool test __attribute__((unused)) = true;
......@@ -53,11 +51,9 @@ test02()
p1.reset();
VERIFY( !p1 );
VERIFY( p2 );
return 0;
}
int
void
test03()
{
bool test __attribute__((unused)) = true;
......@@ -67,8 +63,6 @@ test03()
p2.reset(new A);
VERIFY( p1 );
VERIFY( p2 );
return 0;
}
......
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -32,7 +32,7 @@ struct A
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// get
int
void
test01()
{
bool test __attribute__((unused)) = true;
......@@ -40,12 +40,10 @@ test01()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( p.get() == a );
return 0;
}
// operator*
int
void
test02()
{
bool test __attribute__((unused)) = true;
......@@ -53,13 +51,10 @@ test02()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &*p == a );
return 0;
}
// operator->
int
void
test03()
{
bool test __attribute__((unused)) = true;
......@@ -67,11 +62,8 @@ test03()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &p->i == &a->i );
return 0;
}
int
main()
{
......
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.8.13.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A
{
int i;
virtual ~A() { }
};
struct B : A
{
};
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
void
test01()
{
bool test __attribute__((unused)) = true;
// test empty shared_ptrs compare equivalent
std::shared_ptr<A> p1;
std::shared_ptr<B> p2;
VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) );
}
// Construction from pointer
void
test02()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a0;
std::shared_ptr<A> a1(new A);
VERIFY( a1.owner_before(a0) || a0.owner_before(a1) );
VERIFY( !(a1.owner_before(a0) && a0.owner_before(a1)) );
std::shared_ptr<B> b1(new B);
VERIFY( a1.owner_before(b1) || b1.owner_before(a1) );
VERIFY( !(a1.owner_before(b1) && b1.owner_before(a1)) );
std::shared_ptr<A> a2(a1);
VERIFY( !a1.owner_before(a2) && !a2.owner_before(a1) );
a2 = b1;
VERIFY( !b1.owner_before(a2) && !a2.owner_before(b1) );
std::weak_ptr<A> w1(a1);
VERIFY( !a1.owner_before(w1) && !w1.owner_before(a1) );
std::weak_ptr<A> w2(a2);
VERIFY( !b1.owner_before(w2) && !w2.owner_before(b1) );
}
// Aliasing
void
test03()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A());
std::shared_ptr<int> p2(p1, &p1->i);
VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) );
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -28,7 +28,7 @@ struct A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// unique
int
void
test01()
{
bool test __attribute__((unused)) = true;
......@@ -37,11 +37,9 @@ test01()
VERIFY( !p1.unique() );
const std::shared_ptr<A> p2(p1);
VERIFY( !p1.unique() );
return 0;
}
int
void
test02()
{
bool test __attribute__((unused)) = true;
......@@ -53,11 +51,9 @@ test02()
p1.reset();
VERIFY( !p1.unique() );
VERIFY( p2.unique() );
return 0;
}
int
void
test03()
{
bool test __attribute__((unused)) = true;
......@@ -67,8 +63,6 @@ test03()
p2.reset(new A);
VERIFY( p1.unique() );
VERIFY( p2.unique() );
return 0;
}
......
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -29,7 +29,7 @@ struct B : A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// use_count
int
void
test01()
{
bool test __attribute__((unused)) = true;
......@@ -38,11 +38,9 @@ test01()
VERIFY( p1.use_count() == 0 );
const std::shared_ptr<A> p2(p1);
VERIFY( p1.use_count() == 0 );
return 0;
}
int
void
test02()
{
bool test __attribute__((unused)) = true;
......@@ -52,11 +50,9 @@ test02()
p1.reset();
VERIFY( p1.use_count() == 0 );
VERIFY( p2.use_count() == 1 );
return 0;
}
int
void
test03()
{
bool test __attribute__((unused)) = true;
......@@ -66,8 +62,6 @@ test03()
p2.reset(new B);
VERIFY( p1.use_count() == 1 );
VERIFY( p2.use_count() == 1 );
return 0;
}
......
// { dg-options "-std=gnu++0x " }
// { dg-do compile }
// Copyright (C) 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.3 Template class weak_ptr [util.smartptr.weak]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.8.13.3.6 weak_ptr comparison [util.smartptr.weak.cmp] (removed)
int
test01()
{
std::weak_ptr<A> p1;
// { dg-excess-errors "deleted function" }
p1 < p1; // { dg-error "used here" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2008 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.8.13.3 Template class weak_ptr [util.smartptr.weak]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B { };
// 20.6.6.3.5 weak_ptr observers [util.smartptr.weak.obs]
int
test01()
{
// test empty weak_ptrs compare equivalent
std::weak_ptr<A> p1;
std::weak_ptr<B> p2;
VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) );
std::shared_ptr<B> p3;
VERIFY( !p1.owner_before(p3) && !p3.owner_before(p1) );
return 0;
}
int
test02()
{
std::shared_ptr<A> a0;
std::weak_ptr<A> w0(a0);
std::shared_ptr<A> a1(new A);
std::weak_ptr<A> w1(a1);
VERIFY( !a1.owner_before(w1) && !w1.owner_before(a1) );
VERIFY( w1.owner_before(w0) || w0.owner_before(w1) );
VERIFY( !(w1.owner_before(w0) && w0.owner_before(w1)) );
VERIFY( w1.owner_before(a0) || a0.owner_before(w1) );
VERIFY( !(w1.owner_before(a0) && a0.owner_before(w1)) );
std::shared_ptr<B> b1(new B);
VERIFY( w1.owner_before(b1) || b1.owner_before(w1) );
return 0;
}
int
main()
{
test01();
test02();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment