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 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     59 template <class T>
     60 void sink (std::weak_ptr<T> &&) {}
     61 #endif
     62 
     63 int main()
     64 {
     65     {
     66         const std::shared_ptr<A> ps(new A);
     67         const std::weak_ptr<A> pA(ps);
     68         assert(pA.use_count() == 1);
     69         assert(B::count == 1);
     70         assert(A::count == 1);
     71         {
     72             std::weak_ptr<A> pB(pA);
     73             assert(B::count == 1);
     74             assert(A::count == 1);
     75             assert(pB.use_count() == 1);
     76             assert(pA.use_count() == 1);
     77         }
     78         assert(pA.use_count() == 1);
     79         assert(B::count == 1);
     80         assert(A::count == 1);
     81     }
     82     assert(B::count == 0);
     83     assert(A::count == 0);
     84     {
     85         std::weak_ptr<A> pA;
     86         assert(pA.use_count() == 0);
     87         assert(B::count == 0);
     88         assert(A::count == 0);
     89         {
     90             std::weak_ptr<A> pB(pA);
     91             assert(B::count == 0);
     92             assert(A::count == 0);
     93             assert(pB.use_count() == 0);
     94             assert(pA.use_count() == 0);
     95         }
     96         assert(pA.use_count() == 0);
     97         assert(B::count == 0);
     98         assert(A::count == 0);
     99     }
    100     assert(B::count == 0);
    101     assert(A::count == 0);
    102 
    103 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    104     {
    105         std::shared_ptr<A> ps(new A);
    106         std::weak_ptr<A> pA = source(ps);
    107         assert(pA.use_count() == 1);
    108         assert(A::count == 1);
    109         sink(std::move(pA)); // kill off the weak pointer
    110     }
    111     assert(B::count == 0);
    112     assert(A::count == 0);
    113 #endif
    114 }
    115