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 <functional> // std::less
30 // A smart pointer that uses intrusive reference counting.
32 // Relies on unqualified calls to
34 // void intrusive_ptr_add_ref(T * p);
35 // void intrusive_ptr_release(T * p);
39 // The object is responsible for destroying itself.
42 template<class T> class intrusive_ptr
46 typedef intrusive_ptr this_type;
50 intrusive_ptr(): p_(0)
54 intrusive_ptr(T * p): p_(p)
56 if(p_ != 0) intrusive_ptr_add_ref(p_);
61 if(p_ != 0) intrusive_ptr_release(p_);
64 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
66 template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
68 if(p_ != 0) intrusive_ptr_add_ref(p_);
73 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
75 if(p_ != 0) intrusive_ptr_add_ref(p_);
78 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
80 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
82 this_type(rhs).swap(*this);
88 intrusive_ptr & operator=(intrusive_ptr const & rhs)
90 this_type(rhs).swap(*this);
94 intrusive_ptr & operator=(T * rhs)
96 this_type(rhs).swap(*this);
100 void swap(intrusive_ptr & rhs)
112 T & operator*() const
117 T * operator->() const
127 typedef bool (intrusive_ptr::*bool_type) () const;
129 operator bool_type () const
131 return p_ == 0? 0: &intrusive_ptr::empty;
139 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
144 template<class T, class U> intrusive_ptr<T> shared_dynamic_cast(intrusive_ptr<U> const & p)
146 return dynamic_cast<T *>(p.get());
149 template<class T, class U> intrusive_ptr<T> shared_static_cast(intrusive_ptr<U> const & p)
151 return static_cast<T *>(p.get());
154 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
156 return a.get() == b.get();
159 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
161 return a.get() != b.get();
164 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
166 return std::less<T *>(a.get(), b.get());
169 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
174 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
179 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
184 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
191 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
199 # pragma warning(pop)
202 #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED