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 D, class T> D* get_deleter(const shared_ptr<T>& p); |
14 | |
15 | // UNSUPPORTED: no-rtti |
16 | |
17 | #include <memory> |
18 | #include <cassert> |
19 | #include "test_macros.h" |
20 | #include "deleter_types.h" |
21 | |
22 | struct A |
23 | { |
24 | static int count; |
25 | |
26 | A() {++count;} |
27 | A(const A&) {++count;} |
28 | ~A() {--count;} |
29 | }; |
30 | |
31 | int A::count = 0; |
32 | |
33 | int main(int, char**) |
34 | { |
35 | { |
36 | { |
37 | A* ptr = new A; |
38 | std::shared_ptr<A> p(ptr, test_deleter<A>(3)); |
39 | test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); |
40 | assert(test_deleter<A>::count == 1); |
41 | assert(test_deleter<A>::dealloc_count == 0); |
42 | assert(d); |
43 | assert(d->state() == 3); |
44 | } |
45 | assert(A::count == 0); |
46 | assert(test_deleter<A>::count == 0); |
47 | assert(test_deleter<A>::dealloc_count == 1); |
48 | } |
49 | test_deleter<A>::dealloc_count = 0; |
50 | { |
51 | { |
52 | std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); |
53 | test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); |
54 | assert(test_deleter<A>::count == 1); |
55 | assert(test_deleter<A>::dealloc_count == 0); |
56 | assert(d); |
57 | assert(d->state() == 3); |
58 | } |
59 | assert(A::count == 0); |
60 | assert(test_deleter<A>::count == 0); |
61 | assert(test_deleter<A>::dealloc_count == 1); |
62 | } |
63 | test_deleter<A>::dealloc_count = 0; |
64 | { |
65 | std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); |
66 | std::default_delete<A>* d = std::get_deleter<std::default_delete<A> >(p: p); |
67 | assert(d == 0); |
68 | } |
69 | |
70 | return 0; |
71 | } |
72 | |