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 | // template<class Y> weak_ptr(const shared_ptr<Y>& r); |
14 | |
15 | #include <memory> |
16 | #include <type_traits> |
17 | #include <cassert> |
18 | |
19 | #include "test_macros.h" |
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& other) : B(other) {++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 | int main(int, char**) |
56 | { |
57 | static_assert(( std::is_convertible<std::shared_ptr<A>, std::weak_ptr<B> >::value), ""); |
58 | static_assert((!std::is_convertible<std::weak_ptr<B>, std::shared_ptr<A> >::value), ""); |
59 | static_assert((!std::is_convertible<std::shared_ptr<A>, std::weak_ptr<C> >::value), ""); |
60 | { |
61 | const std::shared_ptr<A> pA(new A); |
62 | assert(pA.use_count() == 1); |
63 | assert(B::count == 1); |
64 | assert(A::count == 1); |
65 | { |
66 | std::weak_ptr<B> pB(pA); |
67 | assert(B::count == 1); |
68 | assert(A::count == 1); |
69 | assert(pB.use_count() == 1); |
70 | assert(pA.use_count() == 1); |
71 | } |
72 | assert(pA.use_count() == 1); |
73 | assert(B::count == 1); |
74 | assert(A::count == 1); |
75 | } |
76 | assert(B::count == 0); |
77 | assert(A::count == 0); |
78 | { |
79 | std::shared_ptr<A> pA; |
80 | assert(pA.use_count() == 0); |
81 | assert(B::count == 0); |
82 | assert(A::count == 0); |
83 | { |
84 | std::weak_ptr<B> pB(pA); |
85 | assert(B::count == 0); |
86 | assert(A::count == 0); |
87 | assert(pB.use_count() == 0); |
88 | assert(pA.use_count() == 0); |
89 | } |
90 | assert(pA.use_count() == 0); |
91 | assert(B::count == 0); |
92 | assert(A::count == 0); |
93 | } |
94 | assert(B::count == 0); |
95 | assert(A::count == 0); |
96 | |
97 | #if TEST_STD_VER > 14 |
98 | { |
99 | std::shared_ptr<A[]> p1(new A[8]); |
100 | assert(p1.use_count() == 1); |
101 | assert(A::count == 8); |
102 | { |
103 | std::weak_ptr<const A[]> p2(p1); |
104 | assert(A::count == 8); |
105 | assert(p2.use_count() == 1); |
106 | assert(p1.use_count() == 1); |
107 | } |
108 | assert(p1.use_count() == 1); |
109 | assert(A::count == 8); |
110 | } |
111 | assert(A::count == 0); |
112 | #endif |
113 | |
114 | return 0; |
115 | } |
116 |