]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/polymorphic_pointer_cast.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / polymorphic_pointer_cast.hpp
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)
7
8 // See http://www.boost.org/libs/conversion for Documentation.
9
10 #ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
11 #define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
12
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>
20 # endif
21
22 #ifdef BOOST_HAS_PRAGMA_ONCE
23 # pragma once
24 #endif
25
26 namespace boost
27 {
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<>
30
31 // polymorphic_pointer_downcast --------------------------------------------//
32
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.
36
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.
41
42 // Contributed by Boris Rasin
43
44 namespace detail
45 {
46 template <typename Target, typename Source>
47 struct dynamic_pointer_cast_result
48 {
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;
52 #else
53 typedef decltype(dynamic_pointer_cast<Target>(boost::declval<Source>())) type;
54 #endif
55 };
56 }
57
58 template <typename Target, typename Source>
59 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
60 polymorphic_pointer_downcast (const Source& x)
61 {
62 BOOST_ASSERT(dynamic_pointer_cast<Target> (x) == x);
63 return static_pointer_cast<Target> (x);
64 }
65
66 template <typename Target, typename Source>
67 inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
68 polymorphic_pointer_cast (const Source& x)
69 {
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() );
73
74 return tmp;
75 }
76
77 } // namespace boost
78
79 #endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP