Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 3 | |
| 4 | // |
| 5 | // shared_ptr.hpp |
| 6 | // |
| 7 | // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. |
| 8 | // Copyright (c) 2001-2008 Peter Dimov |
| 9 | // |
| 10 | // Distributed under the Boost Software License, Version 1.0. (See |
| 11 | // accompanying file LICENSE_1_0.txt or copy at |
| 12 | // http://www.boost.org/LICENSE_1_0.txt) |
| 13 | // |
| 14 | // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. |
| 15 | // |
| 16 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 17 | #include <ndnboost/config.hpp> // for broken compiler workarounds |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 18 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 19 | #if defined(NDNBOOST_NO_MEMBER_TEMPLATES) && !defined(NDNBOOST_MSVC6_MEMBER_TEMPLATES) |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 20 | #include <ndnboost/smart_ptr/detail/shared_ptr_nmt.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 21 | #else |
| 22 | |
| 23 | // In order to avoid circular dependencies with Boost.TR1 |
| 24 | // we make sure that our include of <memory> doesn't try to |
| 25 | // pull in the TR1 headers: that's why we use this header |
| 26 | // rather than including <memory> directly: |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 27 | #include <ndnboost/config/no_tr1/memory.hpp> // std::auto_ptr |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 28 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 29 | #include <ndnboost/assert.hpp> |
| 30 | #include <ndnboost/checked_delete.hpp> |
| 31 | #include <ndnboost/throw_exception.hpp> |
| 32 | #include <ndnboost/smart_ptr/detail/shared_count.hpp> |
| 33 | #include <ndnboost/detail/workaround.hpp> |
| 34 | #include <ndnboost/smart_ptr/detail/sp_convertible.hpp> |
| 35 | #include <ndnboost/smart_ptr/detail/sp_nullptr_t.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 37 | #if !defined(NDNBOOST_SP_NO_ATOMIC_ACCESS) |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 38 | #include <ndnboost/smart_ptr/detail/spinlock_pool.hpp> |
| 39 | #include <ndnboost/memory_order.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #include <algorithm> // for std::swap |
| 43 | #include <functional> // for std::less |
| 44 | #include <typeinfo> // for std::bad_cast |
| 45 | #include <cstddef> // for std::size_t |
| 46 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 47 | #if !defined(NDNBOOST_NO_IOSTREAM) |
| 48 | #if !defined(NDNBOOST_NO_IOSFWD) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 49 | #include <iosfwd> // for std::basic_ostream |
| 50 | #else |
| 51 | #include <ostream> |
| 52 | #endif |
| 53 | #endif |
| 54 | |
| 55 | namespace ndnboost |
| 56 | { |
| 57 | |
| 58 | template<class T> class shared_ptr; |
| 59 | template<class T> class weak_ptr; |
| 60 | template<class T> class enable_shared_from_this; |
| 61 | class enable_shared_from_raw; |
| 62 | |
| 63 | namespace detail |
| 64 | { |
| 65 | |
| 66 | // sp_element, element_type |
| 67 | |
| 68 | template< class T > struct sp_element |
| 69 | { |
| 70 | typedef T type; |
| 71 | }; |
| 72 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 73 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 74 | |
| 75 | template< class T > struct sp_element< T[] > |
| 76 | { |
| 77 | typedef T type; |
| 78 | }; |
| 79 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 80 | #if !defined( __BORLANDC__ ) || !NDNBOOST_WORKAROUND( __BORLANDC__, < 0x600 ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 81 | |
| 82 | template< class T, std::size_t N > struct sp_element< T[N] > |
| 83 | { |
| 84 | typedef T type; |
| 85 | }; |
| 86 | |
| 87 | #endif |
| 88 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 89 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 90 | |
| 91 | // sp_dereference, return type of operator* |
| 92 | |
| 93 | template< class T > struct sp_dereference |
| 94 | { |
| 95 | typedef T & type; |
| 96 | }; |
| 97 | |
| 98 | template<> struct sp_dereference< void > |
| 99 | { |
| 100 | typedef void type; |
| 101 | }; |
| 102 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 103 | #if !defined(NDNBOOST_NO_CV_VOID_SPECIALIZATIONS) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 104 | |
| 105 | template<> struct sp_dereference< void const > |
| 106 | { |
| 107 | typedef void type; |
| 108 | }; |
| 109 | |
| 110 | template<> struct sp_dereference< void volatile > |
| 111 | { |
| 112 | typedef void type; |
| 113 | }; |
| 114 | |
| 115 | template<> struct sp_dereference< void const volatile > |
| 116 | { |
| 117 | typedef void type; |
| 118 | }; |
| 119 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 120 | #endif // !defined(NDNBOOST_NO_CV_VOID_SPECIALIZATIONS) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 121 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 122 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 123 | |
| 124 | template< class T > struct sp_dereference< T[] > |
| 125 | { |
| 126 | typedef void type; |
| 127 | }; |
| 128 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 129 | #if !defined( __BORLANDC__ ) || !NDNBOOST_WORKAROUND( __BORLANDC__, < 0x600 ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 130 | |
| 131 | template< class T, std::size_t N > struct sp_dereference< T[N] > |
| 132 | { |
| 133 | typedef void type; |
| 134 | }; |
| 135 | |
| 136 | #endif |
| 137 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 138 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 139 | |
| 140 | // sp_member_access, return type of operator-> |
| 141 | |
| 142 | template< class T > struct sp_member_access |
| 143 | { |
| 144 | typedef T * type; |
| 145 | }; |
| 146 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 147 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 148 | |
| 149 | template< class T > struct sp_member_access< T[] > |
| 150 | { |
| 151 | typedef void type; |
| 152 | }; |
| 153 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 154 | #if !defined( __BORLANDC__ ) || !NDNBOOST_WORKAROUND( __BORLANDC__, < 0x600 ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 155 | |
| 156 | template< class T, std::size_t N > struct sp_member_access< T[N] > |
| 157 | { |
| 158 | typedef void type; |
| 159 | }; |
| 160 | |
| 161 | #endif |
| 162 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 163 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 164 | |
| 165 | // sp_array_access, return type of operator[] |
| 166 | |
| 167 | template< class T > struct sp_array_access |
| 168 | { |
| 169 | typedef void type; |
| 170 | }; |
| 171 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 172 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 173 | |
| 174 | template< class T > struct sp_array_access< T[] > |
| 175 | { |
| 176 | typedef T & type; |
| 177 | }; |
| 178 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 179 | #if !defined( __BORLANDC__ ) || !NDNBOOST_WORKAROUND( __BORLANDC__, < 0x600 ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 180 | |
| 181 | template< class T, std::size_t N > struct sp_array_access< T[N] > |
| 182 | { |
| 183 | typedef T & type; |
| 184 | }; |
| 185 | |
| 186 | #endif |
| 187 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 188 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 189 | |
| 190 | // sp_extent, for operator[] index check |
| 191 | |
| 192 | template< class T > struct sp_extent |
| 193 | { |
| 194 | enum _vt { value = 0 }; |
| 195 | }; |
| 196 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 197 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 198 | |
| 199 | template< class T, std::size_t N > struct sp_extent< T[N] > |
| 200 | { |
| 201 | enum _vt { value = N }; |
| 202 | }; |
| 203 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 204 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 205 | |
| 206 | // enable_shared_from_this support |
| 207 | |
| 208 | template< class X, class Y, class T > inline void sp_enable_shared_from_this( ndnboost::shared_ptr<X> const * ppx, Y const * py, ndnboost::enable_shared_from_this< T > const * pe ) |
| 209 | { |
| 210 | if( pe != 0 ) |
| 211 | { |
| 212 | pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | template< class X, class Y > inline void sp_enable_shared_from_this( ndnboost::shared_ptr<X> * ppx, Y const * py, ndnboost::enable_shared_from_raw const * pe ); |
| 217 | |
| 218 | #ifdef _MANAGED |
| 219 | |
| 220 | // Avoid C4793, ... causes native code generation |
| 221 | |
| 222 | struct sp_any_pointer |
| 223 | { |
| 224 | template<class T> sp_any_pointer( T* ) {} |
| 225 | }; |
| 226 | |
| 227 | inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer ) |
| 228 | { |
| 229 | } |
| 230 | |
| 231 | #else // _MANAGED |
| 232 | |
| 233 | inline void sp_enable_shared_from_this( ... ) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | #endif // _MANAGED |
| 238 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 239 | #if !defined( NDNBOOST_NO_SFINAE ) && !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( NDNBOOST_NO_AUTO_PTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 240 | |
| 241 | // rvalue auto_ptr support based on a technique by Dave Abrahams |
| 242 | |
| 243 | template< class T, class R > struct sp_enable_if_auto_ptr |
| 244 | { |
| 245 | }; |
| 246 | |
| 247 | template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R > |
| 248 | { |
| 249 | typedef R type; |
| 250 | }; |
| 251 | |
| 252 | #endif |
| 253 | |
| 254 | // sp_assert_convertible |
| 255 | |
| 256 | template< class Y, class T > inline void sp_assert_convertible() |
| 257 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 258 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 259 | |
| 260 | // static_assert( sp_convertible< Y, T >::value ); |
| 261 | typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ]; |
| 262 | (void)sizeof( tmp ); |
| 263 | |
| 264 | #else |
| 265 | |
| 266 | T* p = static_cast< Y* >( 0 ); |
| 267 | (void)p; |
| 268 | |
| 269 | #endif |
| 270 | } |
| 271 | |
| 272 | // pointer constructor helper |
| 273 | |
| 274 | template< class T, class Y > inline void sp_pointer_construct( ndnboost::shared_ptr< T > * ppx, Y * p, ndnboost::detail::shared_count & pn ) |
| 275 | { |
| 276 | ndnboost::detail::shared_count( p ).swap( pn ); |
| 277 | ndnboost::detail::sp_enable_shared_from_this( ppx, p, p ); |
| 278 | } |
| 279 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 280 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 281 | |
| 282 | template< class T, class Y > inline void sp_pointer_construct( ndnboost::shared_ptr< T[] > * /*ppx*/, Y * p, ndnboost::detail::shared_count & pn ) |
| 283 | { |
| 284 | sp_assert_convertible< Y[], T[] >(); |
| 285 | ndnboost::detail::shared_count( p, ndnboost::checked_array_deleter< T >() ).swap( pn ); |
| 286 | } |
| 287 | |
| 288 | template< class T, std::size_t N, class Y > inline void sp_pointer_construct( ndnboost::shared_ptr< T[N] > * /*ppx*/, Y * p, ndnboost::detail::shared_count & pn ) |
| 289 | { |
| 290 | sp_assert_convertible< Y[N], T[N] >(); |
| 291 | ndnboost::detail::shared_count( p, ndnboost::checked_array_deleter< T >() ).swap( pn ); |
| 292 | } |
| 293 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 294 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 295 | |
| 296 | // deleter constructor helper |
| 297 | |
| 298 | template< class T, class Y > inline void sp_deleter_construct( ndnboost::shared_ptr< T > * ppx, Y * p ) |
| 299 | { |
| 300 | ndnboost::detail::sp_enable_shared_from_this( ppx, p, p ); |
| 301 | } |
| 302 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 303 | #if !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 304 | |
| 305 | template< class T, class Y > inline void sp_deleter_construct( ndnboost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ ) |
| 306 | { |
| 307 | sp_assert_convertible< Y[], T[] >(); |
| 308 | } |
| 309 | |
| 310 | template< class T, std::size_t N, class Y > inline void sp_deleter_construct( ndnboost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ ) |
| 311 | { |
| 312 | sp_assert_convertible< Y[N], T[N] >(); |
| 313 | } |
| 314 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 315 | #endif // !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 316 | |
| 317 | } // namespace detail |
| 318 | |
| 319 | |
| 320 | // |
| 321 | // shared_ptr |
| 322 | // |
| 323 | // An enhanced relative of scoped_ptr with reference counted copy semantics. |
| 324 | // The object pointed to is deleted when the last shared_ptr pointing to it |
| 325 | // is destroyed or reset. |
| 326 | // |
| 327 | |
| 328 | template<class T> class shared_ptr |
| 329 | { |
| 330 | private: |
| 331 | |
| 332 | // Borland 5.5.1 specific workaround |
| 333 | typedef shared_ptr<T> this_type; |
| 334 | |
| 335 | public: |
| 336 | |
| 337 | typedef typename ndnboost::detail::sp_element< T >::type element_type; |
| 338 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 339 | shared_ptr() NDNBOOST_NOEXCEPT : px( 0 ), pn() // never throws in 1.30+ |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 340 | { |
| 341 | } |
| 342 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 343 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 344 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 345 | shared_ptr( ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT : px( 0 ), pn() // never throws |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 346 | { |
| 347 | } |
| 348 | |
| 349 | #endif |
| 350 | |
| 351 | template<class Y> |
| 352 | explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete |
| 353 | { |
| 354 | ndnboost::detail::sp_pointer_construct( this, p, pn ); |
| 355 | } |
| 356 | |
| 357 | // |
| 358 | // Requirements: D's copy constructor must not throw |
| 359 | // |
| 360 | // shared_ptr will release p by calling d(p) |
| 361 | // |
| 362 | |
| 363 | template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d ) |
| 364 | { |
| 365 | ndnboost::detail::sp_deleter_construct( this, p ); |
| 366 | } |
| 367 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 368 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 369 | |
| 370 | template<class D> shared_ptr( ndnboost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d ) |
| 371 | { |
| 372 | } |
| 373 | |
| 374 | #endif |
| 375 | |
| 376 | // As above, but with allocator. A's copy constructor shall not throw. |
| 377 | |
| 378 | template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a ) |
| 379 | { |
| 380 | ndnboost::detail::sp_deleter_construct( this, p ); |
| 381 | } |
| 382 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 383 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 384 | |
| 385 | template<class D, class A> shared_ptr( ndnboost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a ) |
| 386 | { |
| 387 | } |
| 388 | |
| 389 | #endif |
| 390 | |
| 391 | // generated copy constructor, destructor are fine... |
| 392 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 393 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 394 | |
| 395 | // ... except in C++0x, move disables the implicit copy |
| 396 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 397 | shared_ptr( shared_ptr const & r ) NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 398 | { |
| 399 | } |
| 400 | |
| 401 | #endif |
| 402 | |
| 403 | template<class Y> |
| 404 | explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw |
| 405 | { |
| 406 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 407 | |
| 408 | // it is now safe to copy r.px, as pn(r.pn) did not throw |
| 409 | px = r.px; |
| 410 | } |
| 411 | |
| 412 | template<class Y> |
| 413 | shared_ptr( weak_ptr<Y> const & r, ndnboost::detail::sp_nothrow_tag ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 414 | NDNBOOST_NOEXCEPT : px( 0 ), pn( r.pn, ndnboost::detail::sp_nothrow_tag() ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 415 | { |
| 416 | if( !pn.empty() ) |
| 417 | { |
| 418 | px = r.px; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 423 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 424 | |
| 425 | shared_ptr( shared_ptr<Y> const & r, typename ndnboost::detail::sp_enable_if_convertible<Y,T>::type = ndnboost::detail::sp_empty() ) |
| 426 | |
| 427 | #else |
| 428 | |
| 429 | shared_ptr( shared_ptr<Y> const & r ) |
| 430 | |
| 431 | #endif |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 432 | NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 433 | { |
| 434 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 435 | } |
| 436 | |
| 437 | // aliasing |
| 438 | template< class Y > |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 439 | shared_ptr( shared_ptr<Y> const & r, element_type * p ) NDNBOOST_NOEXCEPT : px( p ), pn( r.pn ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 440 | { |
| 441 | } |
| 442 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 443 | #ifndef NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 444 | |
| 445 | template<class Y> |
| 446 | explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn() |
| 447 | { |
| 448 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 449 | |
| 450 | Y * tmp = r.get(); |
| 451 | pn = ndnboost::detail::shared_count( r ); |
| 452 | |
| 453 | ndnboost::detail::sp_deleter_construct( this, tmp ); |
| 454 | } |
| 455 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 456 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 457 | |
| 458 | template<class Y> |
| 459 | shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn() |
| 460 | { |
| 461 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 462 | |
| 463 | Y * tmp = r.get(); |
| 464 | pn = ndnboost::detail::shared_count( r ); |
| 465 | |
| 466 | ndnboost::detail::sp_deleter_construct( this, tmp ); |
| 467 | } |
| 468 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 469 | #elif !defined( NDNBOOST_NO_SFINAE ) && !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 470 | |
| 471 | template<class Ap> |
| 472 | explicit shared_ptr( Ap r, typename ndnboost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn() |
| 473 | { |
| 474 | typedef typename Ap::element_type Y; |
| 475 | |
| 476 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 477 | |
| 478 | Y * tmp = r.get(); |
| 479 | pn = ndnboost::detail::shared_count( r ); |
| 480 | |
| 481 | ndnboost::detail::sp_deleter_construct( this, tmp ); |
| 482 | } |
| 483 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 484 | #endif // NDNBOOST_NO_SFINAE, NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 485 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 486 | #endif // NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 487 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 488 | #if !defined( NDNBOOST_NO_CXX11_SMART_PTR ) && !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 489 | |
| 490 | template< class Y, class D > |
| 491 | shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn() |
| 492 | { |
| 493 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 494 | |
| 495 | typename std::unique_ptr< Y, D >::pointer tmp = r.get(); |
| 496 | pn = ndnboost::detail::shared_count( r ); |
| 497 | |
| 498 | ndnboost::detail::sp_deleter_construct( this, tmp ); |
| 499 | } |
| 500 | |
| 501 | #endif |
| 502 | |
| 503 | // assignment |
| 504 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 505 | shared_ptr & operator=( shared_ptr const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 506 | { |
| 507 | this_type(r).swap(*this); |
| 508 | return *this; |
| 509 | } |
| 510 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 511 | #if !defined(NDNBOOST_MSVC) || (NDNBOOST_MSVC >= 1400) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 512 | |
| 513 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 514 | shared_ptr & operator=(shared_ptr<Y> const & r) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 515 | { |
| 516 | this_type(r).swap(*this); |
| 517 | return *this; |
| 518 | } |
| 519 | |
| 520 | #endif |
| 521 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 522 | #ifndef NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 523 | |
| 524 | template<class Y> |
| 525 | shared_ptr & operator=( std::auto_ptr<Y> & r ) |
| 526 | { |
| 527 | this_type( r ).swap( *this ); |
| 528 | return *this; |
| 529 | } |
| 530 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 531 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 532 | |
| 533 | template<class Y> |
| 534 | shared_ptr & operator=( std::auto_ptr<Y> && r ) |
| 535 | { |
| 536 | this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this ); |
| 537 | return *this; |
| 538 | } |
| 539 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 540 | #elif !defined( NDNBOOST_NO_SFINAE ) && !defined( NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 541 | |
| 542 | template<class Ap> |
| 543 | typename ndnboost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r ) |
| 544 | { |
| 545 | this_type( r ).swap( *this ); |
| 546 | return *this; |
| 547 | } |
| 548 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 549 | #endif // NDNBOOST_NO_SFINAE, NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 550 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 551 | #endif // NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 552 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 553 | #if !defined( NDNBOOST_NO_CXX11_SMART_PTR ) && !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 554 | |
| 555 | template<class Y, class D> |
| 556 | shared_ptr & operator=( std::unique_ptr<Y, D> && r ) |
| 557 | { |
| 558 | this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this); |
| 559 | return *this; |
| 560 | } |
| 561 | |
| 562 | #endif |
| 563 | |
| 564 | // Move support |
| 565 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 566 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 567 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 568 | shared_ptr( shared_ptr && r ) NDNBOOST_NOEXCEPT : px( r.px ), pn() |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 569 | { |
| 570 | pn.swap( r.pn ); |
| 571 | r.px = 0; |
| 572 | } |
| 573 | |
| 574 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 575 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 576 | |
| 577 | shared_ptr( shared_ptr<Y> && r, typename ndnboost::detail::sp_enable_if_convertible<Y,T>::type = ndnboost::detail::sp_empty() ) |
| 578 | |
| 579 | #else |
| 580 | |
| 581 | shared_ptr( shared_ptr<Y> && r ) |
| 582 | |
| 583 | #endif |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 584 | NDNBOOST_NOEXCEPT : px( r.px ), pn() |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 585 | { |
| 586 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 587 | |
| 588 | pn.swap( r.pn ); |
| 589 | r.px = 0; |
| 590 | } |
| 591 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 592 | shared_ptr & operator=( shared_ptr && r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 593 | { |
| 594 | this_type( static_cast< shared_ptr && >( r ) ).swap( *this ); |
| 595 | return *this; |
| 596 | } |
| 597 | |
| 598 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 599 | shared_ptr & operator=( shared_ptr<Y> && r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 600 | { |
| 601 | this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this ); |
| 602 | return *this; |
| 603 | } |
| 604 | |
| 605 | #endif |
| 606 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 607 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 608 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 609 | shared_ptr & operator=( ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT // never throws |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 610 | { |
| 611 | this_type().swap(*this); |
| 612 | return *this; |
| 613 | } |
| 614 | |
| 615 | #endif |
| 616 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 617 | void reset() NDNBOOST_NOEXCEPT // never throws in 1.30+ |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 618 | { |
| 619 | this_type().swap(*this); |
| 620 | } |
| 621 | |
| 622 | template<class Y> void reset( Y * p ) // Y must be complete |
| 623 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 624 | NDNBOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 625 | this_type( p ).swap( *this ); |
| 626 | } |
| 627 | |
| 628 | template<class Y, class D> void reset( Y * p, D d ) |
| 629 | { |
| 630 | this_type( p, d ).swap( *this ); |
| 631 | } |
| 632 | |
| 633 | template<class Y, class D, class A> void reset( Y * p, D d, A a ) |
| 634 | { |
| 635 | this_type( p, d, a ).swap( *this ); |
| 636 | } |
| 637 | |
| 638 | template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) |
| 639 | { |
| 640 | this_type( r, p ).swap( *this ); |
| 641 | } |
| 642 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 643 | // never throws (but has a NDNBOOST_ASSERT in it, so not marked with NDNBOOST_NOEXCEPT) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 644 | typename ndnboost::detail::sp_dereference< T >::type operator* () const |
| 645 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 646 | NDNBOOST_ASSERT( px != 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 647 | return *px; |
| 648 | } |
| 649 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 650 | // never throws (but has a NDNBOOST_ASSERT in it, so not marked with NDNBOOST_NOEXCEPT) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 651 | typename ndnboost::detail::sp_member_access< T >::type operator-> () const |
| 652 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 653 | NDNBOOST_ASSERT( px != 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 654 | return px; |
| 655 | } |
| 656 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 657 | // never throws (but has a NDNBOOST_ASSERT in it, so not marked with NDNBOOST_NOEXCEPT) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 658 | typename ndnboost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const |
| 659 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 660 | NDNBOOST_ASSERT( px != 0 ); |
| 661 | NDNBOOST_ASSERT( i >= 0 && ( i < ndnboost::detail::sp_extent< T >::value || ndnboost::detail::sp_extent< T >::value == 0 ) ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 662 | |
| 663 | return px[ i ]; |
| 664 | } |
| 665 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 666 | element_type * get() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 667 | { |
| 668 | return px; |
| 669 | } |
| 670 | |
| 671 | // implicit conversion to "bool" |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 672 | #include <ndnboost/smart_ptr/detail/operator_bool.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 673 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 674 | bool unique() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 675 | { |
| 676 | return pn.unique(); |
| 677 | } |
| 678 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 679 | long use_count() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 680 | { |
| 681 | return pn.use_count(); |
| 682 | } |
| 683 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 684 | void swap( shared_ptr & other ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 685 | { |
| 686 | std::swap(px, other.px); |
| 687 | pn.swap(other.pn); |
| 688 | } |
| 689 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 690 | template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 691 | { |
| 692 | return pn < rhs.pn; |
| 693 | } |
| 694 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 695 | template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 696 | { |
| 697 | return pn < rhs.pn; |
| 698 | } |
| 699 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 700 | void * _internal_get_deleter( ndnboost::detail::sp_typeinfo const & ti ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 701 | { |
| 702 | return pn.get_deleter( ti ); |
| 703 | } |
| 704 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 705 | void * _internal_get_untyped_deleter() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 706 | { |
| 707 | return pn.get_untyped_deleter(); |
| 708 | } |
| 709 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 710 | bool _internal_equiv( shared_ptr const & r ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 711 | { |
| 712 | return px == r.px && pn == r.pn; |
| 713 | } |
| 714 | |
| 715 | // Tasteless as this may seem, making all members public allows member templates |
| 716 | // to work in the absence of member template friends. (Matthew Langston) |
| 717 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 718 | #ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 719 | |
| 720 | private: |
| 721 | |
| 722 | template<class Y> friend class shared_ptr; |
| 723 | template<class Y> friend class weak_ptr; |
| 724 | |
| 725 | |
| 726 | #endif |
| 727 | |
| 728 | element_type * px; // contained pointer |
| 729 | ndnboost::detail::shared_count pn; // reference counter |
| 730 | |
| 731 | }; // shared_ptr |
| 732 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 733 | template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 734 | { |
| 735 | return a.get() == b.get(); |
| 736 | } |
| 737 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 738 | template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 739 | { |
| 740 | return a.get() != b.get(); |
| 741 | } |
| 742 | |
| 743 | #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 |
| 744 | |
| 745 | // Resolve the ambiguity between our op!= and the one in rel_ops |
| 746 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 747 | template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 748 | { |
| 749 | return a.get() != b.get(); |
| 750 | } |
| 751 | |
| 752 | #endif |
| 753 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 754 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 755 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 756 | template<class T> inline bool operator==( shared_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 757 | { |
| 758 | return p.get() == 0; |
| 759 | } |
| 760 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 761 | template<class T> inline bool operator==( ndnboost::detail::sp_nullptr_t, shared_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 762 | { |
| 763 | return p.get() == 0; |
| 764 | } |
| 765 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 766 | template<class T> inline bool operator!=( shared_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 767 | { |
| 768 | return p.get() != 0; |
| 769 | } |
| 770 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 771 | template<class T> inline bool operator!=( ndnboost::detail::sp_nullptr_t, shared_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 772 | { |
| 773 | return p.get() != 0; |
| 774 | } |
| 775 | |
| 776 | #endif |
| 777 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 778 | template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 779 | { |
| 780 | return a.owner_before( b ); |
| 781 | } |
| 782 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 783 | template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 784 | { |
| 785 | a.swap(b); |
| 786 | } |
| 787 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 788 | template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 789 | { |
| 790 | (void) static_cast< T* >( static_cast< U* >( 0 ) ); |
| 791 | |
| 792 | typedef typename shared_ptr<T>::element_type E; |
| 793 | |
| 794 | E * p = static_cast< E* >( r.get() ); |
| 795 | return shared_ptr<T>( r, p ); |
| 796 | } |
| 797 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 798 | template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 799 | { |
| 800 | (void) const_cast< T* >( static_cast< U* >( 0 ) ); |
| 801 | |
| 802 | typedef typename shared_ptr<T>::element_type E; |
| 803 | |
| 804 | E * p = const_cast< E* >( r.get() ); |
| 805 | return shared_ptr<T>( r, p ); |
| 806 | } |
| 807 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 808 | template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 809 | { |
| 810 | (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); |
| 811 | |
| 812 | typedef typename shared_ptr<T>::element_type E; |
| 813 | |
| 814 | E * p = dynamic_cast< E* >( r.get() ); |
| 815 | return p? shared_ptr<T>( r, p ): shared_ptr<T>(); |
| 816 | } |
| 817 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 818 | template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 819 | { |
| 820 | (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); |
| 821 | |
| 822 | typedef typename shared_ptr<T>::element_type E; |
| 823 | |
| 824 | E * p = reinterpret_cast< E* >( r.get() ); |
| 825 | return shared_ptr<T>( r, p ); |
| 826 | } |
| 827 | |
| 828 | // get_pointer() enables ndnboost::mem_fn to recognize shared_ptr |
| 829 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 830 | template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 831 | { |
| 832 | return p.get(); |
| 833 | } |
| 834 | |
| 835 | // operator<< |
| 836 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 837 | #if !defined(NDNBOOST_NO_IOSTREAM) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 838 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 839 | #if defined(NDNBOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 840 | |
| 841 | template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p) |
| 842 | { |
| 843 | os << p.get(); |
| 844 | return os; |
| 845 | } |
| 846 | |
| 847 | #else |
| 848 | |
| 849 | // in STLport's no-iostreams mode no iostream symbols can be used |
| 850 | #ifndef _STLP_NO_IOSTREAMS |
| 851 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 852 | # if defined(NDNBOOST_MSVC) && NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300 && __SGI_STL_PORT) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 853 | // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL |
| 854 | using std::basic_ostream; |
| 855 | template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p) |
| 856 | # else |
| 857 | template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p) |
| 858 | # endif |
| 859 | { |
| 860 | os << p.get(); |
| 861 | return os; |
| 862 | } |
| 863 | |
| 864 | #endif // _STLP_NO_IOSTREAMS |
| 865 | |
| 866 | #endif // __GNUC__ < 3 |
| 867 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 868 | #endif // !defined(NDNBOOST_NO_IOSTREAM) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 869 | |
| 870 | // get_deleter |
| 871 | |
| 872 | namespace detail |
| 873 | { |
| 874 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 875 | #if ( defined(__GNUC__) && NDNBOOST_WORKAROUND(__GNUC__, < 3) ) || \ |
| 876 | ( defined(__EDG_VERSION__) && NDNBOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \ |
| 877 | ( defined(__HP_aCC) && NDNBOOST_WORKAROUND(__HP_aCC, <= 33500) ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 878 | |
| 879 | // g++ 2.9x doesn't allow static_cast<X const *>(void *) |
| 880 | // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it |
| 881 | |
| 882 | template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p) |
| 883 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 884 | void const * q = p._internal_get_deleter(NDNBOOST_SP_TYPEID(D)); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 885 | return const_cast<D *>(static_cast<D const *>(q)); |
| 886 | } |
| 887 | |
| 888 | #else |
| 889 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 890 | template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 891 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 892 | return static_cast<D *>( p._internal_get_deleter(NDNBOOST_SP_TYPEID(D)) ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | #endif |
| 896 | |
| 897 | class esft2_deleter_wrapper |
| 898 | { |
| 899 | private: |
| 900 | |
| 901 | shared_ptr<void> deleter_; |
| 902 | |
| 903 | public: |
| 904 | |
| 905 | esft2_deleter_wrapper() |
| 906 | { |
| 907 | } |
| 908 | |
| 909 | template< class T > void set_deleter( shared_ptr<T> const & deleter ) |
| 910 | { |
| 911 | deleter_ = deleter; |
| 912 | } |
| 913 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 914 | template<typename D> D* get_deleter() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 915 | { |
| 916 | return ndnboost::detail::basic_get_deleter<D>( deleter_ ); |
| 917 | } |
| 918 | |
| 919 | template< class T> void operator()( T* ) |
| 920 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 921 | NDNBOOST_ASSERT( deleter_.use_count() <= 1 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 922 | deleter_.reset(); |
| 923 | } |
| 924 | }; |
| 925 | |
| 926 | } // namespace detail |
| 927 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 928 | template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 929 | { |
| 930 | D *del = ndnboost::detail::basic_get_deleter<D>(p); |
| 931 | |
| 932 | if(del == 0) |
| 933 | { |
| 934 | ndnboost::detail::esft2_deleter_wrapper *del_wrapper = ndnboost::detail::basic_get_deleter<ndnboost::detail::esft2_deleter_wrapper>(p); |
| 935 | // The following get_deleter method call is fully qualified because |
| 936 | // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>() |
| 937 | if(del_wrapper) del = del_wrapper->::ndnboost::detail::esft2_deleter_wrapper::get_deleter<D>(); |
| 938 | } |
| 939 | |
| 940 | return del; |
| 941 | } |
| 942 | |
| 943 | // atomic access |
| 944 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 945 | #if !defined(NDNBOOST_SP_NO_ATOMIC_ACCESS) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 946 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 947 | template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 948 | { |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) |
| 953 | { |
| 954 | ndnboost::detail::spinlock_pool<2>::scoped_lock lock( p ); |
| 955 | return *p; |
| 956 | } |
| 957 | |
| 958 | template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order /*mo*/ ) |
| 959 | { |
| 960 | return atomic_load( p ); |
| 961 | } |
| 962 | |
| 963 | template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) |
| 964 | { |
| 965 | ndnboost::detail::spinlock_pool<2>::scoped_lock lock( p ); |
| 966 | p->swap( r ); |
| 967 | } |
| 968 | |
| 969 | template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ ) |
| 970 | { |
| 971 | atomic_store( p, r ); // std::move( r ) |
| 972 | } |
| 973 | |
| 974 | template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) |
| 975 | { |
| 976 | ndnboost::detail::spinlock & sp = ndnboost::detail::spinlock_pool<2>::spinlock_for( p ); |
| 977 | |
| 978 | sp.lock(); |
| 979 | p->swap( r ); |
| 980 | sp.unlock(); |
| 981 | |
| 982 | return r; // return std::move( r ) |
| 983 | } |
| 984 | |
| 985 | template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ ) |
| 986 | { |
| 987 | return atomic_exchange( p, r ); // std::move( r ) |
| 988 | } |
| 989 | |
| 990 | template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) |
| 991 | { |
| 992 | ndnboost::detail::spinlock & sp = ndnboost::detail::spinlock_pool<2>::spinlock_for( p ); |
| 993 | |
| 994 | sp.lock(); |
| 995 | |
| 996 | if( p->_internal_equiv( *v ) ) |
| 997 | { |
| 998 | p->swap( w ); |
| 999 | |
| 1000 | sp.unlock(); |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | else |
| 1005 | { |
| 1006 | shared_ptr<T> tmp( *p ); |
| 1007 | |
| 1008 | sp.unlock(); |
| 1009 | |
| 1010 | tmp.swap( *v ); |
| 1011 | return false; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ ) |
| 1016 | { |
| 1017 | return atomic_compare_exchange( p, v, w ); // std::move( w ) |
| 1018 | } |
| 1019 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1020 | #endif // !defined(NDNBOOST_SP_NO_ATOMIC_ACCESS) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 1021 | |
| 1022 | // hash_value |
| 1023 | |
| 1024 | template< class T > struct hash; |
| 1025 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1026 | template< class T > std::size_t hash_value( ndnboost::shared_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 1027 | { |
| 1028 | return ndnboost::hash< T* >()( p.get() ); |
| 1029 | } |
| 1030 | |
| 1031 | } // namespace ndnboost |
| 1032 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1033 | #endif // #if defined(NDNBOOST_NO_MEMBER_TEMPLATES) && !defined(NDNBOOST_MSVC6_MEMBER_TEMPLATES) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 1034 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1035 | #endif // #ifndef NDNBOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED |