1 #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_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/smart_ptr/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(): px( 0 )
70 intrusive_ptr( T * p, bool add_ref = true ): px( p )
72 if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
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 boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
84 intrusive_ptr( intrusive_ptr<U> const & rhs )
89 if( px != 0 ) intrusive_ptr_add_ref( px );
94 intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
96 if( px != 0 ) intrusive_ptr_add_ref( px );
101 if( px != 0 ) intrusive_ptr_release( px );
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);
116 #if defined( BOOST_HAS_RVALUE_REFS )
118 intrusive_ptr(intrusive_ptr && rhs): px( rhs.px )
123 intrusive_ptr & operator=(intrusive_ptr && rhs)
125 this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
131 intrusive_ptr & operator=(intrusive_ptr const & rhs)
133 this_type(rhs).swap(*this);
137 intrusive_ptr & operator=(T * rhs)
139 this_type(rhs).swap(*this);
145 this_type().swap( *this );
148 void reset( T * rhs )
150 this_type( rhs ).swap( *this );
158 T & operator*() const
160 BOOST_ASSERT( px != 0 );
164 T * operator->() const
166 BOOST_ASSERT( px != 0 );
170 // implicit conversion to "bool"
171 #include <boost/smart_ptr/detail/operator_bool.hpp>
173 void swap(intrusive_ptr & rhs)
185 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
187 return a.get() == b.get();
190 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
192 return a.get() != b.get();
195 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
200 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
205 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
210 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
215 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
217 // Resolve the ambiguity between our op!= and the one in rel_ops
219 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
221 return a.get() != b.get();
226 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
228 return std::less<T *>()(a.get(), b.get());
231 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
238 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
243 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
245 return static_cast<T *>(p.get());
248 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
250 return const_cast<T *>(p.get());
253 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
255 return dynamic_cast<T *>(p.get());
260 #if !defined(BOOST_NO_IOSTREAM)
262 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
264 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
272 // in STLport's no-iostreams mode no iostream symbols can be used
273 #ifndef _STLP_NO_IOSTREAMS
275 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
276 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
277 using std::basic_ostream;
278 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
280 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
287 #endif // _STLP_NO_IOSTREAMS
289 #endif // __GNUC__ < 3
291 #endif // !defined(BOOST_NO_IOSTREAM)
296 # pragma warning(pop)
299 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED