1 #define DEBUG_SHARED_PTR
2 #ifndef DEBUG_SHARED_PTR
4 #include <boost/shared_ptr.hpp>
8 #include "pbd/stacktrace.h"
10 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
11 #define BOOST_SHARED_PTR_HPP_INCLUDED
16 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
17 // Copyright (c) 2001, 2002, 2003 Peter Dimov
19 // Distributed under the Boost Software License, Version 1.0. (See
20 // accompanying file LICENSE_1_0.txt or copy at
21 // http://www.boost.org/LICENSE_1_0.txt)
23 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
26 #include "pbd/stacktrace.h"
28 #include <boost/config.hpp> // for broken compiler workarounds
30 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
31 #include <boost/detail/shared_ptr_nmt.hpp>
34 #include <boost/assert.hpp>
35 #include <boost/checked_delete.hpp>
36 #include <boost/throw_exception.hpp>
37 #include <boost/detail/shared_count.hpp>
38 #include <boost/detail/workaround.hpp>
40 #include <memory> // for std::auto_ptr
41 #include <algorithm> // for std::swap
42 #include <functional> // for std::less
43 #include <typeinfo> // for std::bad_cast
44 #include <iosfwd> // for std::basic_ostream
46 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
47 # pragma warning(push)
48 # pragma warning(disable:4284) // odd return type for operator->
54 template<class T> class weak_ptr;
55 template<class T> class enable_shared_from_this;
60 struct static_cast_tag {};
61 struct const_cast_tag {};
62 struct dynamic_cast_tag {};
63 struct polymorphic_cast_tag {};
65 template<class T> struct shared_ptr_traits
67 typedef T & reference;
70 template<> struct shared_ptr_traits<void>
72 typedef void reference;
75 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
77 template<> struct shared_ptr_traits<void const>
79 typedef void reference;
82 template<> struct shared_ptr_traits<void volatile>
84 typedef void reference;
87 template<> struct shared_ptr_traits<void const volatile>
89 typedef void reference;
94 // enable_shared_from_this support
96 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 )
98 if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
101 inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
105 } // namespace detail
111 // An enhanced relative of scoped_ptr with reference counted copy semantics.
112 // The object pointed to is deleted when the last shared_ptr pointing to it
113 // is destroyed or reset.
116 template<class T> class shared_ptr
120 // Borland 5.5.1 specific workaround
121 typedef shared_ptr<T> this_type;
125 typedef T element_type;
126 typedef T value_type;
128 typedef typename detail::shared_ptr_traits<T>::reference reference;
130 shared_ptr(): px(0), pn() // never throws in 1.30+
135 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
137 detail::sp_enable_shared_from_this( pn, p, p );
141 // Requirements: D's copy constructor must not throw
143 // shared_ptr will release p by calling d(p)
146 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
148 detail::sp_enable_shared_from_this( pn, p, p );
151 // generated copy constructor, assignment, destructor are fine...
153 // except that Borland C++ has a bug, and g++ with -Wsynth warns
154 #if defined(__BORLANDC__) || defined(__GNUC__)
156 shared_ptr & operator=(shared_ptr const & r) // never throws
159 pn = r.pn; // shared_count::op= doesn't throw
166 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
168 // it is now safe to copy r.px, as pn(r.pn) did not throw
173 shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
178 shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
183 shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
188 shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
190 if(px == 0) // need to allocate new counter -- the cast failed
192 pn = detail::shared_count();
197 shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
201 boost::throw_exception(std::bad_cast());
205 #ifndef BOOST_NO_AUTO_PTR
208 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
211 pn = detail::shared_count(r);
212 detail::sp_enable_shared_from_this( pn, tmp, tmp );
217 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
220 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
223 pn = r.pn; // shared_count::op= doesn't throw
229 #ifndef BOOST_NO_AUTO_PTR
232 shared_ptr & operator=(std::auto_ptr<Y> & r)
234 this_type(r).swap(*this);
240 void reset() // never throws in 1.30+
242 this_type().swap(*this);
245 template<class Y> void reset(Y * p) // Y must be complete
247 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
248 this_type(p).swap(*this);
251 template<class Y, class D> void reset(Y * p, D d)
253 this_type(p, d).swap(*this);
256 reference operator* () const // never throws
258 BOOST_ASSERT(px != 0);
262 T * operator-> () const // never throws
264 BOOST_ASSERT(px != 0);
268 T * get() const // never throws
273 // implicit conversion to "bool"
275 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
277 operator bool () const
283 ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
284 ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) )
286 typedef T * (this_type::*unspecified_bool_type)() const;
288 operator unspecified_bool_type() const // never throws
290 return px == 0? 0: &this_type::get;
295 typedef T * this_type::*unspecified_bool_type;
297 operator unspecified_bool_type() const // never throws
299 return px == 0? 0: &this_type::px;
304 // operator! is redundant, but some compilers need it
306 bool operator! () const // never throws
311 bool unique() const // never throws
316 long use_count() const // never throws
318 return pn.use_count();
321 void swap(shared_ptr<T> & other) // never throws
323 std::swap(px, other.px);
327 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
332 void * _internal_get_deleter(std::type_info const & ti) const
334 return pn.get_deleter(ti);
337 // Tasteless as this may seem, making all members public allows member templates
338 // to work in the absence of member template friends. (Matthew Langston)
340 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
344 template<class Y> friend class shared_ptr;
345 template<class Y> friend class weak_ptr;
349 T * px; // contained pointer
350 detail::shared_count pn; // reference counter
351 typename PBD::thing_with_backtrace<T> bt; // backtrace
355 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
357 return a.get() == b.get();
360 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
362 return a.get() != b.get();
365 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
367 // Resolve the ambiguity between our op!= and the one in rel_ops
369 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
371 return a.get() != b.get();
376 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
378 return a._internal_less(b);
381 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
386 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
388 return shared_ptr<T>(r, detail::static_cast_tag());
391 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
393 return shared_ptr<T>(r, detail::const_cast_tag());
396 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
398 return shared_ptr<T>(r, detail::dynamic_cast_tag());
401 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
403 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
405 return shared_ptr<T>(r, detail::static_cast_tag());
408 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
410 return shared_ptr<T>(r, detail::dynamic_cast_tag());
413 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
415 return shared_ptr<T>(r, detail::polymorphic_cast_tag());
418 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
420 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
421 return shared_static_cast<T>(r);
424 // get_pointer() enables boost::mem_fn to recognize shared_ptr
426 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
433 #if defined(__GNUC__) && (__GNUC__ < 3)
435 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
443 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
444 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
445 using std::basic_ostream;
446 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
448 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
457 // get_deleter (experimental)
459 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
460 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
461 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
463 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
464 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
466 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
468 void const * q = p._internal_get_deleter(typeid(D));
469 return const_cast<D *>(static_cast<D const *>(q));
474 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
476 return static_cast<D *>(p._internal_get_deleter(typeid(D)));
484 # pragma warning(pop)
487 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
489 #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
491 #endif // #ifndef DEBUG_SHARED_PTR