polymorphic_pointer_cast.hpp 2.8 KB

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