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/ 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>
22 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
24 #include <boost/config/no_tr1/functional.hpp> // for std::less
26 #if !defined(BOOST_NO_IOSTREAM)
27 #if !defined(BOOST_NO_IOSFWD)
28 #include <iosfwd> // for std::basic_ostream
41 // A smart pointer that uses intrusive reference counting.
43 // Relies on unqualified calls to
45 // void intrusive_ptr_add_ref(T * p);
46 // void intrusive_ptr_release(T * p);
50 // The object is responsible for destroying itself.
53 template<class T> class intrusive_ptr
57 typedef intrusive_ptr this_type;
61 typedef T element_type;
63 BOOST_CONSTEXPR intrusive_ptr() BOOST_SP_NOEXCEPT : px( 0 )
67 intrusive_ptr( T * p, bool add_ref = true ): px( p )
69 if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
72 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
75 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
77 intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
81 intrusive_ptr( intrusive_ptr<U> const & rhs )
86 if( px != 0 ) intrusive_ptr_add_ref( px );
91 intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
93 if( px != 0 ) intrusive_ptr_add_ref( px );
98 if( px != 0 ) intrusive_ptr_release( px );
101 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
103 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
105 this_type(rhs).swap(*this);
113 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
115 intrusive_ptr(intrusive_ptr && rhs) BOOST_SP_NOEXCEPT : px( rhs.px )
120 intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_SP_NOEXCEPT
122 this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
126 template<class U> friend class intrusive_ptr;
129 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
131 intrusive_ptr(intrusive_ptr<U> && rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty())
135 intrusive_ptr(intrusive_ptr<U> && rhs)
144 intrusive_ptr & operator=(intrusive_ptr<U> && rhs) BOOST_SP_NOEXCEPT
146 this_type( static_cast< intrusive_ptr<U> && >( rhs ) ).swap(*this);
152 intrusive_ptr & operator=(intrusive_ptr const & rhs)
154 this_type(rhs).swap(*this);
158 intrusive_ptr & operator=(T * rhs)
160 this_type(rhs).swap(*this);
166 this_type().swap( *this );
169 void reset( T * rhs )
171 this_type( rhs ).swap( *this );
174 void reset( T * rhs, bool add_ref )
176 this_type( rhs, add_ref ).swap( *this );
179 T * get() const BOOST_SP_NOEXCEPT
184 T * detach() BOOST_SP_NOEXCEPT
191 T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
193 BOOST_ASSERT( px != 0 );
197 T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
199 BOOST_ASSERT( px != 0 );
203 // implicit conversion to "bool"
204 #include <boost/smart_ptr/detail/operator_bool.hpp>
206 void swap(intrusive_ptr & rhs) BOOST_SP_NOEXCEPT
218 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) BOOST_SP_NOEXCEPT
220 return a.get() == b.get();
223 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) BOOST_SP_NOEXCEPT
225 return a.get() != b.get();
228 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b) BOOST_SP_NOEXCEPT
233 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b) BOOST_SP_NOEXCEPT
238 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b) BOOST_SP_NOEXCEPT
243 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b) BOOST_SP_NOEXCEPT
248 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
250 // Resolve the ambiguity between our op!= and the one in rel_ops
252 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) BOOST_SP_NOEXCEPT
254 return a.get() != b.get();
259 #if !defined( BOOST_NO_CXX11_NULLPTR )
261 template<class T> inline bool operator==( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
266 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_SP_NOEXCEPT
271 template<class T> inline bool operator!=( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
276 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_SP_NOEXCEPT
283 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) BOOST_SP_NOEXCEPT
285 return std::less<T *>()(a.get(), b.get());
288 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs) BOOST_SP_NOEXCEPT
295 template<class T> T * get_pointer(intrusive_ptr<T> const & p) BOOST_SP_NOEXCEPT
300 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
302 return static_cast<T *>(p.get());
305 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
307 return const_cast<T *>(p.get());
310 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
312 return dynamic_cast<T *>(p.get());
317 #if !defined(BOOST_NO_IOSTREAM)
319 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
321 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
329 // in STLport's no-iostreams mode no iostream symbols can be used
330 #ifndef _STLP_NO_IOSTREAMS
332 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
333 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
334 using std::basic_ostream;
335 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
337 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
344 #endif // _STLP_NO_IOSTREAMS
346 #endif // __GNUC__ < 3
348 #endif // !defined(BOOST_NO_IOSTREAM)
352 template< class T > struct hash;
354 template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p ) BOOST_SP_NOEXCEPT
356 return boost::hash< T* >()( p.get() );
361 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED