9
3

pointer_cast.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //////////////////////////////////////////////////////////////////////////////
  9. #ifndef BOOST_POINTER_CAST_HPP
  10. #define BOOST_POINTER_CAST_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  13. namespace boost {
  14. //static_pointer_cast overload for raw pointers
  15. template<class T, class U>
  16. inline T* static_pointer_cast(U *ptr) BOOST_SP_NOEXCEPT
  17. {
  18. return static_cast<T*>(ptr);
  19. }
  20. //dynamic_pointer_cast overload for raw pointers
  21. template<class T, class U>
  22. inline T* dynamic_pointer_cast(U *ptr) BOOST_SP_NOEXCEPT
  23. {
  24. return dynamic_cast<T*>(ptr);
  25. }
  26. //const_pointer_cast overload for raw pointers
  27. template<class T, class U>
  28. inline T* const_pointer_cast(U *ptr) BOOST_SP_NOEXCEPT
  29. {
  30. return const_cast<T*>(ptr);
  31. }
  32. //reinterpret_pointer_cast overload for raw pointers
  33. template<class T, class U>
  34. inline T* reinterpret_pointer_cast(U *ptr) BOOST_SP_NOEXCEPT
  35. {
  36. return reinterpret_cast<T*>(ptr);
  37. }
  38. } // namespace boost
  39. #if !defined( BOOST_NO_CXX11_SMART_PTR )
  40. #include <boost/type_traits/has_virtual_destructor.hpp>
  41. #include <boost/static_assert.hpp>
  42. #include <memory>
  43. namespace boost {
  44. //static_pointer_cast overload for std::shared_ptr
  45. using std::static_pointer_cast;
  46. //dynamic_pointer_cast overload for std::shared_ptr
  47. using std::dynamic_pointer_cast;
  48. //const_pointer_cast overload for std::shared_ptr
  49. using std::const_pointer_cast;
  50. //reinterpret_pointer_cast overload for std::shared_ptr
  51. template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) BOOST_SP_NOEXCEPT
  52. {
  53. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  54. typedef typename std::shared_ptr<T>::element_type E;
  55. E * p = reinterpret_cast< E* >( r.get() );
  56. return std::shared_ptr<T>( r, p );
  57. }
  58. //static_pointer_cast overload for std::unique_ptr
  59. template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_SP_NOEXCEPT
  60. {
  61. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  62. typedef typename std::unique_ptr<T>::element_type E;
  63. return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
  64. }
  65. //dynamic_pointer_cast overload for std::unique_ptr
  66. template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_SP_NOEXCEPT
  67. {
  68. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  69. BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
  70. T * p = dynamic_cast<T*>( r.get() );
  71. if( p ) r.release();
  72. return std::unique_ptr<T>( p );
  73. }
  74. //const_pointer_cast overload for std::unique_ptr
  75. template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_SP_NOEXCEPT
  76. {
  77. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  78. typedef typename std::unique_ptr<T>::element_type E;
  79. return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
  80. }
  81. //reinterpret_pointer_cast overload for std::unique_ptr
  82. template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_SP_NOEXCEPT
  83. {
  84. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  85. typedef typename std::unique_ptr<T>::element_type E;
  86. return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
  87. }
  88. } // namespace boost
  89. #endif // #if !defined( BOOST_NO_CXX11_SMART_PTR )
  90. #endif //BOOST_POINTER_CAST_HPP