sl@0: #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED sl@0: #define BOOST_INTRUSIVE_PTR_HPP_INCLUDED sl@0: sl@0: // sl@0: // intrusive_ptr.hpp sl@0: // sl@0: // Copyright (c) 2001, 2002 Peter Dimov sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: // sl@0: // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation. sl@0: // sl@0: sl@0: #include sl@0: sl@0: #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash sl@0: # pragma warning(push) sl@0: # pragma warning(disable:4284) // odd return type for operator-> sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include // for std::less sl@0: #include // for std::basic_ostream sl@0: sl@0: sl@0: namespace boost sl@0: { sl@0: sl@0: // sl@0: // intrusive_ptr sl@0: // sl@0: // A smart pointer that uses intrusive reference counting. sl@0: // sl@0: // Relies on unqualified calls to sl@0: // sl@0: // void intrusive_ptr_add_ref(T * p); sl@0: // void intrusive_ptr_release(T * p); sl@0: // sl@0: // (p != 0) sl@0: // sl@0: // The object is responsible for destroying itself. sl@0: // sl@0: sl@0: template class intrusive_ptr sl@0: { sl@0: private: sl@0: sl@0: typedef intrusive_ptr this_type; sl@0: sl@0: public: sl@0: sl@0: typedef T element_type; sl@0: sl@0: intrusive_ptr(): p_(0) sl@0: { sl@0: } sl@0: sl@0: intrusive_ptr(T * p, bool add_ref = true): p_(p) sl@0: { sl@0: if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_); sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) sl@0: sl@0: template intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.get()) sl@0: { sl@0: if(p_ != 0) intrusive_ptr_add_ref(p_); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_) sl@0: { sl@0: if(p_ != 0) intrusive_ptr_add_ref(p_); sl@0: } sl@0: sl@0: ~intrusive_ptr() sl@0: { sl@0: if(p_ != 0) intrusive_ptr_release(p_); sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) sl@0: sl@0: template intrusive_ptr & operator=(intrusive_ptr const & rhs) sl@0: { sl@0: this_type(rhs).swap(*this); sl@0: return *this; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: intrusive_ptr & operator=(intrusive_ptr const & rhs) sl@0: { sl@0: this_type(rhs).swap(*this); sl@0: return *this; sl@0: } sl@0: sl@0: intrusive_ptr & operator=(T * rhs) sl@0: { sl@0: this_type(rhs).swap(*this); sl@0: return *this; sl@0: } sl@0: sl@0: T * get() const sl@0: { sl@0: return p_; sl@0: } sl@0: sl@0: T & operator*() const sl@0: { sl@0: return *p_; sl@0: } sl@0: sl@0: T * operator->() const sl@0: { sl@0: return p_; sl@0: } sl@0: sl@0: #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530) sl@0: sl@0: operator bool () const sl@0: { sl@0: return p_ != 0; sl@0: } sl@0: sl@0: #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) sl@0: typedef T * (this_type::*unspecified_bool_type)() const; sl@0: sl@0: operator unspecified_bool_type() const // never throws sl@0: { sl@0: return p_ == 0? 0: &this_type::get; sl@0: } sl@0: sl@0: #else sl@0: sl@0: typedef T * this_type::*unspecified_bool_type; sl@0: sl@0: operator unspecified_bool_type () const sl@0: { sl@0: return p_ == 0? 0: &this_type::p_; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: // operator! is a Borland-specific workaround sl@0: bool operator! () const sl@0: { sl@0: return p_ == 0; sl@0: } sl@0: sl@0: void swap(intrusive_ptr & rhs) sl@0: { sl@0: T * tmp = p_; sl@0: p_ = rhs.p_; sl@0: rhs.p_ = tmp; sl@0: } sl@0: sl@0: private: sl@0: sl@0: T * p_; sl@0: }; sl@0: sl@0: template inline bool operator==(intrusive_ptr const & a, intrusive_ptr const & b) sl@0: { sl@0: return a.get() == b.get(); sl@0: } sl@0: sl@0: template inline bool operator!=(intrusive_ptr const & a, intrusive_ptr const & b) sl@0: { sl@0: return a.get() != b.get(); sl@0: } sl@0: sl@0: template inline bool operator==(intrusive_ptr const & a, U * b) sl@0: { sl@0: return a.get() == b; sl@0: } sl@0: sl@0: template inline bool operator!=(intrusive_ptr const & a, U * b) sl@0: { sl@0: return a.get() != b; sl@0: } sl@0: sl@0: template inline bool operator==(T * a, intrusive_ptr const & b) sl@0: { sl@0: return a == b.get(); sl@0: } sl@0: sl@0: template inline bool operator!=(T * a, intrusive_ptr const & b) sl@0: { sl@0: return a != b.get(); sl@0: } sl@0: sl@0: #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 sl@0: sl@0: // Resolve the ambiguity between our op!= and the one in rel_ops sl@0: sl@0: template inline bool operator!=(intrusive_ptr const & a, intrusive_ptr const & b) sl@0: { sl@0: return a.get() != b.get(); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: template inline bool operator<(intrusive_ptr const & a, intrusive_ptr const & b) sl@0: { sl@0: return std::less()(a.get(), b.get()); sl@0: } sl@0: sl@0: template void swap(intrusive_ptr & lhs, intrusive_ptr & rhs) sl@0: { sl@0: lhs.swap(rhs); sl@0: } sl@0: sl@0: // mem_fn support sl@0: sl@0: template T * get_pointer(intrusive_ptr const & p) sl@0: { sl@0: return p.get(); sl@0: } sl@0: sl@0: template intrusive_ptr static_pointer_cast(intrusive_ptr const & p) sl@0: { sl@0: return static_cast(p.get()); sl@0: } sl@0: sl@0: template intrusive_ptr const_pointer_cast(intrusive_ptr const & p) sl@0: { sl@0: return const_cast(p.get()); sl@0: } sl@0: sl@0: template intrusive_ptr dynamic_pointer_cast(intrusive_ptr const & p) sl@0: { sl@0: return dynamic_cast(p.get()); sl@0: } sl@0: sl@0: // operator<< sl@0: sl@0: #if defined(__GNUC__) && (__GNUC__ < 3) sl@0: sl@0: template std::ostream & operator<< (std::ostream & os, intrusive_ptr const & p) sl@0: { sl@0: os << p.get(); sl@0: return os; sl@0: } sl@0: sl@0: #else sl@0: sl@0: // in STLport's no-iostreams mode no iostream symbols can be used sl@0: #ifndef _STLP_NO_IOSTREAMS sl@0: sl@0: # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT) sl@0: // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL sl@0: using std::basic_ostream; sl@0: template basic_ostream & operator<< (basic_ostream & os, intrusive_ptr const & p) sl@0: # else sl@0: template std::basic_ostream & operator<< (std::basic_ostream & os, intrusive_ptr const & p) sl@0: # endif sl@0: { sl@0: os << p.get(); sl@0: return os; sl@0: } sl@0: sl@0: #endif // _STLP_NO_IOSTREAMS sl@0: sl@0: #endif // __GNUC__ < 3 sl@0: sl@0: } // namespace boost sl@0: sl@0: #ifdef BOOST_MSVC sl@0: # pragma warning(pop) sl@0: #endif sl@0: sl@0: #endif // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED