Files

408 lines
9.6 KiB
C++

#ifndef ACTIV_BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
#define ACTIV_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 <ActivBoost/config.hpp>
#include <ActivBoost/assert.hpp>
#include <ActivBoost/config/workaround.hpp>
#include <ActivBoost/smart_ptr/detail/sp_convertible.hpp>
#include <ActivBoost/smart_ptr/detail/sp_nullptr_t.hpp>
#include <ActivBoost/smart_ptr/detail/sp_noexcept.hpp>
#include <ActivBoost/config/no_tr1/functional.hpp> // for std::less
#if !defined(ACTIV_BOOST_NO_IOSTREAM)
#if !defined(ACTIV_BOOST_NO_IOSFWD)
#include <iosfwd> // for std::basic_ostream
#else
#include <ostream>
#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 T> class intrusive_ptr
{
private:
typedef intrusive_ptr this_type;
public:
typedef T element_type;
ACTIV_BOOST_CONSTEXPR intrusive_ptr() ACTIV_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(ACTIV_BOOST_NO_MEMBER_TEMPLATES) || defined(ACTIV_BOOST_MSVC6_MEMBER_TEMPLATES)
template<class U>
#if !defined( ACTIV_BOOST_SP_NO_SP_CONVERTIBLE )
intrusive_ptr( intrusive_ptr<U> const & rhs, typename Boost::detail::sp_enable_if_convertible<U,T>::type = Boost::detail::sp_empty() )
#else
intrusive_ptr( intrusive_ptr<U> 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(ACTIV_BOOST_NO_MEMBER_TEMPLATES) || defined(ACTIV_BOOST_MSVC6_MEMBER_TEMPLATES)
template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
{
this_type(rhs).swap(*this);
return *this;
}
#endif
// Move support
#if !defined( ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES )
intrusive_ptr(intrusive_ptr && rhs) ACTIV_BOOST_SP_NOEXCEPT : px( rhs.px )
{
rhs.px = 0;
}
intrusive_ptr & operator=(intrusive_ptr && rhs) ACTIV_BOOST_SP_NOEXCEPT
{
this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
return *this;
}
template<class U> friend class intrusive_ptr;
template<class U>
#if !defined( ACTIV_BOOST_SP_NO_SP_CONVERTIBLE )
intrusive_ptr(intrusive_ptr<U> && rhs, typename Boost::detail::sp_enable_if_convertible<U,T>::type = Boost::detail::sp_empty())
#else
intrusive_ptr(intrusive_ptr<U> && rhs)
#endif
: px( rhs.px )
{
rhs.px = 0;
}
template<class U>
intrusive_ptr & operator=(intrusive_ptr<U> && rhs) ACTIV_BOOST_SP_NOEXCEPT
{
this_type( static_cast< intrusive_ptr<U> && >( 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 ACTIV_BOOST_SP_NOEXCEPT
{
return px;
}
T * detach() ACTIV_BOOST_SP_NOEXCEPT
{
T * ret = px;
px = 0;
return ret;
}
T & operator*() const ACTIV_BOOST_SP_NOEXCEPT_WITH_ASSERT
{
ACTIV_BOOST_ASSERT( px != 0 );
return *px;
}
T * operator->() const ACTIV_BOOST_SP_NOEXCEPT_WITH_ASSERT
{
ACTIV_BOOST_ASSERT( px != 0 );
return px;
}
// implicit conversion to "bool"
#include <ActivBoost/smart_ptr/detail/operator_bool.hpp>
void swap(intrusive_ptr & rhs) ACTIV_BOOST_SP_NOEXCEPT
{
T * tmp = px;
px = rhs.px;
rhs.px = tmp;
}
private:
T * px;
};
template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) ACTIV_BOOST_SP_NOEXCEPT
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) ACTIV_BOOST_SP_NOEXCEPT
{
return a.get() != b.get();
}
template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b) ACTIV_BOOST_SP_NOEXCEPT
{
return a.get() == b;
}
template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b) ACTIV_BOOST_SP_NOEXCEPT
{
return a.get() != b;
}
template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b) ACTIV_BOOST_SP_NOEXCEPT
{
return a == b.get();
}
template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b) ACTIV_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<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) ACTIV_BOOST_SP_NOEXCEPT
{
return a.get() != b.get();
}
#endif
#if !defined( ACTIV_BOOST_NO_CXX11_NULLPTR )
template<class T> inline bool operator==( intrusive_ptr<T> const & p, Boost::detail::sp_nullptr_t ) ACTIV_BOOST_SP_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator==( Boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) ACTIV_BOOST_SP_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator!=( intrusive_ptr<T> const & p, Boost::detail::sp_nullptr_t ) ACTIV_BOOST_SP_NOEXCEPT
{
return p.get() != 0;
}
template<class T> inline bool operator!=( Boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) ACTIV_BOOST_SP_NOEXCEPT
{
return p.get() != 0;
}
#endif
template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) ACTIV_BOOST_SP_NOEXCEPT
{
return std::less<T *>()(a.get(), b.get());
}
template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs) ACTIV_BOOST_SP_NOEXCEPT
{
lhs.swap(rhs);
}
// mem_fn support
template<class T> T * get_pointer(intrusive_ptr<T> const & p) ACTIV_BOOST_SP_NOEXCEPT
{
return p.get();
}
// pointer casts
template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
{
return static_cast<T *>(p.get());
}
template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
{
return const_cast<T *>(p.get());
}
template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
{
return dynamic_cast<T *>(p.get());
}
#if !defined( ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES )
template<class T, class U> intrusive_ptr<T> static_pointer_cast( intrusive_ptr<U> && p ) ACTIV_BOOST_SP_NOEXCEPT
{
return intrusive_ptr<T>( static_cast<T*>( p.detach() ), false );
}
template<class T, class U> intrusive_ptr<T> const_pointer_cast( intrusive_ptr<U> && p ) ACTIV_BOOST_SP_NOEXCEPT
{
return intrusive_ptr<T>( const_cast<T*>( p.detach() ), false );
}
template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast( intrusive_ptr<U> && p ) ACTIV_BOOST_SP_NOEXCEPT
{
T * p2 = dynamic_cast<T*>( p.get() );
intrusive_ptr<T> r( p2, false );
if( p2 ) p.detach();
return r;
}
#endif // defined( ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES )
// operator<<
#if !defined(ACTIV_BOOST_NO_IOSTREAM)
#if defined(ACTIV_BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> 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(ACTIV_BOOST_MSVC) && ACTIV_BOOST_WORKAROUND(ACTIV_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<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
# else
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
# endif
{
os << p.get();
return os;
}
#endif // _STLP_NO_IOSTREAMS
#endif // __GNUC__ < 3
#endif // !defined(ACTIV_BOOST_NO_IOSTREAM)
// hash_value
template< class T > struct hash;
template< class T > std::size_t hash_value( Boost::intrusive_ptr<T> const & p ) ACTIV_BOOST_SP_NOEXCEPT
{
return Boost::hash< T* >()( p.get() );
}
} // namespace Boost
// std::hash
#if !defined(ACTIV_BOOST_NO_CXX11_HDR_FUNCTIONAL)
namespace std
{
template<class T> struct hash< ::Boost::intrusive_ptr<T> >
{
std::size_t operator()( ::Boost::intrusive_ptr<T> const & p ) const ACTIV_BOOST_SP_NOEXCEPT
{
return std::hash< T* >()( p.get() );
}
};
} // namespace std
#endif // #if !defined(ACTIV_BOOST_NO_CXX11_HDR_FUNCTIONAL)
#endif // #ifndef ACTIV_BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED