Chris@102
|
1 // boost polymorphic_pointer_cast.hpp header file ----------------------------------------------//
|
Chris@102
|
2 // (C) Copyright Boris Rasin and Antony Polukhin 2014-2015.
|
Chris@102
|
3 // Distributed under the Boost
|
Chris@102
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6
|
Chris@102
|
7 // See http://www.boost.org/libs/conversion for Documentation.
|
Chris@102
|
8
|
Chris@102
|
9 #ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|
Chris@102
|
10 #define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|
Chris@102
|
11
|
Chris@102
|
12 # include <boost/config.hpp>
|
Chris@102
|
13 # include <boost/assert.hpp>
|
Chris@102
|
14 # include <boost/pointer_cast.hpp>
|
Chris@102
|
15 # include <boost/throw_exception.hpp>
|
Chris@102
|
16 # include <boost/utility/declval.hpp>
|
Chris@102
|
17 # ifdef BOOST_NO_CXX11_DECLTYPE
|
Chris@102
|
18 # include <boost/typeof/typeof.hpp>
|
Chris@102
|
19 # endif
|
Chris@102
|
20
|
Chris@102
|
21 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
22 # pragma once
|
Chris@102
|
23 #endif
|
Chris@102
|
24
|
Chris@102
|
25 namespace boost
|
Chris@102
|
26 {
|
Chris@102
|
27 // See the documentation for descriptions of how to choose between
|
Chris@102
|
28 // static_pointer_cast<>, dynamic_pointer_cast<>, polymorphic_pointer_cast<> and polymorphic_pointer_downcast<>
|
Chris@102
|
29
|
Chris@102
|
30 // polymorphic_pointer_downcast --------------------------------------------//
|
Chris@102
|
31
|
Chris@102
|
32 // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
|
Chris@102
|
33 // Supports any type with static_pointer_cast/dynamic_pointer_cast functions:
|
Chris@102
|
34 // built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc.
|
Chris@102
|
35
|
Chris@102
|
36 // WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
Chris@102
|
37 // the One Definition Rule if used in multiple translation units
|
Chris@102
|
38 // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
|
Chris@102
|
39 // NDEBUG are defined inconsistently.
|
Chris@102
|
40
|
Chris@102
|
41 // Contributed by Boris Rasin
|
Chris@102
|
42
|
Chris@102
|
43 namespace detail
|
Chris@102
|
44 {
|
Chris@102
|
45 template <typename Target, typename Source>
|
Chris@102
|
46 struct dynamic_pointer_cast_result
|
Chris@102
|
47 {
|
Chris@102
|
48 #ifdef BOOST_NO_CXX11_DECLTYPE
|
Chris@102
|
49 BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, dynamic_pointer_cast<Target>(boost::declval<Source>()))
|
Chris@102
|
50 typedef typename nested::type type;
|
Chris@102
|
51 #else
|
Chris@102
|
52 typedef decltype(dynamic_pointer_cast<Target>(boost::declval<Source>())) type;
|
Chris@102
|
53 #endif
|
Chris@102
|
54 };
|
Chris@102
|
55 }
|
Chris@102
|
56
|
Chris@102
|
57 template <typename Target, typename Source>
|
Chris@102
|
58 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
|
Chris@102
|
59 polymorphic_pointer_downcast (const Source& x)
|
Chris@102
|
60 {
|
Chris@102
|
61 BOOST_ASSERT(dynamic_pointer_cast<Target> (x) == x);
|
Chris@102
|
62 return static_pointer_cast<Target> (x);
|
Chris@102
|
63 }
|
Chris@102
|
64
|
Chris@102
|
65 template <typename Target, typename Source>
|
Chris@102
|
66 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
|
Chris@102
|
67 polymorphic_pointer_cast (const Source& x)
|
Chris@102
|
68 {
|
Chris@102
|
69 typename detail::dynamic_pointer_cast_result<Target, Source>::type tmp
|
Chris@102
|
70 = dynamic_pointer_cast<Target> (x);
|
Chris@102
|
71 if ( !tmp ) boost::throw_exception( std::bad_cast() );
|
Chris@102
|
72
|
Chris@102
|
73 return tmp;
|
Chris@102
|
74 }
|
Chris@102
|
75
|
Chris@102
|
76 } // namespace boost
|
Chris@102
|
77
|
Chris@102
|
78 #endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|