1 #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED
2 #define BOOST_INTRUSIVE_PTR_HPP_INCLUDED
7 // Copyright (c) 2001, 2002 Peter Dimov
9 // Permission to copy, use, modify, sell and distribute this software
10 // is granted provided this copyright notice appears in all copies.
11 // This software is provided "as is" without express or implied
12 // warranty, and with no claim as to its suitability for any purpose.
14 // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
17 #include <boost/config.hpp>
19 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
20 # pragma warning(push)
21 # pragma warning(disable:4284) // odd return type for operator->
24 #include <boost/assert.hpp>
25 #include <boost/detail/workaround.hpp>
27 #include <functional> // for std::less
28 #include <iosfwd> // for std::basic_ostream
37 // A smart pointer that uses intrusive reference counting.
39 // Relies on unqualified calls to
41 // void intrusive_ptr_add_ref(T * p);
42 // void intrusive_ptr_release(T * p);
46 // The object is responsible for destroying itself.
49 template<class T> class intrusive_ptr
53 typedef intrusive_ptr this_type;
57 typedef T element_type;
59 intrusive_ptr(): p_(0)
63 intrusive_ptr(T * p, bool add_ref = true): p_(p)
65 if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
68 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
70 template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
72 if(p_ != 0) intrusive_ptr_add_ref(p_);
77 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
79 if(p_ != 0) intrusive_ptr_add_ref(p_);
84 if(p_ != 0) intrusive_ptr_release(p_);
87 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
89 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
91 this_type(rhs).swap(*this);
97 intrusive_ptr & operator=(intrusive_ptr const & rhs)
99 this_type(rhs).swap(*this);
103 intrusive_ptr & operator=(T * rhs)
105 this_type(rhs).swap(*this);
114 T & operator*() const
119 T * operator->() const
124 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
126 operator bool () const
131 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
132 typedef T * (this_type::*unspecified_bool_type)() const;
134 operator unspecified_bool_type() const // never throws
136 return p_ == 0? 0: &this_type::get;
141 typedef T * this_type::*unspecified_bool_type;
143 operator unspecified_bool_type () const
145 return p_ == 0? 0: &this_type::p_;
150 // operator! is a Borland-specific workaround
151 bool operator! () const
156 void swap(intrusive_ptr & rhs)
168 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
170 return a.get() == b.get();
173 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
175 return a.get() != b.get();
178 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
183 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
188 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
193 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
198 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
200 // Resolve the ambiguity between our op!= and the one in rel_ops
202 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
204 return a.get() != b.get();
209 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
211 return std::less<T *>()(a.get(), b.get());
214 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
221 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
226 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
228 return static_cast<T *>(p.get());
231 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
233 return const_cast<T *>(p.get());
236 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
238 return dynamic_cast<T *>(p.get());
243 #if defined(__GNUC__) && (__GNUC__ < 3)
245 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
253 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
254 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
255 using std::basic_ostream;
256 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
258 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
270 # pragma warning(pop)
273 #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED