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 T, class U> shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept; |
14 | // template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept; |
15 | |
16 | // UNSUPPORTED: no-rtti |
17 | |
18 | #include <cassert> |
19 | #include <memory> |
20 | #include <type_traits> |
21 | #include <utility> |
22 | |
23 | #include "test_macros.h" |
24 | |
25 | struct B |
26 | { |
27 | static int count; |
28 | |
29 | B() {++count;} |
30 | B(const B&) {++count;} |
31 | virtual ~B() {--count;} |
32 | }; |
33 | |
34 | int B::count = 0; |
35 | |
36 | struct A |
37 | : public B |
38 | { |
39 | static int count; |
40 | |
41 | A() {++count;} |
42 | A(const A& other) : B(other) {++count;} |
43 | ~A() {--count;} |
44 | }; |
45 | |
46 | int A::count = 0; |
47 | |
48 | int main(int, char**) |
49 | { |
50 | { |
51 | const std::shared_ptr<B> pB(new A); |
52 | ASSERT_NOEXCEPT(std::dynamic_pointer_cast<A>(r: pB)); |
53 | std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(r: pB); |
54 | assert(pA.get() == pB.get()); |
55 | assert(!pB.owner_before(pA) && !pA.owner_before(pB)); |
56 | } |
57 | { |
58 | const std::shared_ptr<B> pB(new B); |
59 | std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(r: pB); |
60 | assert(pA.get() == 0); |
61 | assert(pA.use_count() == 0); |
62 | } |
63 | #if TEST_STD_VER > 14 |
64 | { |
65 | const std::shared_ptr<B[8]> pB(new B[8]); |
66 | std::shared_ptr<A[8]> pA = std::dynamic_pointer_cast<A[8]>(pB); |
67 | assert(pA.get() == 0); |
68 | assert(pA.use_count() == 0); |
69 | } |
70 | #endif // TEST_STD_VER > 14 |
71 | #if TEST_STD_VER > 20 |
72 | { |
73 | A* pA_raw = new A; |
74 | std::shared_ptr<B> pB(pA_raw); |
75 | ASSERT_NOEXCEPT(std::dynamic_pointer_cast<A>(std::move(pB))); |
76 | std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(std::move(pB)); |
77 | assert(pB.get() == nullptr); |
78 | assert(pA.get() == pA_raw); |
79 | assert(pA.use_count() == 1); |
80 | } |
81 | #endif // TEST_STD_VER > 20 |
82 | |
83 | return 0; |
84 | } |
85 | |