polymorphic_pointer_cast.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // boost polymorphic_pointer_cast.hpp header file ----------------------------------------------//
  2. // (C) Copyright Boris Rasin, 2014-2021.
  3. // (C) Copyright Antony Polukhin, 2014-2023.
  4. // Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/conversion for Documentation.
  8. #ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
  9. #define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
  10. # include <boost/config.hpp>
  11. # include <boost/assert.hpp>
  12. # include <boost/pointer_cast.hpp>
  13. # include <boost/throw_exception.hpp>
  14. # include <boost/utility/declval.hpp>
  15. # ifdef BOOST_NO_CXX11_DECLTYPE
  16. # include <boost/typeof/typeof.hpp>
  17. # endif
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. # pragma once
  20. #endif
  21. namespace boost
  22. {
  23. // See the documentation for descriptions of how to choose between
  24. // static_pointer_cast<>, dynamic_pointer_cast<>, polymorphic_pointer_cast<> and polymorphic_pointer_downcast<>
  25. // polymorphic_pointer_downcast --------------------------------------------//
  26. // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
  27. // Supports any type with static_pointer_cast/dynamic_pointer_cast functions:
  28. // built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc.
  29. // WARNING: Because this cast uses BOOST_ASSERT(), it violates
  30. // the One Definition Rule if used in multiple translation units
  31. // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
  32. // NDEBUG are defined inconsistently.
  33. // Contributed by Boris Rasin
  34. namespace detail
  35. {
  36. template <typename Target, typename Source>
  37. struct dynamic_pointer_cast_result
  38. {
  39. #ifdef BOOST_NO_CXX11_DECLTYPE
  40. BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, dynamic_pointer_cast<Target>(boost::declval<Source>()))
  41. typedef typename nested::type type;
  42. #else
  43. typedef decltype(dynamic_pointer_cast<Target>(boost::declval<Source>())) type;
  44. #endif
  45. };
  46. }
  47. template <typename Target, typename Source>
  48. inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
  49. polymorphic_pointer_downcast (const Source& x)
  50. {
  51. BOOST_ASSERT(dynamic_pointer_cast<Target> (x) == x);
  52. return static_pointer_cast<Target> (x);
  53. }
  54. template <typename Target, typename Source>
  55. inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
  56. polymorphic_pointer_cast (const Source& x)
  57. {
  58. typename detail::dynamic_pointer_cast_result<Target, Source>::type tmp
  59. = dynamic_pointer_cast<Target> (x);
  60. if ( !tmp ) boost::throw_exception( std::bad_cast() );
  61. return tmp;
  62. }
  63. } // namespace boost
  64. #endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP