cast.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // boost cast.hpp header file ----------------------------------------------//
  2. // (C) Copyright Kevlin Henney and Dave Abrahams 1999.
  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. // Revision History
  8. // 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
  9. // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
  10. // <boost/limits.hpp> instead (the workaround did not
  11. // actually compile when BOOST_NO_LIMITS was defined in
  12. // any case, so we loose nothing). (John Maddock)
  13. // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
  14. // worked with stock GCC; trying to get it to do that broke
  15. // vc-stlport.
  16. // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
  17. // Removed unused BOOST_EXPLICIT_TARGET macro. Moved
  18. // boost::detail::type to boost/type.hpp. Made it compile with
  19. // stock gcc again (Dave Abrahams)
  20. // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
  21. // Review (Beman Dawes)
  22. // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
  23. // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
  24. // (Dave Abrahams)
  25. // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
  26. // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
  27. // 27 Jun 00 More MSVC6 workarounds
  28. // 15 Jun 00 Add workarounds for MSVC6
  29. // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
  30. // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
  31. // 29 Dec 99 Change using declarations so usages in other namespaces work
  32. // correctly (Dave Abrahams)
  33. // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
  34. // as suggested Darin Adler and improved by Valentin Bonnard.
  35. // 2 Sep 99 Remove controversial asserts, simplify, rename.
  36. // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
  37. // place in nested namespace.
  38. // 3 Aug 99 Initial version
  39. #ifndef BOOST_CAST_HPP
  40. #define BOOST_CAST_HPP
  41. # include <boost/config.hpp>
  42. # include <boost/assert.hpp>
  43. # include <typeinfo>
  44. # include <boost/type.hpp>
  45. # include <boost/limits.hpp>
  46. # include <boost/detail/select_type.hpp>
  47. // It has been demonstrated numerous times that MSVC 6.0 fails silently at link
  48. // time if you use a template function which has template parameters that don't
  49. // appear in the function's argument list.
  50. //
  51. // TODO: Add this to config.hpp?
  52. # if defined(BOOST_MSVC) && BOOST_MSVC < 1300
  53. # define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type<Target>* = 0
  54. # else
  55. # define BOOST_EXPLICIT_DEFAULT_TARGET
  56. # endif
  57. namespace boost
  58. {
  59. // See the documentation for descriptions of how to choose between
  60. // static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<>
  61. // polymorphic_cast --------------------------------------------------------//
  62. // Runtime checked polymorphic downcasts and crosscasts.
  63. // Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
  64. // section 15.8 exercise 1, page 425.
  65. template <class Target, class Source>
  66. inline Target polymorphic_cast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
  67. {
  68. Target tmp = dynamic_cast<Target>(x);
  69. if ( tmp == 0 ) throw std::bad_cast();
  70. return tmp;
  71. }
  72. // polymorphic_downcast ----------------------------------------------------//
  73. // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
  74. // WARNING: Because this cast uses BOOST_ASSERT(), it violates
  75. // the One Definition Rule if used in multiple translation units
  76. // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
  77. // NDEBUG are defined inconsistently.
  78. // Contributed by Dave Abrahams
  79. template <class Target, class Source>
  80. inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
  81. {
  82. BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error
  83. return static_cast<Target>(x);
  84. }
  85. # undef BOOST_EXPLICIT_DEFAULT_TARGET
  86. } // namespace boost
  87. # include <boost/numeric/conversion/cast.hpp>
  88. #endif // BOOST_CAST_HPP