mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-01 05:34:36 +02:00
Initial commit of local_shared_ptr
This commit is contained in:
109
include/boost/smart_ptr/detail/local_counted_base.hpp
Normal file
109
include/boost/smart_ptr/detail/local_counted_base.hpp
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
|
||||||
|
#define BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
|
||||||
|
|
||||||
|
// MS compatible compilers support #pragma once
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
||||||
|
# pragma once
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// detail/local_counted_base.hpp
|
||||||
|
//
|
||||||
|
// http://www.boost.org/libs/smart_ptr/
|
||||||
|
//
|
||||||
|
// Copyright 2017 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)
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <utility>
|
||||||
|
#include <climits>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
class local_counted_base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
local_counted_base( local_counted_base const & );
|
||||||
|
local_counted_base & operator= ( local_counted_base const & );
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// not 'int' or 'unsigned' to avoid aliasing and enable optimizations
|
||||||
|
enum count_type { min_ = 0, initial_ = 1, max_ = UINT_MAX };
|
||||||
|
|
||||||
|
count_type local_use_count_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
BOOST_CONSTEXPR local_counted_base() BOOST_NOEXCEPT: local_use_count_( initial_ )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~local_counted_base() BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_ref()
|
||||||
|
{
|
||||||
|
#if defined( __has_builtin )
|
||||||
|
# if __has_builtin( __builtin_assume )
|
||||||
|
|
||||||
|
__builtin_assume( local_use_count_ >= 1 );
|
||||||
|
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
local_use_count_ = static_cast<count_type>( local_use_count_ + 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
void release()
|
||||||
|
{
|
||||||
|
local_use_count_ = static_cast<count_type>( local_use_count_ - 1 );
|
||||||
|
|
||||||
|
if( local_use_count_ == 0 )
|
||||||
|
{
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long local_use_count() const BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
return local_use_count_;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class local_counted_impl: public local_counted_base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
boost::shared_ptr<void const volatile> pn_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
template<class T> explicit local_counted_impl( boost::shared_ptr<T> const& pn ): pn_( pn )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class T> explicit local_counted_impl( boost::shared_ptr<T>&& pn ): pn_( std::move(pn) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
|
517
include/boost/smart_ptr/local_shared_ptr.hpp
Normal file
517
include/boost/smart_ptr/local_shared_ptr.hpp
Normal file
@@ -0,0 +1,517 @@
|
|||||||
|
#ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
|
||||||
|
#define BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
|
||||||
|
|
||||||
|
// local_shared_ptr.hpp
|
||||||
|
//
|
||||||
|
// http://www.boost.org/libs/smart_ptr/
|
||||||
|
//
|
||||||
|
// Copyright 2017 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)
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/detail/local_counted_base.hpp>
|
||||||
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
//
|
||||||
|
// local_shared_ptr
|
||||||
|
//
|
||||||
|
// as shared_ptr, but local to a thread.
|
||||||
|
// reference count manipulations are non-atomic.
|
||||||
|
//
|
||||||
|
|
||||||
|
template<class T> class local_shared_ptr
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
typedef local_shared_ptr this_type;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef typename boost::detail::sp_element<T>::type element_type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
element_type * px;
|
||||||
|
boost::detail::local_counted_base * pn;
|
||||||
|
|
||||||
|
template<class Y> friend class local_shared_ptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
|
||||||
|
~local_shared_ptr()
|
||||||
|
{
|
||||||
|
if( pn )
|
||||||
|
{
|
||||||
|
pn->release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
BOOST_CONSTEXPR local_shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_NULLPTR )
|
||||||
|
|
||||||
|
BOOST_CONSTEXPR local_shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
explicit local_shared_ptr( Y * p ): px( p ),
|
||||||
|
pn( new boost::detail::local_counted_impl( shared_ptr<T>( p ) ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y, class D> local_shared_ptr( Y * p, D d ): px( p ),
|
||||||
|
pn( new boost::detail::local_counted_impl( shared_ptr<T>( p, d ) ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_NULLPTR )
|
||||||
|
|
||||||
|
template<class D> local_shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ),
|
||||||
|
pn( new boost::detail::local_counted_impl( shared_ptr<T>( p, d ) ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class Y, class D, class A> local_shared_ptr( Y * p, D d, A a ): px( p ),
|
||||||
|
pn( new boost::detail::local_counted_impl( shared_ptr<T>( p, d, a ) ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_NULLPTR )
|
||||||
|
|
||||||
|
template<class D, class A> local_shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ),
|
||||||
|
pn( new boost::detail::local_counted_impl( shared_ptr<T>( p, d, a ) ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// construction from shared_ptr
|
||||||
|
|
||||||
|
template<class Y> local_shared_ptr( shared_ptr<Y> const & r,
|
||||||
|
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
|
||||||
|
: px( r.get() ), pn( new boost::detail::local_counted_impl( r ) )
|
||||||
|
{
|
||||||
|
boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class Y> local_shared_ptr( shared_ptr<Y> && r,
|
||||||
|
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
|
||||||
|
: px( r.get() ), pn( new boost::detail::local_counted_impl( std::move(r) ) )
|
||||||
|
{
|
||||||
|
boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// construction from unique_ptr
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template< class Y, class D >
|
||||||
|
local_shared_ptr( std::unique_ptr< Y, D > && r )
|
||||||
|
: px( r.get() ), pn( new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) ) ) )
|
||||||
|
{
|
||||||
|
boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template< class Y, class D >
|
||||||
|
local_shared_ptr( boost::movelib::unique_ptr< Y, D > r ); // !
|
||||||
|
// : px( r.get() ), pn( new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) ) ) )
|
||||||
|
//{
|
||||||
|
// boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
//}
|
||||||
|
|
||||||
|
// copy constructor
|
||||||
|
|
||||||
|
local_shared_ptr( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
|
||||||
|
{
|
||||||
|
if( pn )
|
||||||
|
{
|
||||||
|
pn->add_ref();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// move constructor
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
local_shared_ptr( local_shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
|
||||||
|
{
|
||||||
|
r.px = 0;
|
||||||
|
r.pn = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// converting copy constructor
|
||||||
|
|
||||||
|
template<class Y> local_shared_ptr( local_shared_ptr<Y> const & r,
|
||||||
|
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
|
||||||
|
: px( r.px ), pn( r.pn )
|
||||||
|
{
|
||||||
|
boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
|
||||||
|
if( pn )
|
||||||
|
{
|
||||||
|
pn->add_ref();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// converting move constructor
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class Y> local_shared_ptr( local_shared_ptr<Y> && r,
|
||||||
|
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
|
||||||
|
: px( r.px ), pn( r.pn )
|
||||||
|
{
|
||||||
|
boost::detail::sp_assert_convertible< Y, T >();
|
||||||
|
|
||||||
|
r.px = 0;
|
||||||
|
r.pn = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// aliasing
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
local_shared_ptr( local_shared_ptr<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
|
||||||
|
{
|
||||||
|
if( pn )
|
||||||
|
{
|
||||||
|
pn->add_ref();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
local_shared_ptr( local_shared_ptr<Y> && r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
|
||||||
|
{
|
||||||
|
r.px = 0;
|
||||||
|
r.pn = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// assignment
|
||||||
|
|
||||||
|
local_shared_ptr & operator=( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr( r ).swap( *this );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y> local_shared_ptr & operator=( local_shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr( r ).swap( *this );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
local_shared_ptr & operator=( local_shared_ptr && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr( std::move( r ) ).swap( *this );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
local_shared_ptr & operator=( local_shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr( std::move( r ) ).swap( *this );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_NULLPTR )
|
||||||
|
|
||||||
|
local_shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr().swap(*this);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class Y, class D>
|
||||||
|
local_shared_ptr & operator=( std::unique_ptr<Y, D> && r )
|
||||||
|
{
|
||||||
|
local_shared_ptr( std::move(r) ).swap( *this );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class Y, class D>
|
||||||
|
local_shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r ); // !
|
||||||
|
|
||||||
|
// reset
|
||||||
|
|
||||||
|
void reset() BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
local_shared_ptr().swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y> void reset( Y * p ) // Y must be complete
|
||||||
|
{
|
||||||
|
local_shared_ptr( p ).swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y, class D> void reset( Y * p, D d )
|
||||||
|
{
|
||||||
|
local_shared_ptr( p, d ).swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y, class D, class A> void reset( Y * p, D d, A a )
|
||||||
|
{
|
||||||
|
local_shared_ptr( p, d, a ).swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Y> void reset( local_shared_ptr<Y> const & r, element_type * p )
|
||||||
|
{
|
||||||
|
local_shared_ptr( r, p ).swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class Y> void reset( local_shared_ptr<Y> && r, element_type * p )
|
||||||
|
{
|
||||||
|
local_shared_ptr( std::move( r ), p ).swap( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
|
||||||
|
typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return *px;
|
||||||
|
}
|
||||||
|
|
||||||
|
typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return px;
|
||||||
|
}
|
||||||
|
|
||||||
|
// never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
|
||||||
|
typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( px != 0 );
|
||||||
|
BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
|
||||||
|
|
||||||
|
return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
element_type * get() const BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
return px;
|
||||||
|
}
|
||||||
|
|
||||||
|
// implicit conversion to "bool"
|
||||||
|
#include <boost/smart_ptr/detail/operator_bool.hpp>
|
||||||
|
|
||||||
|
long local_use_count() const BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return pn? pn->local_use_count(): 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap
|
||||||
|
|
||||||
|
void swap( local_shared_ptr & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
std::swap( px, r.px );
|
||||||
|
std::swap( pn, r.pn );
|
||||||
|
}
|
||||||
|
|
||||||
|
// owner_before
|
||||||
|
|
||||||
|
template<class Y> bool owner_before( local_shared_ptr<Y> const & r ) const BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::less< boost::detail::local_counted_base* >()( pn, r.pn );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return a.get() == b.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return a.get() != b.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_NULLPTR )
|
||||||
|
|
||||||
|
template<class T> inline bool operator==( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return p.get() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> inline bool operator==( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return p.get() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> inline bool operator!=( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return p.get() != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return p.get() != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class T> inline void swap( local_shared_ptr<T> & a, local_shared_ptr<T> & b ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
a.swap( b );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) static_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = static_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( r, p );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) const_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = const_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( r, p );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = dynamic_cast< E* >( r.get() );
|
||||||
|
return p? local_shared_ptr<T>( r, p ): local_shared_ptr<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = reinterpret_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( r, p );
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) static_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = static_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( std::move(r), p );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) const_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = const_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( std::move(r), p );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = dynamic_cast< E* >( r.get() );
|
||||||
|
return p? local_shared_ptr<T>( std::move(r), p ): local_shared_ptr<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
|
typedef typename local_shared_ptr<T>::element_type E;
|
||||||
|
|
||||||
|
E * p = reinterpret_cast< E* >( r.get() );
|
||||||
|
return local_shared_ptr<T>( std::move(r), p );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
// get_pointer() enables boost::mem_fn to recognize local_shared_ptr
|
||||||
|
|
||||||
|
template<class T> inline typename local_shared_ptr<T>::element_type * get_pointer( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
|
||||||
|
{
|
||||||
|
return p.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// operator<<
|
||||||
|
|
||||||
|
#if !defined(BOOST_NO_IOSTREAM)
|
||||||
|
|
||||||
|
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< ( std::basic_ostream<E, T> & os, local_shared_ptr<Y> const & p )
|
||||||
|
{
|
||||||
|
os << p.get();
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BOOST_NO_IOSTREAM)
|
||||||
|
|
||||||
|
// hash_value
|
||||||
|
|
||||||
|
template< class T > struct hash;
|
||||||
|
|
||||||
|
template< class T > std::size_t hash_value( local_shared_ptr<T> const & p ) BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
return boost::hash< typename local_shared_ptr<T>::element_type* >()( p.get() );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
|
@@ -210,5 +210,7 @@ import testing ;
|
|||||||
[ compile make_shared_msvc_test.cpp ]
|
[ compile make_shared_msvc_test.cpp ]
|
||||||
|
|
||||||
[ compile lwm_win32_cs_test.cpp ]
|
[ compile lwm_win32_cs_test.cpp ]
|
||||||
|
|
||||||
|
[ run local_sp_test.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
275
test/local_sp_test.cpp
Normal file
275
test/local_sp_test.cpp
Normal file
@@ -0,0 +1,275 @@
|
|||||||
|
//
|
||||||
|
// local_sp_test.cpp
|
||||||
|
//
|
||||||
|
// Copyright 2002, 2003, 2017 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/local_shared_ptr.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
static long instances;
|
||||||
|
|
||||||
|
X()
|
||||||
|
{
|
||||||
|
++instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
~X()
|
||||||
|
{
|
||||||
|
--instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
X( X const & );
|
||||||
|
X & operator=( X const & );
|
||||||
|
};
|
||||||
|
|
||||||
|
long X::instances = 0;
|
||||||
|
|
||||||
|
class incomplete;
|
||||||
|
|
||||||
|
static void default_constructor()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<int> p;
|
||||||
|
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<void> p;
|
||||||
|
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<incomplete> p;
|
||||||
|
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<X> p;
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> static void pc0_test_()
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p( static_cast<U*>( 0 ) );
|
||||||
|
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void pc0_test()
|
||||||
|
{
|
||||||
|
pc0_test_<T, T>();
|
||||||
|
pc0_test_<T const, T const>();
|
||||||
|
pc0_test_<T volatile, T volatile>();
|
||||||
|
pc0_test_<T const volatile, T const volatile>();
|
||||||
|
|
||||||
|
pc0_test_<T const, T>();
|
||||||
|
pc0_test_<T volatile, T>();
|
||||||
|
pc0_test_<T const volatile, T>();
|
||||||
|
|
||||||
|
pc0_test_<void, T>();
|
||||||
|
pc0_test_<void const, T>();
|
||||||
|
pc0_test_<void volatile, T>();
|
||||||
|
pc0_test_<void const volatile, T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> static void pc1_test_()
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p( new U() );
|
||||||
|
|
||||||
|
BOOST_TEST( p? true: false );
|
||||||
|
BOOST_TEST( !!p );
|
||||||
|
BOOST_TEST( p.get() != 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void pc1_test()
|
||||||
|
{
|
||||||
|
pc1_test_<T, T>();
|
||||||
|
pc1_test_<T const, T const>();
|
||||||
|
pc1_test_<T volatile, T volatile>();
|
||||||
|
pc1_test_<T const volatile, T const volatile>();
|
||||||
|
|
||||||
|
pc1_test_<T const, T>();
|
||||||
|
pc1_test_<T volatile, T>();
|
||||||
|
pc1_test_<T const volatile, T>();
|
||||||
|
|
||||||
|
pc1_test_<void, T>();
|
||||||
|
pc1_test_<void const, T>();
|
||||||
|
pc1_test_<void volatile, T>();
|
||||||
|
pc1_test_<void const volatile, T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constructor()
|
||||||
|
{
|
||||||
|
pc0_test<int>();
|
||||||
|
pc0_test<X>();
|
||||||
|
|
||||||
|
pc1_test<int>();
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
pc1_test<X>();
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int m = 0;
|
||||||
|
|
||||||
|
void deleter2( int * p )
|
||||||
|
{
|
||||||
|
BOOST_TEST( p == &m );
|
||||||
|
++*p;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void deleter2_test_()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
m = 0;
|
||||||
|
boost::local_shared_ptr<T> p( &m, deleter2 );
|
||||||
|
|
||||||
|
BOOST_TEST( p? true: false );
|
||||||
|
BOOST_TEST( !!p );
|
||||||
|
BOOST_TEST( p.get() == &m );
|
||||||
|
BOOST_TEST( p.local_use_count() == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( m == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deleter_constructor()
|
||||||
|
{
|
||||||
|
deleter2_test_<int>();
|
||||||
|
deleter2_test_<int const>();
|
||||||
|
deleter2_test_<int volatile>();
|
||||||
|
deleter2_test_<int const volatile>();
|
||||||
|
|
||||||
|
deleter2_test_<void>();
|
||||||
|
deleter2_test_<void const>();
|
||||||
|
deleter2_test_<void volatile>();
|
||||||
|
deleter2_test_<void const volatile>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void test_empty( boost::local_shared_ptr<T> const & p )
|
||||||
|
{
|
||||||
|
BOOST_TEST( p? false: true );
|
||||||
|
BOOST_TEST( !p );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.local_use_count() == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void empty_copy_test()
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p1;
|
||||||
|
test_empty( p1 );
|
||||||
|
|
||||||
|
boost::local_shared_ptr<T> p2( p1 );
|
||||||
|
test_empty( p2 );
|
||||||
|
|
||||||
|
boost::local_shared_ptr<T const> p3( p1 );
|
||||||
|
test_empty( p3 );
|
||||||
|
|
||||||
|
boost::local_shared_ptr<void> p4( p1 );
|
||||||
|
test_empty( p4 );
|
||||||
|
|
||||||
|
boost::local_shared_ptr<void const> p5( p3 );
|
||||||
|
test_empty( p5 );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> static void test_nonempty_copy( boost::local_shared_ptr<U> const & p1 )
|
||||||
|
{
|
||||||
|
long k = p1.local_use_count();
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p2( p1 );
|
||||||
|
|
||||||
|
BOOST_TEST( p2.get() == p1.get() );
|
||||||
|
BOOST_TEST( p2.local_use_count() == p1.local_use_count() );
|
||||||
|
BOOST_TEST( p2.local_use_count() == k + 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( p1.local_use_count() == k );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void null_copy_test()
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p1( static_cast<T*>(0) );
|
||||||
|
|
||||||
|
test_nonempty_copy<T>( p1 );
|
||||||
|
test_nonempty_copy<T const>( p1 );
|
||||||
|
test_nonempty_copy<T volatile>( p1 );
|
||||||
|
test_nonempty_copy<T const volatile>( p1 );
|
||||||
|
test_nonempty_copy<void>( p1 );
|
||||||
|
test_nonempty_copy<void const>( p1 );
|
||||||
|
test_nonempty_copy<void volatile>( p1 );
|
||||||
|
test_nonempty_copy<void const volatile>( p1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> static void new_copy_test()
|
||||||
|
{
|
||||||
|
boost::local_shared_ptr<T> p1( new T() );
|
||||||
|
|
||||||
|
test_nonempty_copy<T>( p1 );
|
||||||
|
test_nonempty_copy<T const>( p1 );
|
||||||
|
test_nonempty_copy<T volatile>( p1 );
|
||||||
|
test_nonempty_copy<T const volatile>( p1 );
|
||||||
|
test_nonempty_copy<void>( p1 );
|
||||||
|
test_nonempty_copy<void const>( p1 );
|
||||||
|
test_nonempty_copy<void volatile>( p1 );
|
||||||
|
test_nonempty_copy<void const volatile>( p1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copy_constructor()
|
||||||
|
{
|
||||||
|
empty_copy_test<int>();
|
||||||
|
empty_copy_test<incomplete>();
|
||||||
|
empty_copy_test<X>();
|
||||||
|
|
||||||
|
null_copy_test<int>();
|
||||||
|
null_copy_test<X>();
|
||||||
|
|
||||||
|
new_copy_test<int>();
|
||||||
|
new_copy_test<X>();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
default_constructor();
|
||||||
|
pointer_constructor();
|
||||||
|
deleter_constructor();
|
||||||
|
copy_constructor();
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user