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