diff --git a/cast.htm b/cast.htm index d913d72..4330d89 100644 --- a/cast.htm +++ b/cast.htm @@ -27,10 +27,10 @@
The header boost/polymorphic_cast.hpp provides
- polymorphic_cast
,
- polymorphic_downcast
and
- polymorphic_pointer_downcast
- function templates designed to complement the C++ built-in casts.
polymorphic_cast
and
+ polymorphic_downcast
+ function templates designed to complement the C++ built-in casts. The header boost/polymorphic_pointer_cast.hpp provides
+ polymorphic_pointer_downcast
function template.
The program cast_test.cpp can be used to verify these function templates work as expected.
@@ -137,7 +137,7 @@ void f( Fruit * fruit ) {-#include <boost/polymorphic_cast.hpp> +#include <boost/polymorphic_pointer_cast.hpp> class Fruit { public: virtual ~Fruit(){} }; class Banana : public Fruit {}; diff --git a/include/boost/polymorphic_cast.hpp b/include/boost/polymorphic_cast.hpp index 7221ade..70afa6b 100644 --- a/include/boost/polymorphic_cast.hpp +++ b/include/boost/polymorphic_cast.hpp @@ -9,6 +9,8 @@ // See http://www.boost.org/libs/conversion for Documentation. // Revision History +// 10 Nov 14 polymorphic_pointer_downcast moved to a separate header, +// minor improvements to stisfy latest Boost coding style // 08 Nov 14 Add polymorphic_pointer_downcast (Boris Rasin) // 09 Jun 14 "cast.hpp" was renamed to "polymorphic_cast.hpp" and // inclusion of numeric_cast was removed (Antony Polukhin) @@ -49,11 +51,13 @@ # include# include -# include -# include -# include +# include # include +#ifdef BOOST_HAS_PRAGMA_ONCE +# pragma once +#endif + namespace boost { // See the documentation for descriptions of how to choose between @@ -69,7 +73,7 @@ namespace boost inline Target polymorphic_cast(Source* x) { Target tmp = dynamic_cast (x); - if ( tmp == 0 ) throw std::bad_cast(); + if ( tmp == 0 ) boost::throw_exception( std::bad_cast() ); return tmp; } @@ -91,41 +95,6 @@ namespace boost return static_cast (x); } -// polymorphic_pointer_downcast --------------------------------------------// - - // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited. - // Supports any type with static_pointer_cast/dynamic_pointer_cast functions: - // built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc. - - // WARNING: Because this cast uses BOOST_ASSERT(), it violates - // the One Definition Rule if used in multiple translation units - // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER - // NDEBUG are defined inconsistently. - - // Contributed by Boris Rasin - - namespace detail - { - template - struct static_pointer_cast_result - { -#ifdef BOOST_NO_CXX11_DECLTYPE - BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, static_pointer_cast (boost::declval ())) - typedef typename nested::type type; -#else - typedef decltype(static_pointer_cast (boost::declval ())) type; -#endif - }; - } - - template - inline typename detail::static_pointer_cast_result ::type - polymorphic_pointer_downcast (const Source& x) - { - BOOST_ASSERT(dynamic_pointer_cast (x) == x); - return static_pointer_cast (x); - } - } // namespace boost #endif // BOOST_POLYMORPHIC_CAST_HPP diff --git a/include/boost/polymorphic_pointer_cast.hpp b/include/boost/polymorphic_pointer_cast.hpp new file mode 100644 index 0000000..bcf6d0f --- /dev/null +++ b/include/boost/polymorphic_pointer_cast.hpp @@ -0,0 +1,66 @@ +// boost polymorphic_pointer_cast.hpp header file ----------------------------------------------// +// (C) Copyright Boris Rasin 2014. +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/conversion for Documentation. + +#ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP +#define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP + +# include +# include +# include +# include +# ifdef BOOST_NO_CXX11_DECLTYPE +# include +# endif + +#ifdef BOOST_HAS_PRAGMA_ONCE +# pragma once +#endif + +namespace boost +{ +// See the documentation for descriptions of how to choose between +// static_pointer_cast<>, dynamic_pointer_cast<>, polymorphic_pointer_cast<> and polymorphic_pointer_downcast<> + +// polymorphic_pointer_downcast --------------------------------------------// + + // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited. + // Supports any type with static_pointer_cast/dynamic_pointer_cast functions: + // built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc. + + // WARNING: Because this cast uses BOOST_ASSERT(), it violates + // the One Definition Rule if used in multiple translation units + // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER + // NDEBUG are defined inconsistently. + + // Contributed by Boris Rasin + + namespace detail + { + template + struct static_pointer_cast_result + { +#ifdef BOOST_NO_CXX11_DECLTYPE + BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, static_pointer_cast (boost::declval ())) + typedef typename nested::type type; +#else + typedef decltype(static_pointer_cast (boost::declval ())) type; +#endif + }; + } + + template + inline typename detail::static_pointer_cast_result ::type + polymorphic_pointer_downcast (const Source& x) + { + BOOST_ASSERT(dynamic_pointer_cast (x) == x); + return static_pointer_cast (x); + } + +} // namespace boost + +#endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP diff --git a/test/polymorphic_cast_test.cpp b/test/polymorphic_cast_test.cpp index 9c79152..9a11068 100644 --- a/test/polymorphic_cast_test.cpp +++ b/test/polymorphic_cast_test.cpp @@ -13,6 +13,7 @@ #define BOOST_ENABLE_ASSERT_HANDLER #include +#include #include #include #include