Ticket #942: introduce move semantics for our custom shared-ptr-wrapper lib::P
This commit is contained in:
parent
f28ad3cf74
commit
2e9467fe76
2 changed files with 33 additions and 8 deletions
|
|
@ -81,14 +81,16 @@ namespace lib {
|
||||||
template<class Y, class D> P (Y* p, D d) : BASE(p,d){}
|
template<class Y, class D> P (Y* p, D d) : BASE(p,d){}
|
||||||
|
|
||||||
P (P const& r) : BASE(r) {}
|
P (P const& r) : BASE(r) {}
|
||||||
|
P (P const&& rr) : BASE(rr) {}
|
||||||
template<class Y> P (shared_ptr<Y> const& r) : BASE(r) {}
|
template<class Y> P (shared_ptr<Y> const& r) : BASE(r) {}
|
||||||
template<class Y> explicit P (weak_ptr<Y> const& wr) : BASE(wr) {}
|
template<class Y> explicit P (weak_ptr<Y> const& wr) : BASE(wr) {}
|
||||||
template<class Y> explicit P (std::auto_ptr<Y> & ar) : BASE(ar) {}
|
template<class Y> explicit P (std::auto_ptr<Y> && ar) : BASE(std::move(ar)) {}
|
||||||
|
|
||||||
|
|
||||||
P& operator= (P const& r) { BASE::operator= (r); return *this; }
|
P& operator= (P const& r) { BASE::operator= (r); return *this; }
|
||||||
|
P& operator= (P const&& rr) { BASE::operator= (rr); return *this; }
|
||||||
template<class Y> P& operator=(shared_ptr<Y> const& sr) { BASE::operator= (sr); return *this; }
|
template<class Y> P& operator=(shared_ptr<Y> const& sr) { BASE::operator= (sr); return *this; }
|
||||||
template<class Y> P& operator=(std::auto_ptr<Y> & ar) { BASE::operator= (ar); return *this; }
|
template<class Y> P& operator=(std::auto_ptr<Y> && ar) { BASE::operator= (std::move(ar)); return *this; }
|
||||||
|
|
||||||
TAR* get() const { return dynamic_cast<TAR*> (BASE::get()); }
|
TAR* get() const { return dynamic_cast<TAR*> (BASE::get()); }
|
||||||
TAR& operator*() const { return *get(); }
|
TAR& operator*() const { return *get(); }
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,7 @@ namespace test{
|
||||||
{
|
{
|
||||||
check_refcounting ();
|
check_refcounting ();
|
||||||
check_shared_ownership ();
|
check_shared_ownership ();
|
||||||
|
check_ownership_transfer ();
|
||||||
check_type_relations ();
|
check_type_relations ();
|
||||||
check_ordering ();
|
check_ordering ();
|
||||||
}
|
}
|
||||||
|
|
@ -116,14 +117,9 @@ namespace test{
|
||||||
void
|
void
|
||||||
check_shared_ownership ()
|
check_shared_ownership ()
|
||||||
{
|
{
|
||||||
std::auto_ptr<X> au (new X(22));
|
P<X> pX (new X(22));
|
||||||
CHECK (au.get());
|
|
||||||
|
|
||||||
P<X> pX (au);
|
|
||||||
CHECK (!au.get());
|
|
||||||
CHECK (pX);
|
CHECK (pX);
|
||||||
CHECK (1 == pX.use_count());
|
CHECK (1 == pX.use_count());
|
||||||
CHECK (22 == pX->x_);
|
|
||||||
|
|
||||||
weak_ptr<X> wX (pX);
|
weak_ptr<X> wX (pX);
|
||||||
CHECK (wX.lock());
|
CHECK (wX.lock());
|
||||||
|
|
@ -167,6 +163,33 @@ namespace test{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
check_ownership_transfer ()
|
||||||
|
{
|
||||||
|
std::auto_ptr<X> au (new X(23));
|
||||||
|
CHECK (au.get());
|
||||||
|
|
||||||
|
P<X> pX (std::move(au));
|
||||||
|
CHECK (!au.get());
|
||||||
|
CHECK (pX);
|
||||||
|
CHECK (1 == pX.use_count());
|
||||||
|
CHECK (23 == pX->x_);
|
||||||
|
|
||||||
|
au.reset (new X(21));
|
||||||
|
CHECK (au.get());
|
||||||
|
|
||||||
|
pX.reset();
|
||||||
|
CHECK (!pX);
|
||||||
|
CHECK (0 == pX.use_count());
|
||||||
|
|
||||||
|
pX = std::move(au);
|
||||||
|
CHECK (!au.get());
|
||||||
|
CHECK (pX);
|
||||||
|
CHECK (1 == pX.use_count());
|
||||||
|
CHECK (21 == pX->x_);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @test building type relationships on smart-ptrs */
|
/** @test building type relationships on smart-ptrs */
|
||||||
void
|
void
|
||||||
check_type_relations ()
|
check_type_relations ()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue