Chris@16
|
1 #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
|
Chris@16
|
2 #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
|
Chris@16
|
3
|
Chris@16
|
4 //
|
Chris@16
|
5 // shared_array.hpp
|
Chris@16
|
6 //
|
Chris@16
|
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
Chris@16
|
8 // Copyright (c) 2001, 2002, 2012 Peter Dimov
|
Chris@16
|
9 //
|
Chris@16
|
10 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
11 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
12 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
13 //
|
Chris@16
|
14 // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
|
Chris@16
|
15 //
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/config.hpp> // for broken compiler workarounds
|
Chris@16
|
18
|
Chris@16
|
19 #include <memory> // TR1 cyclic inclusion fix
|
Chris@16
|
20
|
Chris@16
|
21 #include <boost/assert.hpp>
|
Chris@16
|
22 #include <boost/checked_delete.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 #include <boost/smart_ptr/shared_ptr.hpp>
|
Chris@16
|
25 #include <boost/smart_ptr/detail/shared_count.hpp>
|
Chris@16
|
26 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
|
Chris@16
|
27 #include <boost/detail/workaround.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <cstddef> // for std::ptrdiff_t
|
Chris@16
|
30 #include <algorithm> // for std::swap
|
Chris@16
|
31 #include <functional> // for std::less
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost
|
Chris@16
|
34 {
|
Chris@16
|
35
|
Chris@16
|
36 //
|
Chris@16
|
37 // shared_array
|
Chris@16
|
38 //
|
Chris@16
|
39 // shared_array extends shared_ptr to arrays.
|
Chris@16
|
40 // The array pointed to is deleted when the last shared_array pointing to it
|
Chris@16
|
41 // is destroyed or reset.
|
Chris@16
|
42 //
|
Chris@16
|
43
|
Chris@16
|
44 template<class T> class shared_array
|
Chris@16
|
45 {
|
Chris@16
|
46 private:
|
Chris@16
|
47
|
Chris@16
|
48 // Borland 5.5.1 specific workarounds
|
Chris@16
|
49 typedef checked_array_deleter<T> deleter;
|
Chris@16
|
50 typedef shared_array<T> this_type;
|
Chris@16
|
51
|
Chris@16
|
52 public:
|
Chris@16
|
53
|
Chris@16
|
54 typedef T element_type;
|
Chris@16
|
55
|
Chris@16
|
56 shared_array() BOOST_NOEXCEPT : px( 0 ), pn()
|
Chris@16
|
57 {
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@101
|
60 #if !defined( BOOST_NO_CXX11_NULLPTR )
|
Chris@101
|
61
|
Chris@101
|
62 shared_array( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn()
|
Chris@101
|
63 {
|
Chris@101
|
64 }
|
Chris@101
|
65
|
Chris@101
|
66 #endif
|
Chris@101
|
67
|
Chris@16
|
68 template<class Y>
|
Chris@16
|
69 explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
|
Chris@16
|
70 {
|
Chris@16
|
71 boost::detail::sp_assert_convertible< Y[], T[] >();
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 //
|
Chris@16
|
75 // Requirements: D's copy constructor must not throw
|
Chris@16
|
76 //
|
Chris@16
|
77 // shared_array will release p by calling d(p)
|
Chris@16
|
78 //
|
Chris@16
|
79
|
Chris@16
|
80 template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
|
Chris@16
|
81 {
|
Chris@16
|
82 boost::detail::sp_assert_convertible< Y[], T[] >();
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 // As above, but with allocator. A's copy constructor shall not throw.
|
Chris@16
|
86
|
Chris@16
|
87 template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
|
Chris@16
|
88 {
|
Chris@16
|
89 boost::detail::sp_assert_convertible< Y[], T[] >();
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 // generated copy constructor, destructor are fine...
|
Chris@16
|
93
|
Chris@16
|
94 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
Chris@16
|
95
|
Chris@16
|
96 // ... except in C++0x, move disables the implicit copy
|
Chris@16
|
97
|
Chris@16
|
98 shared_array( shared_array const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
|
Chris@16
|
99 {
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 shared_array( shared_array && r ) BOOST_NOEXCEPT : px( r.px ), pn()
|
Chris@16
|
103 {
|
Chris@16
|
104 pn.swap( r.pn );
|
Chris@16
|
105 r.px = 0;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 #endif
|
Chris@16
|
109
|
Chris@16
|
110 // conversion
|
Chris@16
|
111
|
Chris@16
|
112 template<class Y>
|
Chris@16
|
113 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
Chris@16
|
114
|
Chris@16
|
115 shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
|
Chris@16
|
116
|
Chris@16
|
117 #else
|
Chris@16
|
118
|
Chris@16
|
119 shared_array( shared_array<Y> const & r )
|
Chris@16
|
120
|
Chris@16
|
121 #endif
|
Chris@16
|
122 BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) // never throws
|
Chris@16
|
123 {
|
Chris@16
|
124 boost::detail::sp_assert_convertible< Y[], T[] >();
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 // aliasing
|
Chris@16
|
128
|
Chris@16
|
129 template< class Y >
|
Chris@16
|
130 shared_array( shared_array<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
|
Chris@16
|
131 {
|
Chris@16
|
132 }
|
Chris@16
|
133
|
Chris@16
|
134 // assignment
|
Chris@16
|
135
|
Chris@16
|
136 shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT
|
Chris@16
|
137 {
|
Chris@16
|
138 this_type( r ).swap( *this );
|
Chris@16
|
139 return *this;
|
Chris@16
|
140 }
|
Chris@16
|
141
|
Chris@16
|
142 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
|
Chris@16
|
143
|
Chris@16
|
144 template<class Y>
|
Chris@16
|
145 shared_array & operator=( shared_array<Y> const & r ) BOOST_NOEXCEPT
|
Chris@16
|
146 {
|
Chris@16
|
147 this_type( r ).swap( *this );
|
Chris@16
|
148 return *this;
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 #endif
|
Chris@16
|
152
|
Chris@16
|
153 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
Chris@16
|
154
|
Chris@16
|
155 shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT
|
Chris@16
|
156 {
|
Chris@16
|
157 this_type( static_cast< shared_array && >( r ) ).swap( *this );
|
Chris@16
|
158 return *this;
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 template<class Y>
|
Chris@16
|
162 shared_array & operator=( shared_array<Y> && r ) BOOST_NOEXCEPT
|
Chris@16
|
163 {
|
Chris@16
|
164 this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
|
Chris@16
|
165 return *this;
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 #endif
|
Chris@16
|
169
|
Chris@16
|
170 void reset() BOOST_NOEXCEPT
|
Chris@16
|
171 {
|
Chris@16
|
172 this_type().swap( *this );
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 template<class Y> void reset( Y * p ) // Y must be complete
|
Chris@16
|
176 {
|
Chris@16
|
177 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
|
Chris@16
|
178 this_type( p ).swap( *this );
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 template<class Y, class D> void reset( Y * p, D d )
|
Chris@16
|
182 {
|
Chris@16
|
183 this_type( p, d ).swap( *this );
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 template<class Y, class D, class A> void reset( Y * p, D d, A a )
|
Chris@16
|
187 {
|
Chris@16
|
188 this_type( p, d, a ).swap( *this );
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 template<class Y> void reset( shared_array<Y> const & r, element_type * p )
|
Chris@16
|
192 {
|
Chris@16
|
193 this_type( r, p ).swap( *this );
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 T & operator[] (std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
|
Chris@16
|
197 {
|
Chris@16
|
198 BOOST_ASSERT(px != 0);
|
Chris@16
|
199 BOOST_ASSERT(i >= 0);
|
Chris@16
|
200 return px[i];
|
Chris@16
|
201 }
|
Chris@16
|
202
|
Chris@16
|
203 T * get() const BOOST_NOEXCEPT
|
Chris@16
|
204 {
|
Chris@16
|
205 return px;
|
Chris@16
|
206 }
|
Chris@16
|
207
|
Chris@16
|
208 // implicit conversion to "bool"
|
Chris@16
|
209 #include <boost/smart_ptr/detail/operator_bool.hpp>
|
Chris@16
|
210
|
Chris@16
|
211 bool unique() const BOOST_NOEXCEPT
|
Chris@16
|
212 {
|
Chris@16
|
213 return pn.unique();
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 long use_count() const BOOST_NOEXCEPT
|
Chris@16
|
217 {
|
Chris@16
|
218 return pn.use_count();
|
Chris@16
|
219 }
|
Chris@16
|
220
|
Chris@16
|
221 void swap(shared_array<T> & other) BOOST_NOEXCEPT
|
Chris@16
|
222 {
|
Chris@16
|
223 std::swap(px, other.px);
|
Chris@16
|
224 pn.swap(other.pn);
|
Chris@16
|
225 }
|
Chris@16
|
226
|
Chris@16
|
227 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
|
Chris@16
|
228 {
|
Chris@16
|
229 return pn.get_deleter( ti );
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 private:
|
Chris@16
|
233
|
Chris@16
|
234 template<class Y> friend class shared_array;
|
Chris@16
|
235
|
Chris@16
|
236 T * px; // contained pointer
|
Chris@16
|
237 detail::shared_count pn; // reference counter
|
Chris@16
|
238
|
Chris@16
|
239 }; // shared_array
|
Chris@16
|
240
|
Chris@16
|
241 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
|
Chris@16
|
242 {
|
Chris@16
|
243 return a.get() == b.get();
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
|
Chris@16
|
247 {
|
Chris@16
|
248 return a.get() != b.get();
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 #if !defined( BOOST_NO_CXX11_NULLPTR )
|
Chris@16
|
252
|
Chris@16
|
253 template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
|
Chris@16
|
254 {
|
Chris@16
|
255 return p.get() == 0;
|
Chris@16
|
256 }
|
Chris@16
|
257
|
Chris@16
|
258 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
|
Chris@16
|
259 {
|
Chris@16
|
260 return p.get() == 0;
|
Chris@16
|
261 }
|
Chris@16
|
262
|
Chris@16
|
263 template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
|
Chris@16
|
264 {
|
Chris@16
|
265 return p.get() != 0;
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
|
Chris@16
|
269 {
|
Chris@16
|
270 return p.get() != 0;
|
Chris@16
|
271 }
|
Chris@16
|
272
|
Chris@16
|
273 #endif
|
Chris@16
|
274
|
Chris@16
|
275 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
|
Chris@16
|
276 {
|
Chris@16
|
277 return std::less<T*>()(a.get(), b.get());
|
Chris@16
|
278 }
|
Chris@16
|
279
|
Chris@16
|
280 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_NOEXCEPT
|
Chris@16
|
281 {
|
Chris@16
|
282 a.swap(b);
|
Chris@16
|
283 }
|
Chris@16
|
284
|
Chris@16
|
285 template< class D, class T > D * get_deleter( shared_array<T> const & p )
|
Chris@16
|
286 {
|
Chris@16
|
287 return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID(D) ) );
|
Chris@16
|
288 }
|
Chris@16
|
289
|
Chris@16
|
290 } // namespace boost
|
Chris@16
|
291
|
Chris@16
|
292 #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
|