Home | History | Annotate | Download | only in util.smartptr.weak.const
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <memory>
     11 
     12 // weak_ptr
     13 
     14 // weak_ptr(const weak_ptr& r);
     15 // weak_ptr(weak_ptr &&r)
     16 
     17 #include <memory>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 struct B
     22 {
     23     static int count;
     24 
     25     B() {++count;}
     26     B(const B&) {++count;}
     27     virtual ~B() {--count;}
     28 };
     29 
     30 int B::count = 0;
     31 
     32 struct A
     33     : public B
     34 {
     35     static int count;
     36 
     37     A() {++count;}
     38     A(const A&) {++count;}
     39     ~A() {--count;}
     40 };
     41 
     42 int A::count = 0;
     43 
     44 struct C
     45 {
     46     static int count;
     47 
     48     C() {++count;}
     49     C(const C&) {++count;}
     50     virtual ~C() {--count;}
     51 };
     52 
     53 int C::count = 0;
     54 
     55 template <class T>
     56 std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); }
     57 
     58 template <class T>
     59 void sink (std::weak_ptr<T> &&) {}
     60 
     61 int main()
     62 {
     63     {
     64         const std::shared_ptr<A> ps(new A);
     65         const std::weak_ptr<A> pA(ps);
     66         assert(pA.use_count() == 1);
     67         assert(B::count == 1);
     68         assert(A::count == 1);
     69         {
     70             std::weak_ptr<A> pB(pA);
     71             assert(B::count == 1);
     72             assert(A::count == 1);
     73             assert(pB.use_count() == 1);
     74             assert(pA.use_count() == 1);
     75         }
     76         assert(pA.use_count() == 1);
     77         assert(B::count == 1);
     78         assert(A::count == 1);
     79     }
     80     assert(B::count == 0);
     81     assert(A::count == 0);
     82     {
     83         std::weak_ptr<A> pA;
     84         assert(pA.use_count() == 0);
     85         assert(B::count == 0);
     86         assert(A::count == 0);
     87         {
     88             std::weak_ptr<A> pB(pA);
     89             assert(B::count == 0);
     90             assert(A::count == 0);
     91             assert(pB.use_count() == 0);
     92             assert(pA.use_count() == 0);
     93         }
     94         assert(pA.use_count() == 0);
     95         assert(B::count == 0);
     96         assert(A::count == 0);
     97     }
     98     assert(B::count == 0);
     99     assert(A::count == 0);
    100 
    101     {
    102         std::shared_ptr<A> ps(new A);
    103         std::weak_ptr<A> pA = source(ps);
    104         assert(pA.use_count() == 1);
    105         assert(A::count == 1);
    106         sink(std::move(pA)); // kill off the weak pointer
    107     }
    108     assert(B::count == 0);
    109     assert(A::count == 0);
    110 }
    111