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
21struct B
22{
23 static int count;
24
25 B() {++count;}
26 B(const B&) {++count;}
27 virtual ~B() {--count;}
28};
29
30int B::count = 0;
31
32struct 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
42int A::count = 0;
43
44struct bad_ty { };
45struct bad_deleter
46{
47 void operator()(bad_ty) { }
48};
49
50struct Base { };
51struct Derived : Base { };
52
53static_assert( HasReset<std::shared_ptr<int>, int*, test_deleter<int> >::value, "");
54static_assert(!HasReset<std::shared_ptr<int>, int*, bad_deleter>::value, "");
55static_assert( HasReset<std::shared_ptr<Base>, Derived*, test_deleter<Base> >::value, "");
56static_assert(!HasReset<std::shared_ptr<A>, int*, test_deleter<A> >::value, "");
57
58#if TEST_STD_VER >= 17
59static_assert( HasReset<std::shared_ptr<int[]>, int*, test_deleter<int>>::value, "");
60static_assert(!HasReset<std::shared_ptr<int[]>, int*, bad_deleter>::value, "");
61static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[], test_deleter<int>>::value, "");
62static_assert( HasReset<std::shared_ptr<int[5]>, int*, test_deleter<int>>::value, "");
63static_assert(!HasReset<std::shared_ptr<int[5]>, int*, bad_deleter>::value, "");
64static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>>::value, "");
65#endif
66
67int 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

source code of libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp