]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2005. | |
4 | // Distributed under the Boost Software License, Version 1.0. | |
5 | // (See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | ////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef BOOST_POINTER_CAST_HPP | |
11 | #define BOOST_POINTER_CAST_HPP | |
12 | ||
13 | #include <boost/config.hpp> | |
14 | ||
15 | namespace boost { | |
16 | ||
17 | //static_pointer_cast overload for raw pointers | |
18 | template<class T, class U> | |
19 | inline T* static_pointer_cast(U *ptr) | |
20 | { | |
21 | return static_cast<T*>(ptr); | |
22 | } | |
23 | ||
24 | //dynamic_pointer_cast overload for raw pointers | |
25 | template<class T, class U> | |
26 | inline T* dynamic_pointer_cast(U *ptr) | |
27 | { | |
28 | return dynamic_cast<T*>(ptr); | |
29 | } | |
30 | ||
31 | //const_pointer_cast overload for raw pointers | |
32 | template<class T, class U> | |
33 | inline T* const_pointer_cast(U *ptr) | |
34 | { | |
35 | return const_cast<T*>(ptr); | |
36 | } | |
37 | ||
38 | //reinterpret_pointer_cast overload for raw pointers | |
39 | template<class T, class U> | |
40 | inline T* reinterpret_pointer_cast(U *ptr) | |
41 | { | |
42 | return reinterpret_cast<T*>(ptr); | |
43 | } | |
44 | ||
45 | } // namespace boost | |
46 | ||
47 | #if !defined( BOOST_NO_CXX11_SMART_PTR ) | |
48 | ||
49 | #include <boost/type_traits/has_virtual_destructor.hpp> | |
50 | #include <boost/static_assert.hpp> | |
51 | #include <memory> | |
52 | ||
53 | namespace boost { | |
54 | ||
55 | //static_pointer_cast overload for std::shared_ptr | |
56 | using std::static_pointer_cast; | |
57 | ||
58 | //dynamic_pointer_cast overload for std::shared_ptr | |
59 | using std::dynamic_pointer_cast; | |
60 | ||
61 | //const_pointer_cast overload for std::shared_ptr | |
62 | using std::const_pointer_cast; | |
63 | ||
64 | //reinterpret_pointer_cast overload for std::shared_ptr | |
65 | template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) BOOST_NOEXCEPT | |
66 | { | |
67 | (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); | |
68 | ||
69 | typedef typename std::shared_ptr<T>::element_type E; | |
70 | ||
71 | E * p = reinterpret_cast< E* >( r.get() ); | |
72 | return std::shared_ptr<T>( r, p ); | |
73 | } | |
74 | ||
75 | //static_pointer_cast overload for std::unique_ptr | |
76 | template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT | |
77 | { | |
78 | (void) static_cast< T* >( static_cast< U* >( 0 ) ); | |
79 | ||
80 | typedef typename std::unique_ptr<T>::element_type E; | |
81 | ||
82 | return std::unique_ptr<T>( static_cast<E*>( r.release() ) ); | |
83 | } | |
84 | ||
85 | //dynamic_pointer_cast overload for std::unique_ptr | |
86 | template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT | |
87 | { | |
88 | (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); | |
89 | ||
90 | BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." ); | |
91 | ||
92 | T * p = dynamic_cast<T*>( r.get() ); | |
93 | if( p ) r.release(); | |
94 | return std::unique_ptr<T>( p ); | |
95 | } | |
96 | ||
97 | //const_pointer_cast overload for std::unique_ptr | |
98 | template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT | |
99 | { | |
100 | (void) const_cast< T* >( static_cast< U* >( 0 ) ); | |
101 | ||
102 | typedef typename std::unique_ptr<T>::element_type E; | |
103 | ||
104 | return std::unique_ptr<T>( const_cast<E*>( r.release() ) ); | |
105 | } | |
106 | ||
107 | //reinterpret_pointer_cast overload for std::unique_ptr | |
108 | template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT | |
109 | { | |
110 | (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); | |
111 | ||
112 | typedef typename std::unique_ptr<T>::element_type E; | |
113 | ||
114 | return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) ); | |
115 | } | |
116 | ||
117 | } // namespace boost | |
118 | ||
119 | #endif // #if !defined( BOOST_NO_CXX11_SMART_PTR ) | |
120 | ||
121 | #endif //BOOST_POINTER_CAST_HPP |