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 BOOST_CONSTEXPR 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);
125 template<class U> friend class intrusive_ptr;
128 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
130 intrusive_ptr(intrusive_ptr<U> && rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty())
134 intrusive_ptr(intrusive_ptr<U> && rhs)
143 intrusive_ptr & operator=(intrusive_ptr<U> && rhs) BOOST_NOEXCEPT
145 this_type( static_cast< intrusive_ptr<U> && >( rhs ) ).swap(*this);
151 intrusive_ptr & operator=(intrusive_ptr const & rhs)
153 this_type(rhs).swap(*this);
157 intrusive_ptr & operator=(T * rhs)
159 this_type(rhs).swap(*this);
163 void reset() BOOST_NOEXCEPT
165 this_type().swap( *this );
168 void reset( T * rhs )
170 this_type( rhs ).swap( *this );
173 void reset( T * rhs, bool add_ref )
175 this_type( rhs, add_ref ).swap( *this );
178 T * get() const BOOST_NOEXCEPT
183 T * detach() BOOST_NOEXCEPT
190 T & operator*() const
192 BOOST_ASSERT( px != 0 );
196 T * operator->() const
198 BOOST_ASSERT( px != 0 );
202 // implicit conversion to "bool"
203 #include <boost/smart_ptr/detail/operator_bool.hpp>
205 void swap(intrusive_ptr & rhs) BOOST_NOEXCEPT
217 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
219 return a.get() == b.get();
222 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
224 return a.get() != b.get();
227 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
232 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
237 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
242 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
247 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
249 // Resolve the ambiguity between our op!= and the one in rel_ops
251 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
253 return a.get() != b.get();
258 #if !defined( BOOST_NO_CXX11_NULLPTR )
260 template<class T> inline bool operator==( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
265 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
270 template<class T> inline bool operator!=( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
275 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
282 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
284 return std::less<T *>()(a.get(), b.get());
287 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
294 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
299 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
301 return static_cast<T *>(p.get());
304 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
306 return const_cast<T *>(p.get());
309 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
311 return dynamic_cast<T *>(p.get());
316 #if !defined(BOOST_NO_IOSTREAM)
318 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
320 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
328 // in STLport's no-iostreams mode no iostream symbols can be used
329 #ifndef _STLP_NO_IOSTREAMS
331 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
332 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
333 using std::basic_ostream;
334 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
336 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
343 #endif // _STLP_NO_IOSTREAMS
345 #endif // __GNUC__ < 3
347 #endif // !defined(BOOST_NO_IOSTREAM)
351 template< class T > struct hash;
353 template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p )
355 return boost::hash< T* >()( p.get() );
360 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED