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>
21 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
23 #include <boost/config/no_tr1/functional.hpp> // for std::less
25 #if !defined(BOOST_NO_IOSTREAM)
26 #if !defined(BOOST_NO_IOSFWD)
27 #include <iosfwd> // for std::basic_ostream
40 // A smart pointer that uses intrusive reference counting.
42 // Relies on unqualified calls to
44 // void intrusive_ptr_add_ref(T * p);
45 // void intrusive_ptr_release(T * p);
49 // The object is responsible for destroying itself.
52 template<class T> class intrusive_ptr
56 typedef intrusive_ptr this_type;
60 typedef T element_type;
62 intrusive_ptr() BOOST_NOEXCEPT : px( 0 )
66 intrusive_ptr( T * p, bool add_ref = true ): px( p )
68 if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
71 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
74 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
76 intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
80 intrusive_ptr( intrusive_ptr<U> const & rhs )
85 if( px != 0 ) intrusive_ptr_add_ref( px );
90 intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
92 if( px != 0 ) intrusive_ptr_add_ref( px );
97 if( px != 0 ) intrusive_ptr_release( px );
100 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
102 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
104 this_type(rhs).swap(*this);
112 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
114 intrusive_ptr(intrusive_ptr && rhs) BOOST_NOEXCEPT : px( rhs.px )
119 intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_NOEXCEPT
121 this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
127 intrusive_ptr & operator=(intrusive_ptr const & rhs)
129 this_type(rhs).swap(*this);
133 intrusive_ptr & operator=(T * rhs)
135 this_type(rhs).swap(*this);
139 void reset() BOOST_NOEXCEPT
141 this_type().swap( *this );
144 void reset( T * rhs )
146 this_type( rhs ).swap( *this );
149 T * get() const BOOST_NOEXCEPT
154 T & operator*() const
156 BOOST_ASSERT( px != 0 );
160 T * operator->() const
162 BOOST_ASSERT( px != 0 );
166 // implicit conversion to "bool"
167 #include <boost/smart_ptr/detail/operator_bool.hpp>
169 void swap(intrusive_ptr & rhs) BOOST_NOEXCEPT
181 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
183 return a.get() == b.get();
186 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
188 return a.get() != b.get();
191 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
196 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
201 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
206 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
211 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
213 // Resolve the ambiguity between our op!= and the one in rel_ops
215 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
217 return a.get() != b.get();
222 #if !defined( BOOST_NO_CXX11_NULLPTR )
224 template<class T> inline bool operator==( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
229 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
234 template<class T> inline bool operator!=( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
239 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
246 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
248 return std::less<T *>()(a.get(), b.get());
251 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
258 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
263 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
265 return static_cast<T *>(p.get());
268 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
270 return const_cast<T *>(p.get());
273 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
275 return dynamic_cast<T *>(p.get());
280 #if !defined(BOOST_NO_IOSTREAM)
282 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
284 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
292 // in STLport's no-iostreams mode no iostream symbols can be used
293 #ifndef _STLP_NO_IOSTREAMS
295 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
296 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
297 using std::basic_ostream;
298 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
300 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
307 #endif // _STLP_NO_IOSTREAMS
309 #endif // __GNUC__ < 3
311 #endif // !defined(BOOST_NO_IOSTREAM)
315 template< class T > struct hash;
317 template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p )
319 return boost::hash< T* >()( p.get() );
324 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED