1 #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED
2 #define BOOST_INTRUSIVE_PTR_HPP_INCLUDED
7 // Copyright (c) 2001, 2002 Peter Dimov
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
16 #include <boost/config.hpp>
18 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
19 # pragma warning(push)
20 # pragma warning(disable:4284) // odd return type for operator->
23 #include <boost/assert.hpp>
24 #include <boost/detail/workaround.hpp>
25 #include <boost/detail/sp_convertible.hpp>
27 #include <boost/config/no_tr1/functional.hpp> // for std::less
29 #if !defined(BOOST_NO_IOSTREAM)
30 #if !defined(BOOST_NO_IOSFWD)
31 #include <iosfwd> // for std::basic_ostream
44 // A smart pointer that uses intrusive reference counting.
46 // Relies on unqualified calls to
48 // void intrusive_ptr_add_ref(T * p);
49 // void intrusive_ptr_release(T * p);
53 // The object is responsible for destroying itself.
56 template<class T> class intrusive_ptr
60 typedef intrusive_ptr this_type;
64 typedef T element_type;
66 intrusive_ptr(): p_(0)
70 intrusive_ptr(T * p, bool add_ref = true): p_(p)
72 if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
75 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
78 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
80 intrusive_ptr( intrusive_ptr<U> const & rhs, typename detail::sp_enable_if_convertible<U,T>::type = detail::sp_empty() )
84 intrusive_ptr( intrusive_ptr<U> const & rhs )
89 if( p_ != 0 ) intrusive_ptr_add_ref( p_ );
94 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
96 if(p_ != 0) intrusive_ptr_add_ref(p_);
101 if(p_ != 0) intrusive_ptr_release(p_);
104 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
106 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
108 this_type(rhs).swap(*this);
114 intrusive_ptr & operator=(intrusive_ptr const & rhs)
116 this_type(rhs).swap(*this);
120 intrusive_ptr & operator=(T * rhs)
122 this_type(rhs).swap(*this);
128 this_type().swap( *this );
131 void reset( T * rhs )
133 this_type( rhs ).swap( *this );
141 T & operator*() const
143 BOOST_ASSERT( p_ != 0 );
147 T * operator->() const
149 BOOST_ASSERT( p_ != 0 );
153 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
155 operator bool () const
160 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
161 typedef T * (this_type::*unspecified_bool_type)() const;
163 operator unspecified_bool_type() const // never throws
165 return p_ == 0? 0: &this_type::get;
170 typedef T * this_type::*unspecified_bool_type;
172 operator unspecified_bool_type () const
174 return p_ == 0? 0: &this_type::p_;
179 // operator! is a Borland-specific workaround
180 bool operator! () const
185 void swap(intrusive_ptr & rhs)
197 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
199 return a.get() == b.get();
202 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
204 return a.get() != b.get();
207 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
212 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
217 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
222 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
227 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
229 // Resolve the ambiguity between our op!= and the one in rel_ops
231 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
233 return a.get() != b.get();
238 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
240 return std::less<T *>()(a.get(), b.get());
243 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
250 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
255 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
257 return static_cast<T *>(p.get());
260 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
262 return const_cast<T *>(p.get());
265 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
267 return dynamic_cast<T *>(p.get());
272 #if !defined(BOOST_NO_IOSTREAM)
274 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
276 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
284 // in STLport's no-iostreams mode no iostream symbols can be used
285 #ifndef _STLP_NO_IOSTREAMS
287 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
288 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
289 using std::basic_ostream;
290 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
292 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
299 #endif // _STLP_NO_IOSTREAMS
301 #endif // __GNUC__ < 3
303 #endif // !defined(BOOST_NO_IOSTREAM)
308 # pragma warning(pop)
311 #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED