Start work on get_deleter for local_shared_ptr

This commit is contained in:
Peter Dimov
2017-06-20 19:01:16 +03:00
parent 1c097b5764
commit 6e5a382b6b
29 changed files with 326 additions and 68 deletions

View File

@ -17,7 +17,6 @@
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/config.hpp>
#include <utility>
#include <climits>

View File

@ -0,0 +1,82 @@
#ifndef BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/local_sp_deleter.hpp
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/local_counted_base.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace detail
{
template<class D> class local_sp_deleter: public local_counted_impl_em
{
private:
D d_;
public:
local_sp_deleter(): d_()
{
}
explicit local_sp_deleter( D const& d ) BOOST_SP_NOEXCEPT: d_( d )
{
}
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
explicit local_sp_deleter( D&& d ) BOOST_SP_NOEXCEPT: d_( std::move(d) )
{
}
#endif
D& deleter()
{
return d_;
}
template<class Y> void operator()( Y* p ) BOOST_SP_NOEXCEPT
{
d_( p );
}
#if !defined( BOOST_NO_CXX11_NULLPTR )
void operator()( boost::detail::sp_nullptr_t p ) BOOST_SP_NOEXCEPT
{
d_( p );
}
#endif
};
template<class D> D * get_local_deleter( local_sp_deleter<D> * p )
{
return &p->deleter();
}
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED

View File

@ -496,6 +496,11 @@ public:
return pi_? pi_->get_deleter( ti ): 0;
}
void * get_local_deleter( sp_typeinfo const & ti ) const
{
return pi_? pi_->get_local_deleter( ti ): 0;
}
void * get_untyped_deleter() const
{
return pi_? pi_->get_untyped_deleter(): 0;

View File

@ -104,6 +104,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -96,6 +96,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -98,6 +98,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -124,6 +124,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -112,6 +112,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -111,6 +111,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -135,6 +135,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -135,6 +135,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -120,6 +120,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -127,6 +127,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -59,6 +59,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -71,6 +71,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -115,6 +115,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -62,6 +62,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -84,6 +84,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -90,6 +90,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -109,6 +109,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -104,6 +104,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -67,6 +67,7 @@ public:
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()

View File

@ -50,6 +50,19 @@ void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
namespace detail
{
// get_local_deleter
template<class D> class local_sp_deleter;
template<class D> D * get_local_deleter( D * p )
{
return 0;
}
template<class D> D * get_local_deleter( local_sp_deleter<D> * p );
//
template<class X> class sp_counted_impl_p: public sp_counted_base
{
private:
@ -83,6 +96,11 @@ public:
return 0;
}
virtual void * get_local_deleter( sp_typeinfo const & )
{
return 0;
}
virtual void * get_untyped_deleter()
{
return 0;
@ -158,6 +176,11 @@ public:
return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
}
virtual void * get_local_deleter( sp_typeinfo const & ti )
{
return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( &reinterpret_cast<char&>( del ) ): 0;
}
virtual void * get_untyped_deleter()
{
return &reinterpret_cast<char&>( del );
@ -246,6 +269,11 @@ public:
return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
}
virtual void * get_local_deleter( sp_typeinfo const & ti )
{
return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( &reinterpret_cast<char&>( d_ ) ): 0;
}
virtual void * get_untyped_deleter()
{
return &reinterpret_cast<char&>( d_ );

View File

@ -11,7 +11,6 @@
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/local_counted_base.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
namespace boost
@ -22,46 +21,7 @@ template<class T> class local_shared_ptr;
namespace detail
{
template<class D> class local_sp_deleter: public local_counted_impl_em
{
private:
D d_;
public:
local_sp_deleter(): d_()
{
}
explicit local_sp_deleter( D const& d ) BOOST_SP_NOEXCEPT: d_( d )
{
}
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
explicit local_sp_deleter( D&& d ) BOOST_SP_NOEXCEPT: d_( std::move(d) )
{
}
#endif
template<class Y> void operator()( Y* p ) BOOST_SP_NOEXCEPT
{
d_( p );
}
#if !defined( BOOST_NO_CXX11_NULLPTR )
void operator()( boost::detail::sp_nullptr_t p ) BOOST_SP_NOEXCEPT
{
d_( p );
}
#endif
};
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E > * ppx, Y * p, boost::detail::local_counted_base * & pn )
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y, E >();
@ -76,7 +36,7 @@ template< class E, class Y > inline void lsp_pointer_construct( boost::local_sha
pn = pd;
}
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[] > * ppx, Y * p, boost::detail::local_counted_base * & pn )
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y[], E[] >();
@ -91,7 +51,7 @@ template< class E, class Y > inline void lsp_pointer_construct( boost::local_sha
pn = pd;
}
template< class E, std::size_t N, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[N] > * ppx, Y * p, boost::detail::local_counted_base * & pn )
template< class E, std::size_t N, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[N] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y[N], E[N] >();
@ -106,7 +66,7 @@ template< class E, std::size_t N, class Y > inline void lsp_pointer_construct( b
pn = pd;
}
template< class E, class P, class D > inline void lsp_deleter_construct( boost::local_shared_ptr< E > * ppx, P p, D const& d, boost::detail::local_counted_base * & pn )
template< class E, class P, class D > inline void lsp_deleter_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, boost::detail::local_counted_base * & pn )
{
typedef boost::detail::local_sp_deleter<D> D2;
@ -119,7 +79,7 @@ template< class E, class P, class D > inline void lsp_deleter_construct( boost::
pn = pd;
}
template< class E, class P, class D, class A > inline void lsp_allocator_construct( boost::local_shared_ptr< E > * ppx, P p, D const& d, A const& a, boost::detail::local_counted_base * & pn )
template< class E, class P, class D, class A > inline void lsp_allocator_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, A const& a, boost::detail::local_counted_base * & pn )
{
typedef boost::detail::local_sp_deleter<D> D2;
@ -702,6 +662,13 @@ template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< ( std:
#endif // !defined(BOOST_NO_IOSTREAM)
// get_deleter
template<class D, class T> D * get_deleter( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
{
return get_deleter<D>( shared_ptr<T>( p ) );
}
// hash_value
template< class T > struct hash;

View File

@ -12,7 +12,6 @@
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/local_shared_ptr.hpp>
#include <boost/smart_ptr/detail/local_counted_base.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/config.hpp>
#include <utility>

View File

@ -766,6 +766,11 @@ public:
return pn.get_deleter( ti );
}
void * _internal_get_local_deleter( boost::detail::sp_typeinfo const & ti ) const BOOST_SP_NOEXCEPT
{
return pn.get_local_deleter( ti );
}
void * _internal_get_untyped_deleter() const BOOST_SP_NOEXCEPT
{
return pn.get_untyped_deleter();
@ -980,27 +985,13 @@ template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::
namespace detail
{
#if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
// g++ 2.9x doesn't allow static_cast<X const *>(void *)
// apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p)
{
void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
return const_cast<D *>(static_cast<D const *>(q));
}
#else
template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
{
return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID(D)) );
}
#endif
template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
class esft2_deleter_wrapper
{
@ -1035,17 +1026,22 @@ public:
template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
{
D *del = boost::detail::basic_get_deleter<D>(p);
D * d = boost::detail::basic_get_deleter<D>( p );
if(del == 0)
if( d == 0 )
{
d = boost::detail::basic_get_local_deleter( d, p );
}
if( d == 0 )
{
boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
// The following get_deleter method call is fully qualified because
// older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
if(del_wrapper) del = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
}
return del;
return d;
}
// atomic access
@ -1138,6 +1134,28 @@ template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOO
} // namespace boost
#include <boost/smart_ptr/detail/local_sp_deleter.hpp>
namespace boost
{
namespace detail
{
template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
{
return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID(local_sp_deleter<D>) ) );
}
template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
{
return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID(local_sp_deleter<D>) ) );
}
} // namespace detail
} // namespace boost
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic pop
#endif

View File

@ -225,7 +225,10 @@ import testing ;
[ run lsp_array_n_test.cpp ]
[ run lsp_array_cv_test.cpp ]
[ run lsp_array_cast_test.cpp ]
[ run get_local_deleter_test.cpp ]
[ run get_local_deleter_test2.cpp ]
[ run make_local_shared_test.cpp ]
[ run make_local_shared_esft_test.cpp ]
[ run allocate_local_shared_test.cpp ]

View File

@ -0,0 +1,95 @@
//
// get_local_deleter_test.cpp
//
// Copyright 2002, 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 deleter
{
int data;
deleter(): data(0)
{
}
void operator()(void *)
{
BOOST_TEST(data == 17041);
}
};
struct deleter2
{
};
struct X
{
};
int main()
{
{
boost::local_shared_ptr<X> p;
BOOST_TEST(boost::get_deleter<void>(p) == 0);
BOOST_TEST(boost::get_deleter<void const>(p) == 0);
BOOST_TEST(boost::get_deleter<int>(p) == 0);
BOOST_TEST(boost::get_deleter<int const>(p) == 0);
BOOST_TEST(boost::get_deleter<X>(p) == 0);
BOOST_TEST(boost::get_deleter<X const>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter const>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2 const>(p) == 0);
}
{
boost::local_shared_ptr<X> p(new X);
BOOST_TEST(boost::get_deleter<void>(p) == 0);
BOOST_TEST(boost::get_deleter<void const>(p) == 0);
BOOST_TEST(boost::get_deleter<int>(p) == 0);
BOOST_TEST(boost::get_deleter<int const>(p) == 0);
BOOST_TEST(boost::get_deleter<X>(p) == 0);
BOOST_TEST(boost::get_deleter<X const>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter const>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2 const>(p) == 0);
}
{
X x;
boost::local_shared_ptr<X> p(&x, deleter());
BOOST_TEST(boost::get_deleter<void>(p) == 0);
BOOST_TEST(boost::get_deleter<void const>(p) == 0);
BOOST_TEST(boost::get_deleter<int>(p) == 0);
BOOST_TEST(boost::get_deleter<int const>(p) == 0);
BOOST_TEST(boost::get_deleter<X>(p) == 0);
BOOST_TEST(boost::get_deleter<X const>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2>(p) == 0);
BOOST_TEST(boost::get_deleter<deleter2 const>(p) == 0);
deleter * q = boost::get_deleter<deleter>(p);
BOOST_TEST(q != 0);
BOOST_TEST(q->data == 0);
q->data = 17041;
deleter const * r = boost::get_deleter<deleter const>(p);
BOOST_TEST(r == q);
BOOST_TEST(r->data == 17041);
}
return boost::report_errors();
}

View File

@ -0,0 +1,43 @@
//
// get_local_deleter_test2.cpp
//
// 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/local_shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
struct deleter;
struct X
{
};
static void test_lsp_get_deleter( boost::local_shared_ptr<X> const & p )
{
BOOST_TEST( boost::get_deleter<deleter>( p ) != 0 );
}
static void test_sp_get_deleter( boost::shared_ptr<X> const & p )
{
BOOST_TEST( boost::get_deleter<deleter>( p ) != 0 );
}
struct deleter
{
void operator()( X const * p ) { delete p; }
};
int main()
{
boost::local_shared_ptr<X> p( new X, deleter() );
test_lsp_get_deleter( p );
test_sp_get_deleter( p );
return boost::report_errors();
}