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, 2002, 2003 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 <boost/assert.hpp>
24 #include <boost/checked_delete.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <boost/detail/shared_count.hpp>
27 #include <boost/detail/workaround.hpp>
29 #include <memory> // for std::auto_ptr
30 #include <algorithm> // for std::swap
31 #include <functional> // for std::less
32 #include <typeinfo> // for std::bad_cast
33 #include <iosfwd> // for std::basic_ostream
35 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
36 # pragma warning(push)
37 # pragma warning(disable:4284) // odd return type for operator->
43 template<class T> class weak_ptr;
44 template<class T> class enable_shared_from_this;
49 struct static_cast_tag {};
50 struct const_cast_tag {};
51 struct dynamic_cast_tag {};
52 struct polymorphic_cast_tag {};
54 template<class T> struct shared_ptr_traits
56 typedef T & reference;
59 template<> struct shared_ptr_traits<void>
61 typedef void reference;
64 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
66 template<> struct shared_ptr_traits<void const>
68 typedef void reference;
71 template<> struct shared_ptr_traits<void volatile>
73 typedef void reference;
76 template<> struct shared_ptr_traits<void const volatile>
78 typedef void reference;
83 // enable_shared_from_this support
85 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 )
87 if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
90 inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
100 // An enhanced relative of scoped_ptr with reference counted copy semantics.
101 // The object pointed to is deleted when the last shared_ptr pointing to it
102 // is destroyed or reset.
105 template<class T> class shared_ptr
109 // Borland 5.5.1 specific workaround
110 typedef shared_ptr<T> this_type;
114 typedef T element_type;
115 typedef T value_type;
117 typedef typename detail::shared_ptr_traits<T>::reference reference;
119 shared_ptr(): px(0), pn() // never throws in 1.30+
124 explicit shared_ptr(Y * p): px(p), pn(p, checked_deleter<Y>()) // Y must be complete
126 detail::sp_enable_shared_from_this( pn, p, p );
130 // Requirements: D's copy constructor must not throw
132 // shared_ptr will release p by calling d(p)
135 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
137 detail::sp_enable_shared_from_this( pn, p, p );
140 // generated copy constructor, assignment, destructor are fine...
142 // except that Borland C++ has a bug, and g++ with -Wsynth warns
143 #if defined(__BORLANDC__) || defined(__GNUC__)
145 shared_ptr & operator=(shared_ptr const & r) // never throws
148 pn = r.pn; // shared_count::op= doesn't throw
155 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
157 // it is now safe to copy r.px, as pn(r.pn) did not throw
162 shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
167 shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
172 shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
177 shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
179 if(px == 0) // need to allocate new counter -- the cast failed
181 pn = detail::shared_count();
186 shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
190 boost::throw_exception(std::bad_cast());
194 #ifndef BOOST_NO_AUTO_PTR
197 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
200 pn = detail::shared_count(r);
201 detail::sp_enable_shared_from_this( pn, tmp, tmp );
206 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
209 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
212 pn = r.pn; // shared_count::op= doesn't throw
218 #ifndef BOOST_NO_AUTO_PTR
221 shared_ptr & operator=(std::auto_ptr<Y> & r)
223 this_type(r).swap(*this);
229 void reset() // never throws in 1.30+
231 this_type().swap(*this);
234 template<class Y> void reset(Y * p) // Y must be complete
236 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
237 this_type(p).swap(*this);
240 template<class Y, class D> void reset(Y * p, D d)
242 this_type(p, d).swap(*this);
245 reference operator* () const // never throws
247 BOOST_ASSERT(px != 0);
251 T * operator-> () const // never throws
253 BOOST_ASSERT(px != 0);
257 T * get() const // never throws
262 // implicit conversion to "bool"
264 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
266 operator bool () const
271 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
272 typedef T * (this_type::*unspecified_bool_type)() const;
274 operator unspecified_bool_type() const // never throws
276 return px == 0? 0: &this_type::get;
281 typedef T * this_type::*unspecified_bool_type;
283 operator unspecified_bool_type() const // never throws
285 return px == 0? 0: &this_type::px;
290 // operator! is redundant, but some compilers need it
292 bool operator! () const // never throws
297 bool unique() const // never throws
302 long use_count() const // never throws
304 return pn.use_count();
307 void swap(shared_ptr<T> & other) // never throws
309 std::swap(px, other.px);
313 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
318 void * _internal_get_deleter(std::type_info const & ti) const
320 return pn.get_deleter(ti);
323 // Tasteless as this may seem, making all members public allows member templates
324 // to work in the absence of member template friends. (Matthew Langston)
326 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
330 template<class Y> friend class shared_ptr;
331 template<class Y> friend class weak_ptr;
336 T * px; // contained pointer
337 detail::shared_count pn; // reference counter
341 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
343 return a.get() == b.get();
346 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
348 return a.get() != b.get();
351 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
353 // Resolve the ambiguity between our op!= and the one in rel_ops
355 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
357 return a.get() != b.get();
362 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
364 return a._internal_less(b);
367 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
372 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
374 return shared_ptr<T>(r, detail::static_cast_tag());
377 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
379 return shared_ptr<T>(r, detail::const_cast_tag());
382 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
384 return shared_ptr<T>(r, detail::dynamic_cast_tag());
387 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
389 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
391 return shared_ptr<T>(r, detail::static_cast_tag());
394 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
396 return shared_ptr<T>(r, detail::dynamic_cast_tag());
399 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
401 return shared_ptr<T>(r, detail::polymorphic_cast_tag());
404 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
406 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
407 return shared_static_cast<T>(r);
410 // get_pointer() enables boost::mem_fn to recognize shared_ptr
412 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
419 #if defined(__GNUC__) && (__GNUC__ < 3)
421 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
429 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
430 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
431 using std::basic_ostream;
432 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
434 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
443 // get_deleter (experimental)
445 #if (defined(__GNUC__) && (__GNUC__ < 3)) || (defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238))
447 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
448 // apparently EDG 2.38 also doesn't accept it
450 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
452 void const * q = p._internal_get_deleter(typeid(D));
453 return const_cast<D *>(static_cast<D const *>(q));
458 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
460 return static_cast<D *>(p._internal_get_deleter(typeid(D)));
468 # pragma warning(pop)
471 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
473 #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED