1 | // |
---|---|
2 | // shared_ptr_move_test.cpp |
3 | // |
4 | // Copyright (c) 2007 Peter Dimov |
5 | // |
6 | // Distributed under the Boost Software License, Version 1.0. |
7 | // See accompanying file LICENSE_1_0.txt or copy at |
8 | // http://www.boost.org/LICENSE_1_0.txt |
9 | // |
10 | |
11 | #include <boost/shared_ptr.hpp> |
12 | #include <boost/core/lightweight_test.hpp> |
13 | #include <utility> |
14 | |
15 | #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) |
16 | |
17 | struct X |
18 | { |
19 | static long instances; |
20 | |
21 | X() |
22 | { |
23 | ++instances; |
24 | } |
25 | |
26 | ~X() |
27 | { |
28 | --instances; |
29 | } |
30 | |
31 | private: |
32 | |
33 | X( X const & ); |
34 | X & operator=( X const & ); |
35 | }; |
36 | |
37 | long X::instances = 0; |
38 | |
39 | int main() |
40 | { |
41 | BOOST_TEST( X::instances == 0 ); |
42 | |
43 | { |
44 | boost::shared_ptr<X> p( new X ); |
45 | BOOST_TEST( X::instances == 1 ); |
46 | |
47 | boost::shared_ptr<X> p2( std::move( p ) ); |
48 | BOOST_TEST( X::instances == 1 ); |
49 | BOOST_TEST( p.get() == 0 ); |
50 | |
51 | boost::shared_ptr<void> p3( std::move( p2 ) ); |
52 | BOOST_TEST( X::instances == 1 ); |
53 | BOOST_TEST( p2.get() == 0 ); |
54 | |
55 | p3.reset(); |
56 | BOOST_TEST( X::instances == 0 ); |
57 | } |
58 | |
59 | { |
60 | boost::shared_ptr<X> p( new X ); |
61 | BOOST_TEST( X::instances == 1 ); |
62 | |
63 | boost::shared_ptr<X> p2; |
64 | p2 = std::move( p ); |
65 | BOOST_TEST( X::instances == 1 ); |
66 | BOOST_TEST( p.get() == 0 ); |
67 | |
68 | boost::shared_ptr<void> p3; |
69 | p3 = std::move( p2 ); |
70 | BOOST_TEST( X::instances == 1 ); |
71 | BOOST_TEST( p2.get() == 0 ); |
72 | |
73 | p3.reset(); |
74 | BOOST_TEST( X::instances == 0 ); |
75 | } |
76 | |
77 | { |
78 | boost::shared_ptr<X> p( new X ); |
79 | BOOST_TEST( X::instances == 1 ); |
80 | |
81 | boost::shared_ptr<X> p2( new X ); |
82 | BOOST_TEST( X::instances == 2 ); |
83 | p2 = std::move( p ); |
84 | BOOST_TEST( X::instances == 1 ); |
85 | BOOST_TEST( p.get() == 0 ); |
86 | |
87 | boost::shared_ptr<void> p3( new X ); |
88 | BOOST_TEST( X::instances == 2 ); |
89 | p3 = std::move( p2 ); |
90 | BOOST_TEST( X::instances == 1 ); |
91 | BOOST_TEST( p2.get() == 0 ); |
92 | |
93 | p3.reset(); |
94 | BOOST_TEST( X::instances == 0 ); |
95 | } |
96 | |
97 | return boost::report_errors(); |
98 | } |
99 | |
100 | #else // defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) |
101 | |
102 | int main() |
103 | { |
104 | return 0; |
105 | } |
106 | |
107 | #endif |
108 |