1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <memory>
10 
11 // weak_ptr
12 
13 // weak_ptr(const weak_ptr& r);
14 // weak_ptr(weak_ptr &&r)
15 
16 #include <memory>
17 #include <type_traits>
18 #include <cassert>
19 #include <utility>
20 
21 #include "test_macros.h"
22 
23 struct B
24 {
25     static int count;
26 
BB27     B() {++count;}
BB28     B(const B&) {++count;}
~BB29     virtual ~B() {--count;}
30 };
31 
32 int B::count = 0;
33 
34 struct A
35     : public B
36 {
37     static int count;
38 
AA39     A() {++count;}
AA40     A(const A& other) : B(other) {++count;}
~AA41     ~A() {--count;}
42 };
43 
44 int A::count = 0;
45 
46 struct C
47 {
48     static int count;
49 
CC50     C() {++count;}
CC51     C(const C&) {++count;}
~CC52     virtual ~C() {--count;}
53 };
54 
55 int C::count = 0;
56 
57 template <class T>
source(std::shared_ptr<T> p)58 std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); }
59 
60 #if TEST_STD_VER >= 11
61 template <class T>
sink(std::weak_ptr<T> &&)62 void sink (std::weak_ptr<T> &&) {}
63 #endif
64 
main(int,char **)65 int main(int, char**)
66 {
67     {
68         const std::shared_ptr<A> ps(new A);
69         const std::weak_ptr<A> pA(ps);
70         assert(pA.use_count() == 1);
71         assert(B::count == 1);
72         assert(A::count == 1);
73         {
74             std::weak_ptr<A> pB(pA);
75             assert(B::count == 1);
76             assert(A::count == 1);
77             assert(pB.use_count() == 1);
78             assert(pA.use_count() == 1);
79         }
80         assert(pA.use_count() == 1);
81         assert(B::count == 1);
82         assert(A::count == 1);
83     }
84     assert(B::count == 0);
85     assert(A::count == 0);
86     {
87         std::weak_ptr<A> pA;
88         assert(pA.use_count() == 0);
89         assert(B::count == 0);
90         assert(A::count == 0);
91         {
92             std::weak_ptr<A> pB(pA);
93             assert(B::count == 0);
94             assert(A::count == 0);
95             assert(pB.use_count() == 0);
96             assert(pA.use_count() == 0);
97         }
98         assert(pA.use_count() == 0);
99         assert(B::count == 0);
100         assert(A::count == 0);
101     }
102     assert(B::count == 0);
103     assert(A::count == 0);
104 
105 #if TEST_STD_VER >= 11
106     {
107         std::shared_ptr<A> ps(new A);
108         std::weak_ptr<A> pA = source(ps);
109         assert(pA.use_count() == 1);
110         assert(A::count == 1);
111         sink(std::move(pA)); // kill off the weak pointer
112     }
113     assert(B::count == 0);
114     assert(A::count == 0);
115 #endif
116 
117   return 0;
118 }
119