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
133 typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
135 operator unspecified_bool_type () const
137 return p_ == 0? 0: &intrusive_ptr::get;
142 // operator! is a Borland-specific workaround
143 bool operator! () const
148 void swap(intrusive_ptr & rhs)
160 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
162 return a.get() == b.get();
165 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
167 return a.get() != b.get();
170 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
175 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
180 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
185 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
190 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
192 // Resolve the ambiguity between our op!= and the one in rel_ops
194 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
196 return a.get() != b.get();
201 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
203 return std::less<T *>()(a.get(), b.get());
206 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
213 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
218 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
220 return static_cast<T *>(p.get());
223 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
225 return dynamic_cast<T *>(p.get());
230 #if defined(__GNUC__) && (__GNUC__ < 3)
232 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
240 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
241 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
242 using std::basic_ostream;
243 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
245 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
257 # pragma warning(pop)
260 #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED