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