polymorphic_cast.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // boost polymorphic_cast.hpp header file ----------------------------------------------//
  2. // (C) Copyright Kevlin Henney and Dave Abrahams 1999.
  3. // (C) Copyright Boris Rasin 2014.
  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. // Revision History
  9. // 10 Nov 14 polymorphic_pointer_downcast moved to a separate header,
  10. // minor improvements to stisfy latest Boost coding style
  11. // 08 Nov 14 Add polymorphic_pointer_downcast (Boris Rasin)
  12. // 09 Jun 14 "cast.hpp" was renamed to "polymorphic_cast.hpp" and
  13. // inclusion of numeric_cast was removed (Antony Polukhin)
  14. // 23 Jun 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
  15. // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
  16. // <boost/limits.hpp> instead (the workaround did not
  17. // actually compile when BOOST_NO_LIMITS was defined in
  18. // any case, so we loose nothing). (John Maddock)
  19. // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
  20. // worked with stock GCC; trying to get it to do that broke
  21. // vc-stlport.
  22. // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
  23. // Removed unused BOOST_EXPLICIT_TARGET macro. Moved
  24. // boost::detail::type to boost/type.hpp. Made it compile with
  25. // stock gcc again (Dave Abrahams)
  26. // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
  27. // Review (Beman Dawes)
  28. // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
  29. // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
  30. // (Dave Abrahams)
  31. // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
  32. // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
  33. // 27 Jun 00 More MSVC6 workarounds
  34. // 15 Jun 00 Add workarounds for MSVC6
  35. // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
  36. // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
  37. // 29 Dec 99 Change using declarations so usages in other namespaces work
  38. // correctly (Dave Abrahams)
  39. // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
  40. // as suggested Darin Adler and improved by Valentin Bonnard.
  41. // 2 Sep 99 Remove controversial asserts, simplify, rename.
  42. // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
  43. // place in nested namespace.
  44. // 3 Aug 99 Initial version
  45. #ifndef BOOST_POLYMORPHIC_CAST_HPP
  46. #define BOOST_POLYMORPHIC_CAST_HPP
  47. # include <boost/config.hpp>
  48. #ifdef BOOST_HAS_PRAGMA_ONCE
  49. # pragma once
  50. #endif
  51. # include <boost/assert.hpp>
  52. # include <boost/core/addressof.hpp>
  53. # include <boost/core/enable_if.hpp>
  54. # include <boost/throw_exception.hpp>
  55. # include <boost/type_traits/is_reference.hpp>
  56. # include <boost/type_traits/remove_reference.hpp>
  57. # include <typeinfo>
  58. namespace boost
  59. {
  60. // See the documentation for descriptions of how to choose between
  61. // static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<>
  62. // polymorphic_cast --------------------------------------------------------//
  63. // Runtime checked polymorphic downcasts and crosscasts.
  64. // Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
  65. // section 15.8 exercise 1, page 425.
  66. template <class Target, class Source>
  67. inline Target polymorphic_cast(Source* x)
  68. {
  69. Target tmp = dynamic_cast<Target>(x);
  70. if ( tmp == 0 ) boost::throw_exception( std::bad_cast() );
  71. return tmp;
  72. }
  73. // polymorphic_downcast ----------------------------------------------------//
  74. // BOOST_ASSERT() checked raw pointer polymorphic downcast. Crosscasts prohibited.
  75. // WARNING: Because this cast uses BOOST_ASSERT(), it violates
  76. // the One Definition Rule if used in multiple translation units
  77. // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
  78. // NDEBUG are defined inconsistently.
  79. // Contributed by Dave Abrahams
  80. template <class Target, class Source>
  81. inline Target polymorphic_downcast(Source* x)
  82. {
  83. BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error
  84. return static_cast<Target>(x);
  85. }
  86. // BOOST_ASSERT() checked reference polymorphic downcast. Crosscasts prohibited.
  87. // WARNING: Because this cast uses BOOST_ASSERT(), it violates
  88. // the One Definition Rule if used in multiple translation units
  89. // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
  90. // NDEBUG are defined inconsistently.
  91. // Contributed by Julien Delacroix
  92. template <class Target, class Source>
  93. inline typename boost::enable_if_c<
  94. boost::is_reference<Target>::value, Target
  95. >::type polymorphic_downcast(Source& x)
  96. {
  97. typedef typename boost::remove_reference<Target>::type* target_pointer_type;
  98. return *boost::polymorphic_downcast<target_pointer_type>(
  99. boost::addressof(x)
  100. );
  101. }
  102. } // namespace boost
  103. #endif // BOOST_POLYMORPHIC_CAST_HPP