Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/pointer_cast.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005. 
0004 // Distributed under the Boost Software License, Version 1.0. 
0005 // (See accompanying file LICENSE_1_0.txt or copy at 
0006 //  http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 //////////////////////////////////////////////////////////////////////////////
0009 
0010 #ifndef BOOST_POINTER_CAST_HPP
0011 #define BOOST_POINTER_CAST_HPP
0012 
0013 #include <memory>
0014 #include <type_traits>
0015 
0016 namespace boost { 
0017 
0018 //static_pointer_cast overload for raw pointers
0019 template<class T, class U>
0020 inline T* static_pointer_cast(U *ptr) noexcept
0021 {  
0022    return static_cast<T*>(ptr);
0023 }
0024 
0025 //dynamic_pointer_cast overload for raw pointers
0026 template<class T, class U>
0027 inline T* dynamic_pointer_cast(U *ptr) noexcept
0028 {  
0029    return dynamic_cast<T*>(ptr);
0030 }
0031 
0032 //const_pointer_cast overload for raw pointers
0033 template<class T, class U>
0034 inline T* const_pointer_cast(U *ptr) noexcept
0035 {  
0036    return const_cast<T*>(ptr);
0037 }
0038 
0039 //reinterpret_pointer_cast overload for raw pointers
0040 template<class T, class U>
0041 inline T* reinterpret_pointer_cast(U *ptr) noexcept
0042 {  
0043    return reinterpret_cast<T*>(ptr);
0044 }
0045 
0046 //static_pointer_cast overload for std::shared_ptr
0047 using std::static_pointer_cast;
0048 
0049 //dynamic_pointer_cast overload for std::shared_ptr
0050 using std::dynamic_pointer_cast;
0051 
0052 //const_pointer_cast overload for std::shared_ptr
0053 using std::const_pointer_cast;
0054 
0055 //reinterpret_pointer_cast overload for std::shared_ptr
0056 template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) noexcept
0057 {
0058     (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
0059 
0060     typedef typename std::shared_ptr<T>::element_type E;
0061 
0062     E * p = reinterpret_cast< E* >( r.get() );
0063     return std::shared_ptr<T>( r, p );
0064 }
0065 
0066 //static_pointer_cast overload for std::unique_ptr
0067 template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) noexcept
0068 {
0069     (void) static_cast< T* >( static_cast< U* >( 0 ) );
0070 
0071     typedef typename std::unique_ptr<T>::element_type E;
0072 
0073     return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
0074 }
0075 
0076 //dynamic_pointer_cast overload for std::unique_ptr
0077 template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) noexcept
0078 {
0079     (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
0080 
0081     static_assert( std::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
0082 
0083     T * p = dynamic_cast<T*>( r.get() );
0084     if( p ) r.release();
0085     return std::unique_ptr<T>( p );
0086 }
0087 
0088 //const_pointer_cast overload for std::unique_ptr
0089 template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) noexcept
0090 {
0091     (void) const_cast< T* >( static_cast< U* >( 0 ) );
0092 
0093     typedef typename std::unique_ptr<T>::element_type E;
0094 
0095     return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
0096 }
0097 
0098 //reinterpret_pointer_cast overload for std::unique_ptr
0099 template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) noexcept
0100 {
0101     (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
0102 
0103     typedef typename std::unique_ptr<T>::element_type E;
0104 
0105     return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
0106 }
0107 
0108 } // namespace boost
0109 
0110 #endif   //BOOST_POINTER_CAST_HPP