pointer_casts with move semantics for unique_ptr

This commit is contained in:
Karolin Varner
2015-12-17 15:32:30 +01:00
parent 6b787f1cec
commit ce52fb1045
3 changed files with 222 additions and 15 deletions

View File

@ -7,6 +7,8 @@
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/config.hpp>
#ifndef BOOST_POINTER_CAST_HPP
#define BOOST_POINTER_CAST_HPP
@ -44,10 +46,27 @@ inline T* reinterpret_pointer_cast(U *ptr)
#if !defined( BOOST_NO_CXX11_SMART_PTR )
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/type_traits/is_pod.hpp>
#include <boost/type_traits/has_virtual_destructor.hpp>
#include <memory>
namespace boost {
namespace detail {
template<class T, class U>
void assert_safe_moving_upcast() {
BOOST_STATIC_ASSERT_MSG( !(boost::is_base_of<T, U>::value && !boost::is_pod<U>::value && !boost::has_virtual_destructor<T>::value)
, "Upcast from a non-POD child to a base without virtual destructor is unsafe, because the child's destructor "
"will not be called when the base pointer is deleted. Consider using shared_ptr for such types.");
}
}
//static_pointer_cast overload for std::shared_ptr
using std::static_pointer_cast;
@ -68,6 +87,35 @@ template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std
return std::shared_ptr<T>( r, p );
}
//static_pointer_cast overload for std::unique_ptr
template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
{
detail::assert_safe_moving_upcast<T, U>();
return std::unique_ptr<T>( static_cast<T*>( r.release() ) );
}
//dynamic_pointer_cast overload for std::unique_ptr
template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
{
detail::assert_safe_moving_upcast<T, U>();
T * p = dynamic_cast<T*>( r.get() );
if( p ) r.release();
return std::unique_ptr<T>( p );
}
//const_pointer_cast overload for std::unique_ptr
template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
{
return std::unique_ptr<T>( const_cast<T*>( r.release() ) );
}
//reinterpret_pointer_cast overload for std::unique_ptr
template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
{
return std::unique_ptr<T>( reinterpret_cast<T*>( r.release() ) );
}
} // namespace boost
#endif // #if !defined( BOOST_NO_CXX11_SMART_PTR )