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 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
18 # pragma warning(push)
19 # pragma warning(disable:4284) // odd return type for operator->
22 #include <boost/assert.hpp>
23 #include <boost/detail/workaround.hpp>
25 #include <functional> // for std::less
26 #include <iosfwd> // for std::basic_ostream
35 // A smart pointer that uses intrusive reference counting.
37 // Relies on unqualified calls to
39 // void intrusive_ptr_add_ref(T * p);
40 // void intrusive_ptr_release(T * p);
44 // The object is responsible for destroying itself.
47 template<class T> class intrusive_ptr
51 typedef intrusive_ptr this_type;
55 typedef T element_type;
57 intrusive_ptr(): p_(0)
61 intrusive_ptr(T * p, bool add_ref = true): p_(p)
63 if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
66 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
68 template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
70 if(p_ != 0) intrusive_ptr_add_ref(p_);
75 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
77 if(p_ != 0) intrusive_ptr_add_ref(p_);
82 if(p_ != 0) intrusive_ptr_release(p_);
85 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
87 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
89 this_type(rhs).swap(*this);
95 intrusive_ptr & operator=(intrusive_ptr const & rhs)
97 this_type(rhs).swap(*this);
101 intrusive_ptr & operator=(T * rhs)
103 this_type(rhs).swap(*this);
112 T & operator*() const
117 T * operator->() const
122 typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
124 operator unspecified_bool_type () const
126 return p_ == 0? 0: &intrusive_ptr::get;
129 // operator! is a Borland-specific workaround
130 bool operator! () const
135 void swap(intrusive_ptr & rhs)
147 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
149 return a.get() == b.get();
152 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
154 return a.get() != b.get();
157 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
162 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
167 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
172 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
177 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
179 // Resolve the ambiguity between our op!= and the one in rel_ops
181 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
183 return a.get() != b.get();
188 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
190 return std::less<T *>()(a.get(), b.get());
193 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
200 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
205 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
207 return static_cast<T *>(p.get());
210 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
212 return dynamic_cast<T *>(p.get());
217 #if defined(__GNUC__) && (__GNUC__ < 3)
219 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
227 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
228 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
229 using std::basic_ostream;
230 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
232 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
244 # pragma warning(pop)
247 #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED