#ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED #define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED // // intrusive_ptr.hpp // // Copyright (c) 2001, 2002 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/smart_ptr/ for documentation. // #include #include #include #include #include #include #include // for std::less #if !defined(BOOST_NO_IOSTREAM) #if !defined(BOOST_NO_IOSFWD) #include // for std::basic_ostream #else #include #endif #endif namespace boost { // // intrusive_ptr // // A smart pointer that uses intrusive reference counting. // // Relies on unqualified calls to // // void intrusive_ptr_add_ref(T * p); // void intrusive_ptr_release(T * p); // // (p != 0) // // The object is responsible for destroying itself. // template class intrusive_ptr { private: typedef intrusive_ptr this_type; public: typedef T element_type; BOOST_CONSTEXPR intrusive_ptr() BOOST_SP_NOEXCEPT : px( 0 ) { } intrusive_ptr( T * p, bool add_ref = true ): px( p ) { if( px != 0 && add_ref ) intrusive_ptr_add_ref( px ); } #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) template #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) intrusive_ptr( intrusive_ptr const & rhs, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) #else intrusive_ptr( intrusive_ptr const & rhs ) #endif : px( rhs.get() ) { if( px != 0 ) intrusive_ptr_add_ref( px ); } #endif intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px ) { if( px != 0 ) intrusive_ptr_add_ref( px ); } ~intrusive_ptr() { if( px != 0 ) intrusive_ptr_release( px ); } #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) template intrusive_ptr & operator=(intrusive_ptr const & rhs) { this_type(rhs).swap(*this); return *this; } #endif // Move support #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) intrusive_ptr(intrusive_ptr && rhs) BOOST_SP_NOEXCEPT : px( rhs.px ) { rhs.px = 0; } intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_SP_NOEXCEPT { this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this); return *this; } template friend class intrusive_ptr; template #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) intrusive_ptr(intrusive_ptr && rhs, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty()) #else intrusive_ptr(intrusive_ptr && rhs) #endif : px( rhs.px ) { rhs.px = 0; } template intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_SP_NOEXCEPT { this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this); return *this; } #endif intrusive_ptr & operator=(intrusive_ptr const & rhs) { this_type(rhs).swap(*this); return *this; } intrusive_ptr & operator=(T * rhs) { this_type(rhs).swap(*this); return *this; } void reset() { this_type().swap( *this ); } void reset( T * rhs ) { this_type( rhs ).swap( *this ); } void reset( T * rhs, bool add_ref ) { this_type( rhs, add_ref ).swap( *this ); } T * get() const BOOST_SP_NOEXCEPT { return px; } T * detach() BOOST_SP_NOEXCEPT { T * ret = px; px = 0; return ret; } T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT { BOOST_ASSERT( px != 0 ); return *px; } T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT { BOOST_ASSERT( px != 0 ); return px; } // implicit conversion to "bool" #include void swap(intrusive_ptr & rhs) BOOST_SP_NOEXCEPT { T * tmp = px; px = rhs.px; rhs.px = tmp; } private: T * px; }; template inline bool operator==(intrusive_ptr const & a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return a.get() == b.get(); } template inline bool operator!=(intrusive_ptr const & a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return a.get() != b.get(); } template inline bool operator==(intrusive_ptr const & a, U * b) BOOST_SP_NOEXCEPT { return a.get() == b; } template inline bool operator!=(intrusive_ptr const & a, U * b) BOOST_SP_NOEXCEPT { return a.get() != b; } template inline bool operator==(T * a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return a == b.get(); } template inline bool operator!=(T * a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return a != b.get(); } #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 // Resolve the ambiguity between our op!= and the one in rel_ops template inline bool operator!=(intrusive_ptr const & a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return a.get() != b.get(); } #endif #if !defined( BOOST_NO_CXX11_NULLPTR ) template inline bool operator==( intrusive_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT { return p.get() == 0; } template inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr const & p ) BOOST_SP_NOEXCEPT { return p.get() == 0; } template inline bool operator!=( intrusive_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT { return p.get() != 0; } template inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr const & p ) BOOST_SP_NOEXCEPT { return p.get() != 0; } #endif template inline bool operator<(intrusive_ptr const & a, intrusive_ptr const & b) BOOST_SP_NOEXCEPT { return std::less()(a.get(), b.get()); } template void swap(intrusive_ptr & lhs, intrusive_ptr & rhs) BOOST_SP_NOEXCEPT { lhs.swap(rhs); } // mem_fn support template T * get_pointer(intrusive_ptr const & p) BOOST_SP_NOEXCEPT { return p.get(); } // pointer casts template intrusive_ptr static_pointer_cast(intrusive_ptr const & p) { return static_cast(p.get()); } template intrusive_ptr const_pointer_cast(intrusive_ptr const & p) { return const_cast(p.get()); } template intrusive_ptr dynamic_pointer_cast(intrusive_ptr const & p) { return dynamic_cast(p.get()); } #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) template intrusive_ptr static_pointer_cast( intrusive_ptr && p ) BOOST_SP_NOEXCEPT { return intrusive_ptr( static_cast( p.detach() ), false ); } template intrusive_ptr const_pointer_cast( intrusive_ptr && p ) BOOST_SP_NOEXCEPT { return intrusive_ptr( const_cast( p.detach() ), false ); } template intrusive_ptr dynamic_pointer_cast( intrusive_ptr && p ) BOOST_SP_NOEXCEPT { T * p2 = dynamic_cast( p.get() ); intrusive_ptr r( p2, false ); if( p2 ) p.detach(); return r; } #endif // defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) // operator<< #if !defined(BOOST_NO_IOSTREAM) #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) ) template std::ostream & operator<< (std::ostream & os, intrusive_ptr const & p) { os << p.get(); return os; } #else // in STLport's no-iostreams mode no iostream symbols can be used #ifndef _STLP_NO_IOSTREAMS # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT) // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL using std::basic_ostream; template basic_ostream & operator<< (basic_ostream & os, intrusive_ptr const & p) # else template std::basic_ostream & operator<< (std::basic_ostream & os, intrusive_ptr const & p) # endif { os << p.get(); return os; } #endif // _STLP_NO_IOSTREAMS #endif // __GNUC__ < 3 #endif // !defined(BOOST_NO_IOSTREAM) // hash_value template< class T > struct hash; template< class T > std::size_t hash_value( boost::intrusive_ptr const & p ) BOOST_SP_NOEXCEPT { return boost::hash< T* >()( p.get() ); } } // namespace boost // std::hash #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) namespace std { template struct hash< ::boost::intrusive_ptr > { std::size_t operator()( ::boost::intrusive_ptr const & p ) const BOOST_SP_NOEXCEPT { return std::hash< T* >()( p.get() ); } }; } // namespace std #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED