1 #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
\r
7 // Copyright (c) 2001, 2002 Peter Dimov
\r
9 // Distributed under the Boost Software License, Version 1.0. (See
\r
10 // accompanying file LICENSE_1_0.txt or copy at
\r
11 // http://www.boost.org/LICENSE_1_0.txt)
\r
13 // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
\r
16 #include <boost/config.hpp>
\r
18 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
\r
19 # pragma warning(push)
\r
20 # pragma warning(disable:4284) // odd return type for operator->
\r
23 #include <boost/assert.hpp>
\r
24 #include <boost/detail/workaround.hpp>
\r
25 #include <boost/smart_ptr/detail/sp_convertible.hpp>
\r
27 #include <boost/config/no_tr1/functional.hpp> // for std::less
\r
29 #if !defined(BOOST_NO_IOSTREAM)
\r
30 #if !defined(BOOST_NO_IOSFWD)
\r
31 #include <iosfwd> // for std::basic_ostream
\r
44 // A smart pointer that uses intrusive reference counting.
\r
46 // Relies on unqualified calls to
\r
48 // void intrusive_ptr_add_ref(T * p);
\r
49 // void intrusive_ptr_release(T * p);
\r
53 // The object is responsible for destroying itself.
\r
56 template<class T> class intrusive_ptr
\r
60 typedef intrusive_ptr this_type;
\r
64 typedef T element_type;
\r
66 intrusive_ptr(): px( 0 )
\r
70 intrusive_ptr( T * p, bool add_ref = true ): px( p )
\r
72 if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
\r
75 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
\r
78 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
80 intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
\r
84 intrusive_ptr( intrusive_ptr<U> const & rhs )
\r
89 if( px != 0 ) intrusive_ptr_add_ref( px );
\r
94 intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
\r
96 if( px != 0 ) intrusive_ptr_add_ref( px );
\r
101 if( px != 0 ) intrusive_ptr_release( px );
\r
104 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
\r
106 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
\r
108 this_type(rhs).swap(*this);
\r
116 #if defined( BOOST_HAS_RVALUE_REFS )
\r
118 intrusive_ptr(intrusive_ptr && rhs): px( rhs.px )
\r
123 intrusive_ptr & operator=(intrusive_ptr && rhs)
\r
125 this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
\r
131 intrusive_ptr & operator=(intrusive_ptr const & rhs)
\r
133 this_type(rhs).swap(*this);
\r
137 intrusive_ptr & operator=(T * rhs)
\r
139 this_type(rhs).swap(*this);
\r
145 this_type().swap( *this );
\r
148 void reset( T * rhs )
\r
150 this_type( rhs ).swap( *this );
\r
158 T & operator*() const
\r
160 BOOST_ASSERT( px != 0 );
\r
164 T * operator->() const
\r
166 BOOST_ASSERT( px != 0 );
\r
170 // implicit conversion to "bool"
\r
171 #include <boost/smart_ptr/detail/operator_bool.hpp>
\r
173 void swap(intrusive_ptr & rhs)
\r
185 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
\r
187 return a.get() == b.get();
\r
190 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
\r
192 return a.get() != b.get();
\r
195 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
\r
197 return a.get() == b;
\r
200 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
\r
202 return a.get() != b;
\r
205 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
\r
207 return a == b.get();
\r
210 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
\r
212 return a != b.get();
\r
215 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
\r
217 // Resolve the ambiguity between our op!= and the one in rel_ops
\r
219 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
\r
221 return a.get() != b.get();
\r
226 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
\r
228 return std::less<T *>()(a.get(), b.get());
\r
231 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
\r
238 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
\r
243 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
\r
245 return static_cast<T *>(p.get());
\r
248 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
\r
250 return const_cast<T *>(p.get());
\r
253 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
\r
255 return dynamic_cast<T *>(p.get());
\r
260 #if !defined(BOOST_NO_IOSTREAM)
\r
262 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
\r
264 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
\r
272 // in STLport's no-iostreams mode no iostream symbols can be used
\r
273 #ifndef _STLP_NO_IOSTREAMS
\r
275 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
\r
276 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
\r
277 using std::basic_ostream;
\r
278 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
\r
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)
\r
287 #endif // _STLP_NO_IOSTREAMS
\r
289 #endif // __GNUC__ < 3
\r
291 #endif // !defined(BOOST_NO_IOSTREAM)
\r
293 } // namespace boost
\r
296 # pragma warning(pop)
\r
299 #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
\r