1 #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001-2008 Peter Dimov
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)
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
17 #include <boost/config.hpp> // for broken compiler workarounds
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/smart_ptr/detail/shared_ptr_nmt.hpp>
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:
27 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
29 #include <boost/assert.hpp>
30 #include <boost/checked_delete.hpp>
31 #include <boost/throw_exception.hpp>
32 #include <boost/smart_ptr/detail/shared_count.hpp>
33 #include <boost/detail/workaround.hpp>
34 #include <boost/smart_ptr/detail/sp_convertible.hpp>
36 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
37 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
38 #include <boost/memory_order.hpp>
41 #include <algorithm> // for std::swap
42 #include <functional> // for std::less
43 #include <typeinfo> // for std::bad_cast
45 #if !defined(BOOST_NO_IOSTREAM)
46 #if !defined(BOOST_NO_IOSFWD)
47 #include <iosfwd> // for std::basic_ostream
53 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
54 # pragma warning(push)
55 # pragma warning(disable:4284) // odd return type for operator->
61 template<class T
> class shared_ptr
;
62 template<class T
> class weak_ptr
;
63 template<class T
> class enable_shared_from_this
;
64 template<class T
> class enable_shared_from_this2
;
69 struct static_cast_tag
{};
70 struct const_cast_tag
{};
71 struct dynamic_cast_tag
{};
72 struct polymorphic_cast_tag
{};
74 template<class T
> struct shared_ptr_traits
76 typedef T
& reference
;
79 template<> struct shared_ptr_traits
<void>
81 typedef void reference
;
84 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
86 template<> struct shared_ptr_traits
<void const>
88 typedef void reference
;
91 template<> struct shared_ptr_traits
<void volatile>
93 typedef void reference
;
96 template<> struct shared_ptr_traits
<void const volatile>
98 typedef void reference
;
103 // enable_shared_from_this support
105 template< class X
, class Y
, class T
> inline void sp_enable_shared_from_this( boost::shared_ptr
<X
> const * ppx
, Y
const * py
, boost::enable_shared_from_this
< T
> const * pe
)
109 pe
->_internal_accept_owner( ppx
, const_cast< Y
* >( py
) );
113 template< class X
, class Y
, class T
> inline void sp_enable_shared_from_this( boost::shared_ptr
<X
> * ppx
, Y
const * py
, boost::enable_shared_from_this2
< T
> const * pe
)
117 pe
->_internal_accept_owner( ppx
, const_cast< Y
* >( py
) );
123 // Avoid C4793, ... causes native code generation
125 struct sp_any_pointer
127 template<class T
> sp_any_pointer( T
* ) {}
130 inline void sp_enable_shared_from_this( sp_any_pointer
, sp_any_pointer
, sp_any_pointer
)
136 inline void sp_enable_shared_from_this( ... )
142 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
144 // rvalue auto_ptr support based on a technique by Dave Abrahams
146 template< class T
, class R
> struct sp_enable_if_auto_ptr
150 template< class T
, class R
> struct sp_enable_if_auto_ptr
< std::auto_ptr
< T
>, R
>
157 } // namespace detail
163 // An enhanced relative of scoped_ptr with reference counted copy semantics.
164 // The object pointed to is deleted when the last shared_ptr pointing to it
165 // is destroyed or reset.
168 template<class T
> class shared_ptr
172 // Borland 5.5.1 specific workaround
173 typedef shared_ptr
<T
> this_type
;
177 typedef T element_type
;
178 typedef T value_type
;
180 typedef typename
boost::detail::shared_ptr_traits
<T
>::reference reference
;
182 shared_ptr(): px(0), pn() // never throws in 1.30+
187 explicit shared_ptr( Y
* p
): px( p
), pn( p
) // Y must be complete
189 boost::detail::sp_enable_shared_from_this( this, p
, p
);
193 // Requirements: D's copy constructor must not throw
195 // shared_ptr will release p by calling d(p)
198 template<class Y
, class D
> shared_ptr(Y
* p
, D d
): px(p
), pn(p
, d
)
200 boost::detail::sp_enable_shared_from_this( this, p
, p
);
203 // As above, but with allocator. A's copy constructor shall not throw.
205 template<class Y
, class D
, class A
> shared_ptr( Y
* p
, D d
, A a
): px( p
), pn( p
, d
, a
)
207 boost::detail::sp_enable_shared_from_this( this, p
, p
);
210 // generated copy constructor, destructor are fine
213 explicit shared_ptr(weak_ptr
<Y
> const & r
): pn(r
.pn
) // may throw
215 // it is now safe to copy r.px, as pn(r.pn) did not throw
220 shared_ptr( weak_ptr
<Y
> const & r
, boost::detail::sp_nothrow_tag
): px( 0 ), pn( r
.pn
, boost::detail::sp_nothrow_tag() ) // never throws
229 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
231 shared_ptr( shared_ptr
<Y
> const & r
, typename
detail::sp_enable_if_convertible
<Y
,T
>::type
= detail::sp_empty() )
235 shared_ptr( shared_ptr
<Y
> const & r
)
238 : px( r
.px
), pn( r
.pn
) // never throws
244 shared_ptr( shared_ptr
<Y
> const & r
, T
* p
): px( p
), pn( r
.pn
) // never throws
249 shared_ptr(shared_ptr
<Y
> const & r
, boost::detail::static_cast_tag
): px(static_cast<element_type
*>(r
.px
)), pn(r
.pn
)
254 shared_ptr(shared_ptr
<Y
> const & r
, boost::detail::const_cast_tag
): px(const_cast<element_type
*>(r
.px
)), pn(r
.pn
)
259 shared_ptr(shared_ptr
<Y
> const & r
, boost::detail::dynamic_cast_tag
): px(dynamic_cast<element_type
*>(r
.px
)), pn(r
.pn
)
261 if(px
== 0) // need to allocate new counter -- the cast failed
263 pn
= boost::detail::shared_count();
268 shared_ptr(shared_ptr
<Y
> const & r
, boost::detail::polymorphic_cast_tag
): px(dynamic_cast<element_type
*>(r
.px
)), pn(r
.pn
)
272 boost::throw_exception(std::bad_cast());
276 #ifndef BOOST_NO_AUTO_PTR
279 explicit shared_ptr(std::auto_ptr
<Y
> & r
): px(r
.get()), pn()
282 pn
= boost::detail::shared_count(r
);
283 boost::detail::sp_enable_shared_from_this( this, tmp
, tmp
);
286 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
289 explicit shared_ptr( Ap r
, typename
boost::detail::sp_enable_if_auto_ptr
<Ap
, int>::type
= 0 ): px( r
.get() ), pn()
291 typename
Ap::element_type
* tmp
= r
.get();
292 pn
= boost::detail::shared_count( r
);
293 boost::detail::sp_enable_shared_from_this( this, tmp
, tmp
);
297 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
299 #endif // BOOST_NO_AUTO_PTR
303 shared_ptr
& operator=( shared_ptr
const & r
) // never throws
305 this_type(r
).swap(*this);
309 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
312 shared_ptr
& operator=(shared_ptr
<Y
> const & r
) // never throws
314 this_type(r
).swap(*this);
320 #ifndef BOOST_NO_AUTO_PTR
323 shared_ptr
& operator=( std::auto_ptr
<Y
> & r
)
325 this_type(r
).swap(*this);
329 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
332 typename
boost::detail::sp_enable_if_auto_ptr
< Ap
, shared_ptr
& >::type
operator=( Ap r
)
334 this_type( r
).swap( *this );
339 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
341 #endif // BOOST_NO_AUTO_PTR
345 #if defined( BOOST_HAS_RVALUE_REFS )
347 shared_ptr( shared_ptr
&& r
): px( r
.px
), pn() // never throws
354 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
356 shared_ptr( shared_ptr
<Y
> && r
, typename
detail::sp_enable_if_convertible
<Y
,T
>::type
= detail::sp_empty() )
360 shared_ptr( shared_ptr
<Y
> && r
)
363 : px( r
.px
), pn() // never throws
369 shared_ptr
& operator=( shared_ptr
&& r
) // never throws
371 this_type( std::move( r
) ).swap( *this );
376 shared_ptr
& operator=( shared_ptr
<Y
> && r
) // never throws
378 this_type( std::move( r
) ).swap( *this );
384 void reset() // never throws in 1.30+
386 this_type().swap(*this);
389 template<class Y
> void reset(Y
* p
) // Y must be complete
391 BOOST_ASSERT(p
== 0 || p
!= px
); // catch self-reset errors
392 this_type(p
).swap(*this);
395 template<class Y
, class D
> void reset( Y
* p
, D d
)
397 this_type( p
, d
).swap( *this );
400 template<class Y
, class D
, class A
> void reset( Y
* p
, D d
, A a
)
402 this_type( p
, d
, a
).swap( *this );
405 template<class Y
> void reset( shared_ptr
<Y
> const & r
, T
* p
)
407 this_type( r
, p
).swap( *this );
410 reference
operator* () const // never throws
412 BOOST_ASSERT(px
!= 0);
416 T
* operator-> () const // never throws
418 BOOST_ASSERT(px
!= 0);
422 T
* get() const // never throws
427 // implicit conversion to "bool"
428 #include <boost/smart_ptr/detail/operator_bool.hpp>
430 bool unique() const // never throws
435 long use_count() const // never throws
437 return pn
.use_count();
440 void swap(shared_ptr
<T
> & other
) // never throws
442 std::swap(px
, other
.px
);
446 template<class Y
> bool _internal_less(shared_ptr
<Y
> const & rhs
) const
451 void * _internal_get_deleter( detail::sp_typeinfo
const & ti
) const
453 return pn
.get_deleter( ti
);
456 bool _internal_equiv( shared_ptr
const & r
) const
458 return px
== r
.px
&& pn
== r
.pn
;
461 // Tasteless as this may seem, making all members public allows member templates
462 // to work in the absence of member template friends. (Matthew Langston)
464 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
468 template<class Y
> friend class shared_ptr
;
469 template<class Y
> friend class weak_ptr
;
474 T
* px
; // contained pointer
475 boost::detail::shared_count pn
; // reference counter
479 template<class T
, class U
> inline bool operator==(shared_ptr
<T
> const & a
, shared_ptr
<U
> const & b
)
481 return a
.get() == b
.get();
484 template<class T
, class U
> inline bool operator!=(shared_ptr
<T
> const & a
, shared_ptr
<U
> const & b
)
486 return a
.get() != b
.get();
489 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
491 // Resolve the ambiguity between our op!= and the one in rel_ops
493 template<class T
> inline bool operator!=(shared_ptr
<T
> const & a
, shared_ptr
<T
> const & b
)
495 return a
.get() != b
.get();
500 template<class T
, class U
> inline bool operator<(shared_ptr
<T
> const & a
, shared_ptr
<U
> const & b
)
502 return a
._internal_less(b
);
505 template<class T
> inline void swap(shared_ptr
<T
> & a
, shared_ptr
<T
> & b
)
510 template<class T
, class U
> shared_ptr
<T
> static_pointer_cast(shared_ptr
<U
> const & r
)
512 return shared_ptr
<T
>(r
, boost::detail::static_cast_tag());
515 template<class T
, class U
> shared_ptr
<T
> const_pointer_cast(shared_ptr
<U
> const & r
)
517 return shared_ptr
<T
>(r
, boost::detail::const_cast_tag());
520 template<class T
, class U
> shared_ptr
<T
> dynamic_pointer_cast(shared_ptr
<U
> const & r
)
522 return shared_ptr
<T
>(r
, boost::detail::dynamic_cast_tag());
525 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
527 template<class T
, class U
> shared_ptr
<T
> shared_static_cast(shared_ptr
<U
> const & r
)
529 return shared_ptr
<T
>(r
, boost::detail::static_cast_tag());
532 template<class T
, class U
> shared_ptr
<T
> shared_dynamic_cast(shared_ptr
<U
> const & r
)
534 return shared_ptr
<T
>(r
, boost::detail::dynamic_cast_tag());
537 template<class T
, class U
> shared_ptr
<T
> shared_polymorphic_cast(shared_ptr
<U
> const & r
)
539 return shared_ptr
<T
>(r
, boost::detail::polymorphic_cast_tag());
542 template<class T
, class U
> shared_ptr
<T
> shared_polymorphic_downcast(shared_ptr
<U
> const & r
)
544 BOOST_ASSERT(dynamic_cast<T
*>(r
.get()) == r
.get());
545 return shared_static_cast
<T
>(r
);
548 // get_pointer() enables boost::mem_fn to recognize shared_ptr
550 template<class T
> inline T
* get_pointer(shared_ptr
<T
> const & p
)
557 #if !defined(BOOST_NO_IOSTREAM)
559 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
561 template<class Y
> std::ostream
& operator<< (std::ostream
& os
, shared_ptr
<Y
> const & p
)
569 // in STLport's no-iostreams mode no iostream symbols can be used
570 #ifndef _STLP_NO_IOSTREAMS
572 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
573 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
574 using std::basic_ostream
;
575 template<class E
, class T
, class Y
> basic_ostream
<E
, T
> & operator<< (basic_ostream
<E
, T
> & os
, shared_ptr
<Y
> const & p
)
577 template<class E
, class T
, class Y
> std::basic_ostream
<E
, T
> & operator<< (std::basic_ostream
<E
, T
> & os
, shared_ptr
<Y
> const & p
)
584 #endif // _STLP_NO_IOSTREAMS
586 #endif // __GNUC__ < 3
588 #endif // !defined(BOOST_NO_IOSTREAM)
592 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
593 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
594 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
596 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
597 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
599 template<class D
, class T
> D
* get_deleter(shared_ptr
<T
> const & p
)
601 void const * q
= p
._internal_get_deleter(BOOST_SP_TYPEID(D
));
602 return const_cast<D
*>(static_cast<D
const *>(q
));
607 template<class D
, class T
> D
* get_deleter(shared_ptr
<T
> const & p
)
609 return static_cast<D
*>(p
._internal_get_deleter(BOOST_SP_TYPEID(D
)));
616 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
618 template<class T
> inline bool atomic_is_lock_free( shared_ptr
<T
> const * /*p*/ )
623 template<class T
> shared_ptr
<T
> atomic_load( shared_ptr
<T
> const * p
)
625 boost::detail::spinlock_pool
<2>::scoped_lock
lock( p
);
629 template<class T
> inline shared_ptr
<T
> atomic_load_explicit( shared_ptr
<T
> const * p
, memory_order
/*mo*/ )
631 return atomic_load( p
);
634 template<class T
> void atomic_store( shared_ptr
<T
> * p
, shared_ptr
<T
> r
)
636 boost::detail::spinlock_pool
<2>::scoped_lock
lock( p
);
640 template<class T
> inline void atomic_store_explicit( shared_ptr
<T
> * p
, shared_ptr
<T
> r
, memory_order
/*mo*/ )
642 atomic_store( p
, r
); // std::move( r )
645 template<class T
> shared_ptr
<T
> atomic_exchange( shared_ptr
<T
> * p
, shared_ptr
<T
> r
)
647 boost::detail::spinlock
& sp
= boost::detail::spinlock_pool
<2>::spinlock_for( p
);
653 return r
; // return std::move( r )
656 template<class T
> shared_ptr
<T
> atomic_exchange_explicit( shared_ptr
<T
> * p
, shared_ptr
<T
> r
, memory_order
/*mo*/ )
658 return atomic_exchange( p
, r
); // std::move( r )
661 template<class T
> bool atomic_compare_exchange( shared_ptr
<T
> * p
, shared_ptr
<T
> * v
, shared_ptr
<T
> w
)
663 boost::detail::spinlock
& sp
= boost::detail::spinlock_pool
<2>::spinlock_for( p
);
667 if( p
->_internal_equiv( *v
) )
677 shared_ptr
<T
> tmp( *p
);
686 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*/ )
688 return atomic_compare_exchange( p
, v
, w
); // std::move( w )
696 # pragma warning(pop)
699 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
701 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED