1 | // (C) Copyright John Maddock 2005. |
---|
2 | // Use, modification and distribution are subject to the |
---|
3 | // Boost Software License, Version 1.0. (See accompanying file |
---|
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | #ifdef TEST_STD_HEADERS |
---|
7 | #include <memory> |
---|
8 | #else |
---|
9 | #include <boost/tr1/memory.hpp> |
---|
10 | #endif |
---|
11 | |
---|
12 | #include <iostream> |
---|
13 | #include <boost/static_assert.hpp> |
---|
14 | #include <boost/type_traits/is_base_and_derived.hpp> |
---|
15 | #include <boost/type_traits/is_same.hpp> |
---|
16 | #include "verify_return.hpp" |
---|
17 | |
---|
18 | struct base |
---|
19 | { |
---|
20 | char member(); |
---|
21 | }; |
---|
22 | struct derived : public base{}; |
---|
23 | struct derived_deleter |
---|
24 | { |
---|
25 | void operator()(derived*b) |
---|
26 | { |
---|
27 | delete b; |
---|
28 | } |
---|
29 | }; |
---|
30 | |
---|
31 | struct abstract_base |
---|
32 | { |
---|
33 | virtual ~abstract_base(); |
---|
34 | }; |
---|
35 | |
---|
36 | struct concrete : public abstract_base |
---|
37 | {}; |
---|
38 | |
---|
39 | void noop(){} |
---|
40 | |
---|
41 | struct shared_self : public std::tr1::enable_shared_from_this<shared_self> |
---|
42 | { |
---|
43 | typedef std::tr1::enable_shared_from_this<shared_self> base_type; |
---|
44 | shared_self() : base_type(){} |
---|
45 | shared_self(const shared_self& s) : base_type(s){} |
---|
46 | ~shared_self(){} |
---|
47 | // implicit assignment: |
---|
48 | //shared_self& operator=(const shared_self& s) |
---|
49 | //{ |
---|
50 | // return *this; |
---|
51 | //} |
---|
52 | }; |
---|
53 | |
---|
54 | int main() |
---|
55 | { |
---|
56 | // bad_weak_ptr: |
---|
57 | std::tr1::bad_weak_ptr b; |
---|
58 | BOOST_STATIC_ASSERT((::boost::is_base_and_derived<std::exception, std::tr1::bad_weak_ptr>::value)); |
---|
59 | |
---|
60 | // shared_ptr: |
---|
61 | typedef std::tr1::shared_ptr<derived> pderived; |
---|
62 | typedef std::tr1::shared_ptr<base> pbase; |
---|
63 | typedef std::tr1::shared_ptr<abstract_base> pabase; |
---|
64 | BOOST_STATIC_ASSERT((::boost::is_same<pderived::element_type, derived>::value)); |
---|
65 | pderived pd1; |
---|
66 | pabase pb1(new concrete()); |
---|
67 | pbase pb2(new derived(), derived_deleter()); |
---|
68 | pderived pd2(pd1); |
---|
69 | pbase pb3(pd1); |
---|
70 | std::tr1::weak_ptr<derived>* pweak = 0; |
---|
71 | pbase pb4(*pweak); |
---|
72 | std::auto_ptr<derived>* pap = 0; |
---|
73 | pbase pb5(*pap); |
---|
74 | pb2 = pb3; |
---|
75 | pb2 = pd1; |
---|
76 | pb2 = *pap; |
---|
77 | pb2.swap(pb3); |
---|
78 | pb2.reset(); |
---|
79 | pb1.reset(new concrete()); |
---|
80 | pb2.reset(new derived(), derived_deleter()); |
---|
81 | verify_return_type(pb2.get(), static_cast<base*>(0)); |
---|
82 | verify_return_type(*pb2, base()); |
---|
83 | verify_return_type(pb2->member(), char(0)); |
---|
84 | verify_return_type(pb2.use_count(), long(0)); |
---|
85 | verify_return_type(pb2.unique(), bool(0)); |
---|
86 | if(pb2) { noop(); } |
---|
87 | if(!pb3) { noop(); } |
---|
88 | if(pb2 && pb3) { noop(); } |
---|
89 | // heterogeneous compare: |
---|
90 | verify_return_type(pd1 == pb2, bool()); |
---|
91 | verify_return_type(pd1 != pb2, bool()); |
---|
92 | verify_return_type(pd1 < pb2, bool()); |
---|
93 | std::cout << pb1 << pb2 << std::endl; |
---|
94 | std::tr1::swap(pb2, pb3); |
---|
95 | swap(pb2, pb3); // ADL |
---|
96 | verify_return_type(std::tr1::static_pointer_cast<derived>(pb2), pderived()); |
---|
97 | verify_return_type(std::tr1::dynamic_pointer_cast<concrete>(pb1), std::tr1::shared_ptr<concrete>()); |
---|
98 | verify_return_type(std::tr1::const_pointer_cast<base>(std::tr1::shared_ptr<const base>()), std::tr1::shared_ptr<base>()); |
---|
99 | verify_return_type(std::tr1::get_deleter<derived_deleter>(pb2), static_cast<derived_deleter*>(0)); |
---|
100 | |
---|
101 | // weak_ptr: |
---|
102 | typedef std::tr1::weak_ptr<base> wpb_t; |
---|
103 | BOOST_STATIC_ASSERT((::boost::is_same<wpb_t::element_type, base>::value)); |
---|
104 | wpb_t wpb1; |
---|
105 | wpb_t wpb2(pd1); |
---|
106 | wpb_t wpb3(wpb1); |
---|
107 | std::tr1::weak_ptr<derived> wpd; |
---|
108 | wpb_t wpb4(wpd); |
---|
109 | wpb4 = wpb1; |
---|
110 | wpb4 = wpd; |
---|
111 | wpb4 = pd1; |
---|
112 | wpb4.swap(wpb1); |
---|
113 | wpb4.reset(); |
---|
114 | verify_return_type(wpb4.use_count(), long(0)); |
---|
115 | verify_return_type(wpb4.expired(), bool(0)); |
---|
116 | verify_return_type(wpb4.lock(), pb2); |
---|
117 | |
---|
118 | // enable_shared_from_this: |
---|
119 | typedef std::tr1::shared_ptr<shared_self> pshared_self; |
---|
120 | typedef std::tr1::shared_ptr<const shared_self> pcshared_self; |
---|
121 | pshared_self sf1(new shared_self()); |
---|
122 | pshared_self sf2(new shared_self(*sf1)); |
---|
123 | *sf2 = *sf1; |
---|
124 | pcshared_self csf(sf1); |
---|
125 | verify_return_type(sf1->shared_from_this(), sf1); |
---|
126 | verify_return_type(csf->shared_from_this(), csf); |
---|
127 | } |
---|
128 | |
---|