pointer_cast.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <memory>
  12. #include <type_traits>
  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) 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) 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) 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) noexcept
  35. {
  36. return reinterpret_cast<T*>(ptr);
  37. }
  38. //static_pointer_cast overload for std::shared_ptr
  39. using std::static_pointer_cast;
  40. //dynamic_pointer_cast overload for std::shared_ptr
  41. using std::dynamic_pointer_cast;
  42. //const_pointer_cast overload for std::shared_ptr
  43. using std::const_pointer_cast;
  44. //reinterpret_pointer_cast overload for std::shared_ptr
  45. template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) noexcept
  46. {
  47. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  48. typedef typename std::shared_ptr<T>::element_type E;
  49. E * p = reinterpret_cast< E* >( r.get() );
  50. return std::shared_ptr<T>( r, p );
  51. }
  52. //static_pointer_cast overload for std::unique_ptr
  53. template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) noexcept
  54. {
  55. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  56. typedef typename std::unique_ptr<T>::element_type E;
  57. return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
  58. }
  59. //dynamic_pointer_cast overload for std::unique_ptr
  60. template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) noexcept
  61. {
  62. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  63. static_assert( std::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
  64. T * p = dynamic_cast<T*>( r.get() );
  65. if( p ) r.release();
  66. return std::unique_ptr<T>( p );
  67. }
  68. //const_pointer_cast overload for std::unique_ptr
  69. template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) noexcept
  70. {
  71. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  72. typedef typename std::unique_ptr<T>::element_type E;
  73. return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
  74. }
  75. //reinterpret_pointer_cast overload for std::unique_ptr
  76. template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) noexcept
  77. {
  78. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  79. typedef typename std::unique_ptr<T>::element_type E;
  80. return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
  81. }
  82. } // namespace boost
  83. #endif //BOOST_POINTER_CAST_HPP