1 | #include <boost/config.hpp> |
---|---|
2 | |
3 | // shared_ptr_alloc3_test.cpp |
4 | // |
5 | // Copyright (c) 2005, 2014 Peter Dimov |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. |
8 | // See accompanying file LICENSE_1_0.txt or copy at |
9 | // http://www.boost.org/LICENSE_1_0.txt |
10 | |
11 | |
12 | #include <boost/core/lightweight_test.hpp> |
13 | #include <boost/shared_ptr.hpp> |
14 | #include <memory> |
15 | #include <cstddef> |
16 | |
17 | // |
18 | |
19 | struct D; |
20 | |
21 | struct X |
22 | { |
23 | static int instances; |
24 | |
25 | X(): deleted_( false ) |
26 | { |
27 | ++instances; |
28 | } |
29 | |
30 | ~X() |
31 | { |
32 | BOOST_TEST( deleted_ ); |
33 | --instances; |
34 | } |
35 | |
36 | private: |
37 | |
38 | friend struct D; |
39 | |
40 | bool deleted_; |
41 | |
42 | X( X const & ); |
43 | X & operator=( X const & ); |
44 | }; |
45 | |
46 | int X::instances = 0; |
47 | |
48 | struct D |
49 | { |
50 | void operator()( X * px ) const |
51 | { |
52 | px->deleted_ = true; |
53 | delete px; |
54 | } |
55 | }; |
56 | |
57 | int main() |
58 | { |
59 | BOOST_TEST( X::instances == 0 ); |
60 | |
61 | boost::shared_ptr<void> pv( new X, D(), std::allocator<X>() ); |
62 | |
63 | BOOST_TEST( X::instances == 1 ); |
64 | |
65 | pv.reset(); |
66 | |
67 | BOOST_TEST( X::instances == 0 ); |
68 | |
69 | pv.reset( p: new X, d: D(), a: std::allocator<void>() ); |
70 | |
71 | BOOST_TEST( X::instances == 1 ); |
72 | |
73 | pv.reset(); |
74 | |
75 | BOOST_TEST( X::instances == 0 ); |
76 | |
77 | return boost::report_errors(); |
78 | } |
79 |