////////////////////////////////////////////////////////////////////////////// // // (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 namespace boost { //static_pointer_cast overload for raw pointers template inline T* static_pointer_cast(U *ptr) { return static_cast(ptr); } //dynamic_pointer_cast overload for raw pointers template inline T* dynamic_pointer_cast(U *ptr) { return dynamic_cast(ptr); } //const_pointer_cast overload for raw pointers template inline T* const_pointer_cast(U *ptr) { return const_cast(ptr); } //reinterpret_pointer_cast overload for raw pointers template inline T* reinterpret_pointer_cast(U *ptr) { return reinterpret_cast(ptr); } } // namespace boost #if !defined( BOOST_NO_CXX11_SMART_PTR ) #include #include #include namespace boost { //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 std::shared_ptr reinterpret_pointer_cast(const std::shared_ptr & r ) BOOST_NOEXCEPT { (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); typedef typename std::shared_ptr::element_type E; E * p = reinterpret_cast< E* >( r.get() ); return std::shared_ptr( r, p ); } //static_pointer_cast overload for std::unique_ptr template std::unique_ptr static_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { (void) static_cast< T* >( static_cast< U* >( 0 ) ); typedef typename std::unique_ptr::element_type E; return std::unique_ptr( static_cast( r.release() ) ); } //dynamic_pointer_cast overload for std::unique_ptr template std::unique_ptr dynamic_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor::value, "The target of dynamic_pointer_cast must have a virtual destructor." ); T * p = dynamic_cast( r.get() ); if( p ) r.release(); return std::unique_ptr( p ); } //const_pointer_cast overload for std::unique_ptr template std::unique_ptr const_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { (void) const_cast< T* >( static_cast< U* >( 0 ) ); typedef typename std::unique_ptr::element_type E; return std::unique_ptr( const_cast( r.release() ) ); } //reinterpret_pointer_cast overload for std::unique_ptr template std::unique_ptr reinterpret_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); typedef typename std::unique_ptr::element_type E; return std::unique_ptr( reinterpret_cast( r.release() ) ); } } // namespace boost #endif // #if !defined( BOOST_NO_CXX11_SMART_PTR ) #endif //BOOST_POINTER_CAST_HPP