Chris@102: // boost polymorphic_cast.hpp header file ----------------------------------------------// Chris@102: Chris@102: // (C) Copyright Kevlin Henney and Dave Abrahams 1999. Chris@102: // (C) Copyright Boris Rasin 2014. Chris@102: // Distributed under the Boost Chris@102: // Software License, Version 1.0. (See accompanying file Chris@102: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: Chris@102: // See http://www.boost.org/libs/conversion for Documentation. Chris@102: Chris@102: // Revision History Chris@102: // 10 Nov 14 polymorphic_pointer_downcast moved to a separate header, Chris@102: // minor improvements to stisfy latest Boost coding style Chris@102: // 08 Nov 14 Add polymorphic_pointer_downcast (Boris Rasin) Chris@102: // 09 Jun 14 "cast.hpp" was renamed to "polymorphic_cast.hpp" and Chris@102: // inclusion of numeric_cast was removed (Antony Polukhin) Chris@102: // 23 Jun 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola) Chris@102: // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included Chris@102: // instead (the workaround did not Chris@102: // actually compile when BOOST_NO_LIMITS was defined in Chris@102: // any case, so we loose nothing). (John Maddock) Chris@102: // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never Chris@102: // worked with stock GCC; trying to get it to do that broke Chris@102: // vc-stlport. Chris@102: // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp. Chris@102: // Removed unused BOOST_EXPLICIT_TARGET macro. Moved Chris@102: // boost::detail::type to boost/type.hpp. Made it compile with Chris@102: // stock gcc again (Dave Abrahams) Chris@102: // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal Chris@102: // Review (Beman Dawes) Chris@102: // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams) Chris@102: // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC Chris@102: // (Dave Abrahams) Chris@102: // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams) Chris@102: // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes) Chris@102: // 27 Jun 00 More MSVC6 workarounds Chris@102: // 15 Jun 00 Add workarounds for MSVC6 Chris@102: // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov) Chris@102: // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar) Chris@102: // 29 Dec 99 Change using declarations so usages in other namespaces work Chris@102: // correctly (Dave Abrahams) Chris@102: // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors Chris@102: // as suggested Darin Adler and improved by Valentin Bonnard. Chris@102: // 2 Sep 99 Remove controversial asserts, simplify, rename. Chris@102: // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast, Chris@102: // place in nested namespace. Chris@102: // 3 Aug 99 Initial version Chris@102: Chris@102: #ifndef BOOST_POLYMORPHIC_CAST_HPP Chris@102: #define BOOST_POLYMORPHIC_CAST_HPP Chris@102: Chris@102: # include Chris@102: # include Chris@102: # include Chris@102: # include Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: namespace boost Chris@102: { Chris@102: // See the documentation for descriptions of how to choose between Chris@102: // static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<> Chris@102: Chris@102: // polymorphic_cast --------------------------------------------------------// Chris@102: Chris@102: // Runtime checked polymorphic downcasts and crosscasts. Chris@102: // Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup, Chris@102: // section 15.8 exercise 1, page 425. Chris@102: Chris@102: template Chris@102: inline Target polymorphic_cast(Source* x) Chris@102: { Chris@102: Target tmp = dynamic_cast(x); Chris@102: if ( tmp == 0 ) boost::throw_exception( std::bad_cast() ); Chris@102: return tmp; Chris@102: } Chris@102: Chris@102: // polymorphic_downcast ----------------------------------------------------// Chris@102: Chris@102: // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited. Chris@102: Chris@102: // WARNING: Because this cast uses BOOST_ASSERT(), it violates Chris@102: // the One Definition Rule if used in multiple translation units Chris@102: // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER Chris@102: // NDEBUG are defined inconsistently. Chris@102: Chris@102: // Contributed by Dave Abrahams Chris@102: Chris@102: template Chris@102: inline Target polymorphic_downcast(Source* x) Chris@102: { Chris@102: BOOST_ASSERT( dynamic_cast(x) == x ); // detect logic error Chris@102: return static_cast(x); Chris@102: } Chris@102: Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_POLYMORPHIC_CAST_HPP