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 | // shared_ptr |
12 | |
13 | // template<class Y, class D> void reset(Y* p, D d); |
14 | |
15 | #include <cassert> |
16 | #include <memory> |
17 | #include "deleter_types.h" |
18 | #include "reset_helper.h" |
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 bad_ty { }; |
45 | struct bad_deleter |
46 | { |
47 | void operator()(bad_ty) { } |
48 | }; |
49 | |
50 | struct Base { }; |
51 | struct Derived : Base { }; |
52 | |
53 | static_assert( HasReset<std::shared_ptr<int>, int*, test_deleter<int> >::value, "" ); |
54 | static_assert(!HasReset<std::shared_ptr<int>, int*, bad_deleter>::value, "" ); |
55 | static_assert( HasReset<std::shared_ptr<Base>, Derived*, test_deleter<Base> >::value, "" ); |
56 | static_assert(!HasReset<std::shared_ptr<A>, int*, test_deleter<A> >::value, "" ); |
57 | |
58 | #if TEST_STD_VER >= 17 |
59 | static_assert( HasReset<std::shared_ptr<int[]>, int*, test_deleter<int>>::value, "" ); |
60 | static_assert(!HasReset<std::shared_ptr<int[]>, int*, bad_deleter>::value, "" ); |
61 | static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[], test_deleter<int>>::value, "" ); |
62 | static_assert( HasReset<std::shared_ptr<int[5]>, int*, test_deleter<int>>::value, "" ); |
63 | static_assert(!HasReset<std::shared_ptr<int[5]>, int*, bad_deleter>::value, "" ); |
64 | static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>>::value, "" ); |
65 | #endif |
66 | |
67 | int main(int, char**) |
68 | { |
69 | { |
70 | std::shared_ptr<B> p(new B); |
71 | A* ptr = new A; |
72 | p.reset(ptr, test_deleter<A>(3)); |
73 | assert(A::count == 1); |
74 | assert(B::count == 1); |
75 | assert(p.use_count() == 1); |
76 | assert(p.get() == ptr); |
77 | assert(test_deleter<A>::count == 1); |
78 | assert(test_deleter<A>::dealloc_count == 0); |
79 | #ifndef TEST_HAS_NO_RTTI |
80 | test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); |
81 | assert(d); |
82 | assert(d->state() == 3); |
83 | #endif |
84 | } |
85 | assert(A::count == 0); |
86 | assert(test_deleter<A>::count == 0); |
87 | assert(test_deleter<A>::dealloc_count == 1); |
88 | { |
89 | std::shared_ptr<B> p; |
90 | A* ptr = new A; |
91 | p.reset(ptr, test_deleter<A>(3)); |
92 | assert(A::count == 1); |
93 | assert(B::count == 1); |
94 | assert(p.use_count() == 1); |
95 | assert(p.get() == ptr); |
96 | assert(test_deleter<A>::count == 1); |
97 | assert(test_deleter<A>::dealloc_count == 1); |
98 | #ifndef TEST_HAS_NO_RTTI |
99 | test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); |
100 | assert(d); |
101 | assert(d->state() == 3); |
102 | #endif |
103 | } |
104 | assert(A::count == 0); |
105 | assert(test_deleter<A>::count == 0); |
106 | assert(test_deleter<A>::dealloc_count == 2); |
107 | |
108 | #if TEST_STD_VER > 14 |
109 | { |
110 | std::shared_ptr<const A[]> p; |
111 | A* ptr = new A[8]; |
112 | p.reset(ptr, CDeleter<A[]>()); |
113 | assert(A::count == 8); |
114 | assert(p.use_count() == 1); |
115 | assert(p.get() == ptr); |
116 | } |
117 | assert(A::count == 0); |
118 | #endif |
119 | |
120 | return 0; |
121 | } |
122 | |