1 // boost polymorphic_pointer_cast.hpp header file ----------------------------------------------//
2 // (C) Copyright Boris Rasin, 2014-2021.
3 // (C) Copyright Antony Polukhin, 2014-2022.
4 // Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/conversion for Documentation.
10 #ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
11 #define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
13 # include <boost/config.hpp>
14 # include <boost/assert.hpp>
15 # include <boost/pointer_cast.hpp>
16 # include <boost/throw_exception.hpp>
17 # include <boost/utility/declval.hpp>
18 # ifdef BOOST_NO_CXX11_DECLTYPE
19 # include <boost/typeof/typeof.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
28 // See the documentation for descriptions of how to choose between
29 // static_pointer_cast<>, dynamic_pointer_cast<>, polymorphic_pointer_cast<> and polymorphic_pointer_downcast<>
31 // polymorphic_pointer_downcast --------------------------------------------//
33 // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
34 // Supports any type with static_pointer_cast/dynamic_pointer_cast functions:
35 // built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc.
37 // WARNING: Because this cast uses BOOST_ASSERT(), it violates
38 // the One Definition Rule if used in multiple translation units
39 // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
40 // NDEBUG are defined inconsistently.
42 // Contributed by Boris Rasin
46 template <typename Target, typename Source>
47 struct dynamic_pointer_cast_result
49 #ifdef BOOST_NO_CXX11_DECLTYPE
50 BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, dynamic_pointer_cast<Target>(boost::declval<Source>()))
51 typedef typename nested::type type;
53 typedef decltype(dynamic_pointer_cast<Target>(boost::declval<Source>())) type;
58 template <typename Target, typename Source>
59 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
60 polymorphic_pointer_downcast (const Source& x)
62 BOOST_ASSERT(dynamic_pointer_cast<Target> (x) == x);
63 return static_pointer_cast<Target> (x);
66 template <typename Target, typename Source>
67 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
68 polymorphic_pointer_cast (const Source& x)
70 typename detail::dynamic_pointer_cast_result<Target, Source>::type tmp
71 = dynamic_pointer_cast<Target> (x);
72 if ( !tmp ) boost::throw_exception( std::bad_cast() );
79 #endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP