2 // Copyright (c) Chris Glover, 2016.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_TYPE_INDEX_RUNTIME_CAST_BOOST_SHARED_PTR_CAST_HPP
10 #define BOOST_TYPE_INDEX_RUNTIME_CAST_BOOST_SHARED_PTR_CAST_HPP
12 /// \file boost_shared_ptr_cast.hpp
13 /// \brief Contains the overload of boost::typeindex::runtime_pointer_cast for
14 /// boost::shared_ptr types.
16 #include <boost/type_index/runtime_cast/detail/runtime_cast_impl.hpp>
17 #include <boost/type_traits/is_base_and_derived.hpp>
18 #include <boost/smart_ptr/shared_ptr.hpp>
20 #ifdef BOOST_HAS_PRAGMA_ONCE
24 namespace boost { namespace typeindex {
26 /// \brief Creates a new instance of std::shared_ptr whose stored pointer is obtained from u's
27 /// stored pointer using a runtime_cast.
29 /// The new shared_ptr will share ownership with u, except that it is empty if the runtime_cast
30 /// performed by runtime_pointer_cast returns a null pointer.
31 /// \tparam T The desired target type to return a pointer of.
32 /// \tparam U A complete class type of the source instance pointed to from u.
33 /// \return If there exists a valid conversion from U* to T*, returns a boost::shared_ptr<T>
34 /// that points to an address suitably offset from u.
35 /// If no such conversion exists, returns boost::shared_ptr<T>();
36 template<typename T, typename U>
37 boost::shared_ptr<T> runtime_pointer_cast(boost::shared_ptr<U> const& u) {
38 T* value = detail::runtime_cast_impl<T>(u.get(), boost::is_base_and_derived<T, U>());
40 return boost::shared_ptr<T>(u, value);
41 return boost::shared_ptr<T>();
44 }} // namespace boost::typeindex
46 #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_BOOST_SHARED_PTR_CAST_HPP