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 #include <boost/assert.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/smart_ptr/detail/sp_convertible.hpp>
22 #include <boost/config/no_tr1/functional.hpp> // for std::less
24 #if !defined(BOOST_NO_IOSTREAM)
25 #if !defined(BOOST_NO_IOSFWD)
26 #include <iosfwd> // for std::basic_ostream
39 // A smart pointer that uses intrusive reference counting.
41 // Relies on unqualified calls to
43 // void intrusive_ptr_add_ref(T * p);
44 // void intrusive_ptr_release(T * p);
48 // The object is responsible for destroying itself.
51 template<class T> class intrusive_ptr
55 typedef intrusive_ptr this_type;
59 typedef T element_type;
61 intrusive_ptr(): px( 0 )
65 intrusive_ptr( T * p, bool add_ref = true ): px( p )
67 if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
70 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
73 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
75 intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
79 intrusive_ptr( intrusive_ptr<U> const & rhs )
84 if( px != 0 ) intrusive_ptr_add_ref( px );
89 intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
91 if( px != 0 ) intrusive_ptr_add_ref( px );
96 if( px != 0 ) intrusive_ptr_release( px );
99 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
101 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
103 this_type(rhs).swap(*this);
111 #if defined( BOOST_HAS_RVALUE_REFS )
113 intrusive_ptr(intrusive_ptr && rhs): px( rhs.px )
118 intrusive_ptr & operator=(intrusive_ptr && rhs)
120 this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
126 intrusive_ptr & operator=(intrusive_ptr const & rhs)
128 this_type(rhs).swap(*this);
132 intrusive_ptr & operator=(T * rhs)
134 this_type(rhs).swap(*this);
140 this_type().swap( *this );
143 void reset( T * rhs )
145 this_type( rhs ).swap( *this );
153 T & operator*() const
155 BOOST_ASSERT( px != 0 );
159 T * operator->() const
161 BOOST_ASSERT( px != 0 );
165 // implicit conversion to "bool"
166 #include <boost/smart_ptr/detail/operator_bool.hpp>
168 void swap(intrusive_ptr & rhs)
180 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
182 return a.get() == b.get();
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, U * b)
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==(T * a, intrusive_ptr<U> const & b)
205 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
210 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
212 // Resolve the ambiguity between our op!= and the one in rel_ops
214 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
216 return a.get() != b.get();
221 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
223 return std::less<T *>()(a.get(), b.get());
226 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
233 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
238 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
240 return static_cast<T *>(p.get());
243 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
245 return const_cast<T *>(p.get());
248 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
250 return dynamic_cast<T *>(p.get());
255 #if !defined(BOOST_NO_IOSTREAM)
257 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
259 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
267 // in STLport's no-iostreams mode no iostream symbols can be used
268 #ifndef _STLP_NO_IOSTREAMS
270 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
271 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
272 using std::basic_ostream;
273 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
275 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
282 #endif // _STLP_NO_IOSTREAMS
284 #endif // __GNUC__ < 3
286 #endif // !defined(BOOST_NO_IOSTREAM)
290 template< class T > struct hash;
292 template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p )
294 return boost::hash< T* >()( p.get() );
299 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED