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