Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
1 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SHARED_PTR_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001-2006 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/detail/shared_ptr_nmt.hpp>
23 #include <memory> // for std::auto_ptr
25 #include <boost/assert.hpp>
26 #include <boost/checked_delete.hpp>
27 #include <boost/throw_exception.hpp>
28 #include <boost/detail/shared_count.hpp>
29 #include <boost/detail/workaround.hpp>
31 #include <algorithm> // for std::swap
32 #include <functional> // for std::less
33 #include <typeinfo> // for std::bad_cast
34 #include <iosfwd> // for std::basic_ostream
36 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
37 # pragma warning(push)
38 # pragma warning(disable:4284) // odd return type for operator->
44 template<class T> class weak_ptr;
45 template<class T> class enable_shared_from_this;
50 struct static_cast_tag {};
51 struct const_cast_tag {};
52 struct dynamic_cast_tag {};
53 struct polymorphic_cast_tag {};
55 template<class T> struct shared_ptr_traits
57 typedef T & reference;
60 template<> struct shared_ptr_traits<void>
62 typedef void reference;
65 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
67 template<> struct shared_ptr_traits<void const>
69 typedef void reference;
72 template<> struct shared_ptr_traits<void volatile>
74 typedef void reference;
77 template<> struct shared_ptr_traits<void const volatile>
79 typedef void reference;
84 // enable_shared_from_this support
86 template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )
88 if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
92 // Turn off: the last argument of the varargs function "sp_enable_shared_from_this" is unnamed
93 # pragma set woff 3506
96 inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
101 # pragma reset woff 3506
104 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
106 // rvalue auto_ptr support based on a technique by Dave Abrahams
108 template< class T, class R > struct sp_enable_if_auto_ptr
112 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
119 } // namespace detail
125 // An enhanced relative of scoped_ptr with reference counted copy semantics.
126 // The object pointed to is deleted when the last shared_ptr pointing to it
127 // is destroyed or reset.
130 template<class T> class shared_ptr
134 // Borland 5.5.1 specific workaround
135 typedef shared_ptr<T> this_type;
139 typedef T element_type;
140 typedef T value_type;
142 typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
144 shared_ptr(): px(0), pn() // never throws in 1.30+
149 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
151 boost::detail::sp_enable_shared_from_this( pn, p, p );
155 // Requirements: D's copy constructor must not throw
157 // shared_ptr will release p by calling d(p)
160 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
162 boost::detail::sp_enable_shared_from_this( pn, p, p );
165 // As above, but with allocator. A's copy constructor shall not throw.
167 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
169 boost::detail::sp_enable_shared_from_this( pn, p, p );
172 // generated copy constructor, assignment, destructor are fine...
174 // except that Borland C++ has a bug, and g++ with -Wsynth warns
175 #if defined(__BORLANDC__) || defined(__GNUC__)
177 shared_ptr & operator=(shared_ptr const & r) // never throws
180 pn = r.pn; // shared_count::op= doesn't throw
187 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
189 // it is now safe to copy r.px, as pn(r.pn) did not throw
194 shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
199 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
204 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
209 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
211 if(px == 0) // need to allocate new counter -- the cast failed
213 pn = boost::detail::shared_count();
218 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
222 boost::throw_exception(std::bad_cast());
226 #ifndef BOOST_NO_AUTO_PTR
229 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
232 pn = boost::detail::shared_count(r);
233 boost::detail::sp_enable_shared_from_this( pn, tmp, tmp );
236 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
239 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
241 typename Ap::element_type * tmp = r.get();
242 pn = boost::detail::shared_count( r );
243 boost::detail::sp_enable_shared_from_this( pn, tmp, tmp );
247 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
249 #endif // BOOST_NO_AUTO_PTR
251 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
254 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
257 pn = r.pn; // shared_count::op= doesn't throw
263 #ifndef BOOST_NO_AUTO_PTR
266 shared_ptr & operator=( std::auto_ptr<Y> & r )
268 this_type(r).swap(*this);
272 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
275 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
277 this_type( r ).swap( *this );
282 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
284 #endif // BOOST_NO_AUTO_PTR
286 void reset() // never throws in 1.30+
288 this_type().swap(*this);
291 template<class Y> void reset(Y * p) // Y must be complete
293 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
294 this_type(p).swap(*this);
297 template<class Y, class D> void reset( Y * p, D d )
299 this_type( p, d ).swap( *this );
302 template<class Y, class D, class A> void reset( Y * p, D d, A a )
304 this_type( p, d, a ).swap( *this );
307 reference operator* () const // never throws
309 BOOST_ASSERT(px != 0);
313 T * operator-> () const // never throws
315 BOOST_ASSERT(px != 0);
319 T * get() const // never throws
324 // implicit conversion to "bool"
326 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
328 operator bool () const
333 #elif defined( _MANAGED )
335 static void unspecified_bool( this_type*** )
339 typedef void (*unspecified_bool_type)( this_type*** );
341 operator unspecified_bool_type() const // never throws
343 return px == 0? 0: unspecified_bool;
347 ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
348 ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) )
350 typedef T * (this_type::*unspecified_bool_type)() const;
352 operator unspecified_bool_type() const // never throws
354 return px == 0? 0: &this_type::get;
359 typedef T * this_type::*unspecified_bool_type;
361 operator unspecified_bool_type() const // never throws
363 return px == 0? 0: &this_type::px;
368 // operator! is redundant, but some compilers need it
370 bool operator! () const // never throws
375 bool unique() const // never throws
380 long use_count() const // never throws
382 return pn.use_count();
385 void swap(shared_ptr<T> & other) // never throws
387 std::swap(px, other.px);
391 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
396 void * _internal_get_deleter(std::type_info const & ti) const
398 return pn.get_deleter(ti);
401 // Tasteless as this may seem, making all members public allows member templates
402 // to work in the absence of member template friends. (Matthew Langston)
404 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
408 template<class Y> friend class shared_ptr;
409 template<class Y> friend class weak_ptr;
414 T * px; // contained pointer
415 boost::detail::shared_count pn; // reference counter
419 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
421 return a.get() == b.get();
424 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
426 return a.get() != b.get();
429 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
431 // Resolve the ambiguity between our op!= and the one in rel_ops
433 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
435 return a.get() != b.get();
440 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
442 return a._internal_less(b);
445 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
450 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
452 return shared_ptr<T>(r, boost::detail::static_cast_tag());
455 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
457 return shared_ptr<T>(r, boost::detail::const_cast_tag());
460 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
462 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
465 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
467 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
469 return shared_ptr<T>(r, boost::detail::static_cast_tag());
472 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
474 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
477 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
479 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
482 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
484 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
485 return shared_static_cast<T>(r);
488 // get_pointer() enables boost::mem_fn to recognize shared_ptr
490 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
497 #if defined(__GNUC__) && (__GNUC__ < 3)
499 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
507 // in STLport's no-iostreams mode no iostream symbols can be used
508 #ifndef _STLP_NO_IOSTREAMS
510 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
511 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
512 using std::basic_ostream;
513 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
515 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
522 #endif // _STLP_NO_IOSTREAMS
524 #endif // __GNUC__ < 3
526 // get_deleter (experimental)
528 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
529 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
530 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
532 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
533 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
535 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
537 void const * q = p._internal_get_deleter(typeid(D));
538 return const_cast<D *>(static_cast<D const *>(q));
543 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
545 return static_cast<D *>(p._internal_get_deleter(typeid(D)));
553 # pragma warning(pop)
556 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
558 #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED