| 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> void reset(Y* p); |
| 14 | |
| 15 | #include <memory> |
| 16 | #include <cassert> |
| 17 | #include <utility> |
| 18 | |
| 19 | #include "reset_helper.h" |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | struct B |
| 23 | { |
| 24 | static int count; |
| 25 | |
| 26 | B() {++count;} |
| 27 | B(const B&) {++count;} |
| 28 | virtual ~B() {--count;} |
| 29 | }; |
| 30 | |
| 31 | int B::count = 0; |
| 32 | |
| 33 | struct A |
| 34 | : public B |
| 35 | { |
| 36 | static int count; |
| 37 | |
| 38 | A() {++count;} |
| 39 | A(const A& other) : B(other) {++count;} |
| 40 | ~A() {--count;} |
| 41 | }; |
| 42 | |
| 43 | int A::count = 0; |
| 44 | |
| 45 | struct Derived : A {}; |
| 46 | |
| 47 | static_assert( HasReset<std::shared_ptr<int>, int*>::value, "" ); |
| 48 | static_assert( HasReset<std::shared_ptr<A>, Derived*>::value, "" ); |
| 49 | static_assert(!HasReset<std::shared_ptr<A>, int*>::value, "" ); |
| 50 | |
| 51 | #if TEST_STD_VER >= 17 |
| 52 | static_assert( HasReset<std::shared_ptr<int[]>, int*>::value, "" ); |
| 53 | static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[]>::value, "" ); |
| 54 | static_assert( HasReset<std::shared_ptr<int[5]>, int*>::value, "" ); |
| 55 | static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5]>::value, "" ); |
| 56 | #endif |
| 57 | |
| 58 | int main(int, char**) |
| 59 | { |
| 60 | { |
| 61 | std::shared_ptr<B> p(new B); |
| 62 | A* ptr = new A; |
| 63 | p.reset(p: ptr); |
| 64 | assert(A::count == 1); |
| 65 | assert(B::count == 1); |
| 66 | assert(p.use_count() == 1); |
| 67 | assert(p.get() == ptr); |
| 68 | } |
| 69 | assert(A::count == 0); |
| 70 | { |
| 71 | std::shared_ptr<B> p; |
| 72 | A* ptr = new A; |
| 73 | p.reset(p: ptr); |
| 74 | assert(A::count == 1); |
| 75 | assert(B::count == 1); |
| 76 | assert(p.use_count() == 1); |
| 77 | assert(p.get() == ptr); |
| 78 | } |
| 79 | assert(A::count == 0); |
| 80 | |
| 81 | #if TEST_STD_VER > 14 |
| 82 | { |
| 83 | std::shared_ptr<const A[]> p; |
| 84 | A* ptr = new A[8]; |
| 85 | p.reset(ptr); |
| 86 | assert(A::count == 8); |
| 87 | assert(p.use_count() == 1); |
| 88 | assert(p.get() == ptr); |
| 89 | } |
| 90 | assert(A::count == 0); |
| 91 | #endif |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |