mirror of
				https://github.com/boostorg/smart_ptr.git
				synced 2025-11-03 17:21:51 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
//////////////////////////////////////////////////////////////////////////////
 | 
						|
//
 | 
						|
// (C) Copyright Ion Gaztanaga 2005. 
 | 
						|
// 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)
 | 
						|
//
 | 
						|
//////////////////////////////////////////////////////////////////////////////
 | 
						|
 | 
						|
#ifndef BOOST_POINTER_CAST_HPP
 | 
						|
#define BOOST_POINTER_CAST_HPP
 | 
						|
 | 
						|
#include <memory>
 | 
						|
#include <type_traits>
 | 
						|
 | 
						|
namespace boost { 
 | 
						|
 | 
						|
//static_pointer_cast overload for raw pointers
 | 
						|
template<class T, class U>
 | 
						|
inline T* static_pointer_cast(U *ptr) noexcept
 | 
						|
{  
 | 
						|
   return static_cast<T*>(ptr);
 | 
						|
}
 | 
						|
 | 
						|
//dynamic_pointer_cast overload for raw pointers
 | 
						|
template<class T, class U>
 | 
						|
inline T* dynamic_pointer_cast(U *ptr) noexcept
 | 
						|
{  
 | 
						|
   return dynamic_cast<T*>(ptr);
 | 
						|
}
 | 
						|
 | 
						|
//const_pointer_cast overload for raw pointers
 | 
						|
template<class T, class U>
 | 
						|
inline T* const_pointer_cast(U *ptr) noexcept
 | 
						|
{  
 | 
						|
   return const_cast<T*>(ptr);
 | 
						|
}
 | 
						|
 | 
						|
//reinterpret_pointer_cast overload for raw pointers
 | 
						|
template<class T, class U>
 | 
						|
inline T* reinterpret_pointer_cast(U *ptr) noexcept
 | 
						|
{  
 | 
						|
   return reinterpret_cast<T*>(ptr);
 | 
						|
}
 | 
						|
 | 
						|
//static_pointer_cast overload for std::shared_ptr
 | 
						|
using std::static_pointer_cast;
 | 
						|
 | 
						|
//dynamic_pointer_cast overload for std::shared_ptr
 | 
						|
using std::dynamic_pointer_cast;
 | 
						|
 | 
						|
//const_pointer_cast overload for std::shared_ptr
 | 
						|
using std::const_pointer_cast;
 | 
						|
 | 
						|
//reinterpret_pointer_cast overload for std::shared_ptr
 | 
						|
template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) noexcept
 | 
						|
{
 | 
						|
    (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
 | 
						|
 | 
						|
    typedef typename std::shared_ptr<T>::element_type E;
 | 
						|
 | 
						|
    E * p = reinterpret_cast< E* >( r.get() );
 | 
						|
    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 ) noexcept
 | 
						|
{
 | 
						|
    (void) static_cast< T* >( static_cast< U* >( 0 ) );
 | 
						|
 | 
						|
    typedef typename std::unique_ptr<T>::element_type E;
 | 
						|
 | 
						|
    return std::unique_ptr<T>( static_cast<E*>( 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 ) noexcept
 | 
						|
{
 | 
						|
    (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
 | 
						|
 | 
						|
    static_assert( std::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
 | 
						|
 | 
						|
    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 ) noexcept
 | 
						|
{
 | 
						|
    (void) const_cast< T* >( static_cast< U* >( 0 ) );
 | 
						|
 | 
						|
    typedef typename std::unique_ptr<T>::element_type E;
 | 
						|
 | 
						|
    return std::unique_ptr<T>( const_cast<E*>( 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 ) noexcept
 | 
						|
{
 | 
						|
    (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
 | 
						|
 | 
						|
    typedef typename std::unique_ptr<T>::element_type E;
 | 
						|
 | 
						|
    return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
 | 
						|
}
 | 
						|
 | 
						|
} // namespace boost
 | 
						|
 | 
						|
#endif   //BOOST_POINTER_CAST_HPP
 |