1 | // |
---|---|
2 | // shared_ptr_rv_pointer_cast_test.cpp |
3 | // |
4 | // Copyright (c) 2016 Chris Glover |
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 | |
14 | #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) |
15 | |
16 | struct X |
17 | {}; |
18 | |
19 | struct Y: public X |
20 | {}; |
21 | |
22 | struct U |
23 | { |
24 | virtual ~U() {} |
25 | }; |
26 | |
27 | struct V: public U |
28 | {}; |
29 | |
30 | struct W : public U |
31 | {}; |
32 | |
33 | int main() |
34 | { |
35 | { |
36 | boost::shared_ptr<X> px(new Y); |
37 | |
38 | boost::shared_ptr<Y> py1 = boost::static_pointer_cast<Y>(r: px); |
39 | boost::shared_ptr<Y> py2 = boost::static_pointer_cast<Y>(r: std::move(px)); |
40 | BOOST_TEST(!px); |
41 | BOOST_TEST(px.use_count() == 0); |
42 | BOOST_TEST(py1.get() == py2.get()); |
43 | BOOST_TEST(!(py1 < py2 || py2 < py1)); |
44 | BOOST_TEST(py1.use_count() == 2); |
45 | BOOST_TEST(py2.use_count() == 2); |
46 | } |
47 | |
48 | { |
49 | boost::shared_ptr<int const volatile> px(new int); |
50 | |
51 | boost::shared_ptr<int> px2 = boost::const_pointer_cast<int>(r: px); |
52 | boost::shared_ptr<int> px3 = boost::const_pointer_cast<int>(r: std::move(px)); |
53 | BOOST_TEST(!px); |
54 | BOOST_TEST(px.use_count() == 0); |
55 | BOOST_TEST(px2.get() == px3.get()); |
56 | BOOST_TEST(!(px2 < px3 || px2 < px3)); |
57 | BOOST_TEST(px2.use_count() == 2); |
58 | BOOST_TEST(px3.use_count() == 2); |
59 | } |
60 | |
61 | { |
62 | boost::shared_ptr<char> pv(reinterpret_cast<char*>(new Y)); |
63 | |
64 | boost::shared_ptr<Y> py1 = boost::reinterpret_pointer_cast<Y>(r: pv); |
65 | boost::shared_ptr<Y> py2 = boost::reinterpret_pointer_cast<Y>(r: std::move(pv)); |
66 | BOOST_TEST(!pv); |
67 | BOOST_TEST(pv.use_count() == 0); |
68 | BOOST_TEST(py1.get() == py2.get()); |
69 | BOOST_TEST(!(py1 < py2 || py2 < py1)); |
70 | BOOST_TEST(py1.use_count() == 2); |
71 | BOOST_TEST(py2.use_count() == 2); |
72 | } |
73 | |
74 | #if !defined( BOOST_NO_RTTI ) |
75 | { |
76 | boost::shared_ptr<U> pu(new V); |
77 | |
78 | boost::shared_ptr<V> pv1 = boost::dynamic_pointer_cast<V>(r: pu); |
79 | boost::shared_ptr<V> pv2 = boost::dynamic_pointer_cast<V>(r: std::move(pu)); |
80 | BOOST_TEST(!pu); |
81 | BOOST_TEST(pu.use_count() == 0); |
82 | BOOST_TEST(pv1.get() == pv2.get()); |
83 | BOOST_TEST(!(pv1 < pv2 || pv2 < pv1)); |
84 | BOOST_TEST(pv1.use_count() == 2); |
85 | BOOST_TEST(pv2.use_count() == 2); |
86 | } |
87 | |
88 | { |
89 | boost::shared_ptr<U> pu(new V); |
90 | boost::shared_ptr<W> pw = boost::dynamic_pointer_cast<W>(r: std::move(pu)); |
91 | BOOST_TEST(!pw); |
92 | BOOST_TEST(pu); |
93 | } |
94 | #endif // !defined( BOOST_NO_RTTI ) |
95 | |
96 | return boost::report_errors(); |
97 | } |
98 | |
99 | #else // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) |
100 | |
101 | int main() |
102 | { |
103 | return 0; |
104 | } |
105 | |
106 | #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) |
107 |